In [1]:
from theano import *
import theano.tensor as T
In [2]:
# make a function
# scalar
from theano import function # function is compiled by C
x = T.dscalar('x') # set Variable (symbol)
y = T.dscalar('y')
z = x + y
f = function([x,y], z)
In [3]:
f(2,3)
Out[3]:
In [4]:
f(16.3, 12.1)
Out[4]:
In [6]:
type(x)
type(y)
Out[6]:
In [7]:
from theano import pp
print pp(z)
In [8]:
# make a function
# matrix
x = T.dmatrix('x')
y = T.dmatrix('y')
z = x + y
f = function([x,y], z)
In [9]:
f([[1,2], [3,4]], [[10,20], [30,40]])
Out[9]:
In [10]:
# make a funtion
# broadcasting
x = T.dmatrix('x')
y = T.dscalar('y')
z = x * y
f = function([x,y], z)
In [11]:
f([[1,2],[3,4]], 5)
Out[11]:
In [12]:
# exercise
a = theano.tensor.vector() # declare variable
b = theano.tensor.vector()
out = a**2 + b**2 + 2*a*b # build symbolic expression
f = theano.function([a, b], out) # compile function
print(f([0, 1, 2], [2,3,4]))
In [ ]: