In [37]:
import theano
import theano.tensor as T
In [38]:
x = T.scalar()
In [39]:
x
Out[39]:
Variables can be used in expressions
In [40]:
y = 3*(x**2) + 1
Result is symbolic as well
In [41]:
type(y)
Out[41]:
Investigating expressions
In [42]:
print(y)
In [43]:
theano.pprint(y)
Out[43]:
In [44]:
theano.printing.debugprint(y)
In [45]:
from IPython.display import SVG
SVG(theano.printing.pydotprint(y, return_image=True, format='svg'))
Out[45]:
In [46]:
y.eval({x: 100})
Out[46]:
Or compile a function
In [47]:
f = theano.function([x], y)
In [48]:
f(20)
Out[48]:
Compiled function has been transformed
In [49]:
SVG(theano.printing.pydotprint(f, return_image=True, format='svg'))
Out[49]:
In [50]:
X = T.vector()
X = T.matrix()
X = T.tensor3()
X = T.tensor4()
In [51]:
X = T.vector()
In [52]:
X[1:-1:2]
Out[52]:
In [53]:
X[[1,2,3]]
Out[53]:
Many functions/operations are available through theano.tensor or variable methods
In [54]:
y = X.argmax()
In [55]:
y = T.cosh(X)
In [56]:
y = T.outer(X, X)
But don't try to use numpy functions on Theano variables. Results may vary!
In [57]:
x = T.scalar()
y = T.log(x)
In [58]:
gradient = T.grad(y, x)
gradient.eval({x: 2})
Out[58]:
In [59]:
import numpy as np
x = theano.shared(np.zeros((2, 3), dtype=theano.config.floatX))
In [60]:
x
Out[60]:
We can get and set the variable's value
In [61]:
values = x.get_value()
print(values.shape)
print(values)
In [62]:
x.set_value(values)
Shared variables can be used in expressions as well
In [63]:
(x + 2) ** 2
Out[63]:
Their value is used as input when evaluating
In [64]:
((x + 2) ** 2).eval()
Out[64]:
In [65]:
theano.function([], (x + 2) ** 2)()
Out[65]:
In [66]:
count = theano.shared(0)
new_count = count + 1
updates = {count: new_count}
f = theano.function([], count, updates=updates)
In [67]:
f()
Out[67]:
In [68]:
f()
Out[68]:
In [69]:
f()
Out[69]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: