imports
import theano
from theano import tensor as T
import numpy as np
generate data with noise
trX = np.linspace(-1, 1, 101)
trY = 2 * trX + np.random.randn(*trX.shape) * 0.33
symbolic vriable initalization
X = T.scalar()
Y = T.scalar()
our model
def model(X, w):
return X * w
model parameter initalization. hyper variables, with real value
w = theano.shared(np.asarray(0., dtype=theano.config.floatX))
y = model(X, w)
metric to be optimized by model
cost = T.mean(T.sqr(y - Y))
learning signal for parameters. Computes the gradient symbolically
gradient = T.grad(cost=cost, wrt=w)
how to update in each step. 0.01 is the learning rate
updates = [[w, w - gradient * 0.01]]
compile to a python function. allow_input_downcast=True
is set to ignore typing issue with Theano on different platforms (GPU handle only 32bit)
train = theano.function(inputs=[X, Y], outputs=cost, updates=updates, allow_input_downcast=True)
iterate 100 times over the entire data (epoch.) In each epoch iterate over all the data samples
for i in range(100):
for x, y in zip(trX, trY):
train(x, y)
we should get something close to the true weight of the data: 2
w.get_value()
array(1.9382810308787022)