In [13]:
import numpy as np
import theano
from theano import tensor as T


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-13-81d10aabcb07> in <module>()
      1 import numpy as np
----> 2 from load import mnist
      3 import theano
      4 from theano import tensor as T

ImportError: No module named load

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)


WARNING (theano.gof.compilelock): Overriding existing lock by dead process '29013' (I am process '33194')
WARNING:theano.gof.compilelock:Overriding existing lock by dead process '29013' (I am process '33194')
2.0
9.0


In [3]:
trX = np.linspace(-1, 1, 10)
print trX.shape
trX


(10,)
Out[3]:
array([-1.        , -0.77777778, -0.55555556, -0.33333333, -0.11111111,
        0.11111111,  0.33333333,  0.55555556,  0.77777778,  1.        ])

In [4]:
trY = 2 * trX + np.random.randn(*trX.shape) * 0.33
print trY.shape
trY


(10,)
Out[4]:
array([-1.56909406, -1.73819047, -1.606033  , -0.36409657, -0.127681  ,
        0.31353176,  0.85916629,  1.02792328,  1.07347518,  2.29289803])

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]:
<TensorType(float64, scalar)>

In [7]:
y = model(X, w)
y


Out[7]:
Elemwise{mul,no_inplace}.0

In [8]:
cost = T.mean(T.sqr(y - Y))
cost


Out[8]:
Sum{acc_dtype=float64}.0

In [9]:
gradient = T.grad(cost=cost, wrt=w)
gradient


Out[9]:
Elemwise{mul}.0

In [10]:
updates = [[w, w - gradient * 0.01]]
updates


Out[10]:
[[<TensorType(float64, scalar)>, Elemwise{sub,no_inplace}.0]]

In [11]:
train = theano.function(inputs=[X, Y], outputs=cost, updates=updates, allow_input_downcast=True)
train


Out[11]:
<theano.compile.function_module.Function at 0x1137fbf10>

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 [ ]: