In [1]:
import theano.tensor as T
from theano import function


Couldn't import dot_parser, loading of dot files will not be possible.
Vendor:  Continuum Analytics, Inc.
Package: mkl
Message: trial mode expires in 30 days
Vendor:  Continuum Analytics, Inc.
Package: mkl
Message: trial mode expires in 30 days

In [13]:
x = T.dscalar('x')
y = T.dscalar('y')
z = x + y
f = function([x, y], z)

print f(2, 3)
print f(16.3, 12.1)
print type(x)
print x.type
print T.dscalar
print x.type is T.dscalar


5.0
28.4
<class 'theano.tensor.var.TensorVariable'>
TensorType(float64, scalar)
TensorType(float64, scalar)
True

Logistic Regression


In [15]:
import theano
import theano.tensor as T


Out[15]:
array([[ 0.5       ,  0.73105858],
       [ 0.26894142,  0.11920292]])

In [18]:
# Logisitic Function
x = T.dmatrix('x')
s = 1 / (1 + T.exp(-x))
logistic = theano.function([x], s)

# test
logistic([[0,1],[-1,-2]])


Out[18]:
array([[ 0.5       ,  0.73105858],
       [ 0.26894142,  0.11920292]])

In [19]:
# Alternate formulation
s2 = (1 + T.tanh(x / 2)) / 2
logistic2 = theano.function([x], s2)

# test
logistic2([[0,1],[-1,-2]])


Out[19]:
array([[ 0.5       ,  0.73105858],
       [ 0.26894142,  0.11920292]])

In [21]:
# Computing more than one thing at the same time
a, b = T.dmatrices('a','b')
diff = a - b
abs_diff = abs(diff)
diff_squared = diff**2
f = theano.function([a,b], [diff, abs_diff, diff_squared])

In [22]:
f([[1, 1], [1, 1]],[[0, 1], [2, 3]])


Out[22]:
[array([[ 1.,  0.],
        [-1., -2.]]), array([[ 1.,  0.],
        [ 1.,  2.]]), array([[ 1.,  0.],
        [ 1.,  4.]])]

In [23]:
# Setting a Default Value for an argument
from theano import Param
from theano import function
x, y = T.dscalars('x', 'y')
z = x + y
f = function([x, Param(y, default=1)], z)

print f(33)

print f(33,2)


34.0
35.0

In [25]:
x, y, w = T.dscalars('x','y','w')
z = (x + y) * w
f = function([x, Param(y, default=1), Param(w,default=2, name='w_by_name')], z)

print f(33)
print f(33, 2)
print f(33, 0, 1)
print f(33, w_by_name=1)
print f(33, w_by_name=1, y=0)


68.0
70.0
33.0
34.0
33.0

In [26]:
# Using shared variables
from theano import shared
state = shared(0)
inc = T.iscalar('inc')
accumulator = function([inc], state, updates=[(state, state+inc)])

In [27]:
state.get_value()


Out[27]:
array(0)

In [30]:
accumulator(1)


Out[30]:
array(0)

In [ ]: