Tutorial on the logistic function
In [1]:
%load_ext version_information
%version_information theano, numpy
Out[1]:
We want to compute the logistic function:
$$s(x) = \frac{1}{1 + e^{-x}}$$We apply the function element-wise on the tensor like so.
In [2]:
import theano
import theano.tensor as T
import numpy as np
# Same recipe as last tutorial:
x = T.dmatrix("x") # Define variables
s = 1 / (1 + T.exp(-x)) # Build symbolic expression
logistic = theano.function([x], s) # Compile function
my_matrix = np.array([[0, 1], [-1, -2]])
logistic(my_matrix)
Out[2]:
There's another definition of the logistic function, using the hyperbolic tangent.
$$s(x) = \frac{1}{1 + e^{-x}} = \frac{1 + \tanh(x / 2)}{2}$$Which we can use to get the same result.
In [3]:
s2 =(1 + T.tanh(x / 2)) / 2
logistic2 = theano.function([x], s2)
logistic2(my_matrix)
Out[3]: