Theano Cheatsheet: Imports
Theano Cheatsheet: Imports
Imports:
import theano #main module
from theano import tensor as T #variables & vector/matrix/tensor operations
Symbolic variables
X = T.matrix()
scalar, vector, matrix, tensor, etc.
Type can be specified by using i/f/d for integer/float32/double: X = T.ivector()
Shared variables
Variables with persistent value
Ideal for model parameters
w = theano.shared(np.random.rand(100).astype(’float32’))
o Initialized with numpy arrays (determines shape, size, type and starting values)
In the example above: a vector of 100 float32 values
o Optional name parameter
Can be used as a variable in symbolic expressions
Any operation with shared variables results in a symbolic experession/variable
W.get_value(), W.set_value(new_value)
o Getting/setting the value of a symbolic variable
o Use only when necessary
o borrow parameter
False: always copy the given array
True: reference the given array when possible
Computational graph
Operations with variablers produce other symbolic variables
Computational graph
o Node: a symbolic expression resulting in a variable
o Directed edges
In: from variables needed for computing the node
Out: to variables using this variable for computations
Functions
Parts of the computation graph compiled into a function
o C=A+B
o add = theano.function([A,B], C)
theano.function
o First parameter: list of inputs
Always a list, even if there are no inputs
All variables we need to provide value for to do the computations
Shared variables must not be in the list (they already have value)
o Second parameter (out): output or list of outputs if there are multiple
Optional
o update: dictionary of updates of the shared variables
Optional
Calling the function: res=add(1,1)
Gradient
T.grad(X, wrt=W)
o Computes the gradient of the scalar X with respect to paramaters W
o W must be a part of the computations leading to X
import theano
from theano import tensor as T
import numpy as np
from collections import OrderedDict
#from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
class Learner: