Table Of Contents

Tinkering With Activation Functions

In [5]:
import tensorflow as tf
import matplotlib.pyplot as plt

Create Some Mock Data

In [4]:
dataMin = -10
dataMax = 10
rangeTensor = tf.range(dataMin, dataMax)
mockData = tf.cast(dataRange, tf.float32)
mockData
Out [4]:
<tf.Tensor: shape=(20,), dtype=float32, numpy=
array([-10.,  -9.,  -8.,  -7.,  -6.,  -5.,  -4.,  -3.,  -2.,  -1.,   0.,
         1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.],
      dtype=float32)>
In [7]:
# Visualize the data
plt.plot(mockData);
output png

A Sigmoid Function

In [10]:
# Sigmoid - https://www.tensorflow.org/api_docs/python/tf/keras/activations/sigmoid
def mySigmoid(x):
  return 1 / (1 + tf.exp(-x))

# Use the sigmoid function on our tensor
sigmoidData = mySigmoid(mockData)
sigmoidData
Out [10]:
<tf.Tensor: shape=(20,), dtype=float32, numpy=
array([4.5397872e-05, 1.2339458e-04, 3.3535017e-04, 9.1105117e-04,
       2.4726233e-03, 6.6928510e-03, 1.7986210e-02, 4.7425874e-02,
       1.1920292e-01, 2.6894143e-01, 5.0000000e-01, 7.3105860e-01,
       8.8079703e-01, 9.5257413e-01, 9.8201376e-01, 9.9330717e-01,
       9.9752742e-01, 9.9908900e-01, 9.9966466e-01, 9.9987662e-01],
      dtype=float32)>
In [11]:
plt.plot(sigmoidData);
output png

A Relu Function

In [14]:
# ReLU - https://www.tensorflow.org/api_docs/python/tf/keras/activations/relu
def myRelu(x):
  return tf.maximum(0, x)

reluData = myRelu(mockData)
reluData
Out [14]:
<tf.Tensor: shape=(20,), dtype=float32, numpy=
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 2., 3., 4., 5., 6.,
       7., 8., 9.], dtype=float32)>
In [15]:
plt.plot(reluData)
Out [15]:
[<matplotlib.lines.Line2D at 0x7f2df99c10>]
output png