Created
April 2, 2014 13:46
-
-
Save sisp/9934465 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import theano | |
import theano.tensor as T | |
import numpy as np | |
def step(b, a, x, t, W): | |
y = theano.dot(x[a:b], W) | |
y = theano.printing.Print()(y) | |
error = 0.5 * T.sqr(t[a:b] - y).sum() | |
return b, error | |
if __name__ == '__main__': | |
floatX = theano.config.floatX | |
# input matrix (time steps * number of examples, features) | |
x = T.matrix(name='x') | |
# target matrix (time steps * number of examples, features) | |
t = T.matrix(name='t') | |
# number of time steps for each example | |
i = T.vector(name='i', dtype='int64') | |
W = theano.shared(np.random.uniform(-0.01, 0.01, size=(2, 2)).astype(floatX), name='W') | |
i0 = T.constant(0, dtype='int64') | |
(_, error), _ = theano.scan(step, | |
sequences=[T.cumsum(i)], | |
non_sequences=[x, t, W], | |
outputs_info=[i0, None]) | |
dW = theano.grad(error.mean(), W) | |
f = theano.function([x, t, i], [error, dW]) | |
print f(np.random.uniform(size=(7, 2)).astype(floatX), | |
np.random.uniform(size=(7, 2)).astype(floatX), | |
np.asarray([2,3,2], dtype='int64')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment