In [1]:
import theano.tensor as T
from theano import function
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
In [15]:
import theano
import theano.tensor as T
Out[15]:
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]:
In [19]:
# Alternate formulation
s2 = (1 + T.tanh(x / 2)) / 2
logistic2 = theano.function([x], s2)
# test
logistic2([[0,1],[-1,-2]])
Out[19]:
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]:
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)
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)
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]:
In [30]:
accumulator(1)
Out[30]:
In [ ]: