In [13]:
import numpy as np
import theano
from theano import tensor as T
In [2]:
a = T.scalar()
b = T.scalar()
y = a * b
multiply = theano.function(inputs=[a, b], outputs=y)
print multiply(1, 2)
print multiply(3, 3)
In [3]:
trX = np.linspace(-1, 1, 10)
print trX.shape
trX
Out[3]:
In [4]:
trY = 2 * trX + np.random.randn(*trX.shape) * 0.33
print trY.shape
trY
Out[4]:
In [5]:
X = T.scalar()
Y = T.scalar()
def model(X, w):
return X * w
In [6]:
w = theano.shared(np.asarray(0.0, dtype=theano.config.floatX))
w
Out[6]:
In [7]:
y = model(X, w)
y
Out[7]:
In [8]:
cost = T.mean(T.sqr(y - Y))
cost
Out[8]:
In [9]:
gradient = T.grad(cost=cost, wrt=w)
gradient
Out[9]:
In [10]:
updates = [[w, w - gradient * 0.01]]
updates
Out[10]:
In [11]:
train = theano.function(inputs=[X, Y], outputs=cost, updates=updates, allow_input_downcast=True)
train
Out[11]:
In [12]:
for i in range(100):
for x, y in zip(trX, trY):
train(x, y)
In [14]:
def floatX(X):
return np.asarray(X, dtype=theano.config.floatX)
def init_weights(shape):
return theano.shared(floatX(np.random.randn(*shape) * 0.01))
def model():
return T.nnet.softmax(T.dot(X, w))
In [ ]:
trX, teX, trY, teY = mnist(onehot=True)
In [15]:
X = T.fmatrix()
Y = T.fmatrix()
In [ ]:
w = init_weights((784, 10))
py_x = model(X, w)
y_pred = T.argmax(py_x, axis=1)
cost = T.mean(T.nnet.categorical_crossentropy(py_x, Y))
gradient = T.grad(cost=cost, wrt=w)
update = [[w, w - gradient * 0.05]]
train = theano.function(inputs=[X, Y], outputs=cost, updates=updates, allow_input_downcast=True)
predict = theano.function(inputs=[X], outputs=y_pred, allow_input_downcast=True)
for i in range(100):
for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):
cost = train(trX[start:end], trY[start:end])
print i, np.mean(np.argmax(teY, axis=1) == predict(teX))
In [ ]: