import theano.tensor as T
from theano import function
x = T.dscalar('x')
y = T.dscalar('y')
z = x + y
f = function([x, y], z)
==========================
f(2, 3)
array(5.0)
f(16.3, 12.1)
array(28.4)
T.dscalar is the type we assign to “0-dimensional arrays (scalar) of doubles (d)” It is a Theano Type. dscalar is not a class. Therefore, neither x nor y are actually instances of dscalar. They are instances of TensorVariable. x and y are, however, assigned the theano Type dscalar in their type field
type(x)
theano.tensor.var.TensorVariable
x.type
TensorType(float64, scalar)
T.dscalar
TensorType(float64, scalar)
x.type is T.dscalar
True
You can use the pp function to pretty-print out the computation associated to z.
from theano import pp
print pp(z)
(x + y)
f = function([x, y], z)
The first argument([x,y]) is inputs. The second argment(z) is output
z.eval({x:16.3, y:12.1})
array(28.4)
x = T.dmatrix('x')
y = T.dmatrix('y')
z = x + y
f = function([x, y], z)
f([[1, 2], [3, 4]], [[10, 20], [30, 40]])
array([[ 11., 22.], [ 33., 44.]])
import numpy
f(numpy.array([[1, 2], [3, 4]]), numpy.array([[10, 20], [30, 40]]))
array([[ 11., 22.], [ 33., 44.]])
i = T.iscalar('i')
l = T.lscalar('l')
j = T.scalar('j')
i.type
TensorType(int32, scalar)
l.type
TensorType(int64, scalar)
j.type
TensorType(float64, scalar)
a = T.vector()
out = a + a ** 10
f = function([a],out)
print f([0,1,2])
[ 0. 2. 1026.]
b = T.vector()
out2 = a**2 + b**2 + 2*a*b
f2 = function([a,b], out2)
print f2([1,2,3],[2,3,4])
[ 9. 25. 49.]