In [17]:
import theano
import theano.tensor as T
In [18]:
x = T.scalar()
In [19]:
x
Out[19]:
Variables can be used in expressions
In [20]:
y = 3*(x**2) + 1
Result is symbolic as well
In [21]:
type(y)
Out[21]:
Investigating expressions
In [22]:
print(y)
In [23]:
theano.pprint(y)
Out[23]:
In [24]:
theano.printing.debugprint(y)
In [25]:
from IPython.display import SVG
SVG(theano.printing.pydotprint(y, return_image=True, format='svg'))
Out[25]:
In [26]:
y.eval({x: 100})
Out[26]:
Or compile a function
In [27]:
f = theano.function([x], y)
In [28]:
f(20)
Out[28]:
Compiled function has been transformed
In [29]:
SVG(theano.printing.pydotprint(f, return_image=True, format='svg'))
Out[29]:
In [30]:
X = T.vector()
X = T.matrix()
X = T.tensor3()
X = T.tensor4()
In [31]:
X = T.vector()
In [32]:
X[1:-1:2]
Out[32]:
In [33]:
X[[1,2,3]]
Out[33]:
Many functions/operations are available through theano.tensor
or variable methods
In [34]:
y = X.argmax()
In [35]:
y = T.cosh(X)
In [36]:
y = T.outer(X, X)
But don't try to use numpy functions on Theano variables. Results may vary!
In [37]:
x = T.scalar()
y = T.log(x)
In [38]:
gradient = T.grad(y, x)
gradient.eval({x: 2})
Out[38]:
In [39]:
import numpy as np
x = theano.shared(np.zeros((2, 3), dtype=theano.config.floatX))
In [40]:
x
Out[40]:
We can get and set the variable's value
In [41]:
values = x.get_value()
print(values.shape)
print(values)
In [42]:
x.set_value(values)
Shared variables can be used in expressions as well
In [43]:
(x + 2) ** 2
Out[43]:
Their value is used as input when evaluating
In [44]:
((x + 2) ** 2).eval()
Out[44]:
In [45]:
theano.function([], (x + 2) ** 2)()
Out[45]:
In [46]:
count = theano.shared(0)
new_count = count + 1
updates = {count: new_count}
f = theano.function([], count, updates=updates)
In [47]:
f()
Out[47]:
In [48]:
f()
Out[48]:
In [49]:
f()
Out[49]: