An optimizing compiler for symbolic math expressions
import theano
import theano.tensor as T
x = T.scalar()
x
<TensorType(float32, scalar)>
Variables can be used in expressions
y = 3*(x**2) + 1
Result is symbolic as well
type(y)
theano.tensor.var.TensorVariable
Investigating expressions
print(y)
Elemwise{add,no_inplace}.0
theano.pprint(y)
'((TensorConstant{3} * (<TensorType(float32, scalar)> ** TensorConstant{2})) + TensorConstant{1})'
theano.printing.debugprint(y)
Elemwise{add,no_inplace} [@A] '' |Elemwise{mul,no_inplace} [@B] '' | |TensorConstant{3} [@C] | |Elemwise{pow,no_inplace} [@D] '' | |<TensorType(float32, scalar)> [@E] | |TensorConstant{2} [@F] |TensorConstant{1} [@G]
from IPython.display import SVG
SVG(theano.printing.pydotprint(y, return_image=True, format='svg'))
Supply a dict
mapping variables to values
y.eval({x: 2})
array(13.0, dtype=float32)
Or compile a function
f = theano.function([x], y)
f(2)
array(13.0, dtype=float32)
Compiled function has been transformed
SVG(theano.printing.pydotprint(f, return_image=True, format='svg'))
X = T.vector()
X = T.matrix()
X = T.tensor3()
X = T.tensor4()
X = T.vector()
X[1:-1:2]
Subtensor{int64:int64:int64}.0
X[[1,2,3]]
AdvancedSubtensor1.0
Many functions/operations are available through theano.tensor
or variable methods
y = X.argmax()
y = T.cosh(X)
y = T.outer(X, X)
But don't try to use numpy functions on Theano variables. Results may vary!
x = T.scalar()
y = T.log(x)
gradient = T.grad(y, x)
gradient.eval({x: 2})
array(0.5, dtype=float32)
import numpy as np
x = theano.shared(np.zeros((2, 3), dtype=theano.config.floatX))
x
<CudaNdarrayType(float32, matrix)>
We can get and set the variable's value
values = x.get_value()
print(values.shape)
print(values)
(2, 3) [[ 0. 0. 0.] [ 0. 0. 0.]]
x.set_value(values)
Shared variables can be used in expressions as well
(x + 2) ** 2
Elemwise{pow,no_inplace}.0
Their value is used as input when evaluating
((x + 2) ** 2).eval()
array([[ 4., 4., 4.], [ 4., 4., 4.]], dtype=float32)
theano.function([], (x + 2) ** 2)()
array([[ 4., 4., 4.], [ 4., 4., 4.]], dtype=float32)
dict
mapping shared variables to new valuescount = theano.shared(0)
new_count = count + 1
updates = {count: new_count}
f = theano.function([], count, updates=updates)
f()
array(0)
f()
array(1)
f()
array(2)