In [1]:
import os
os.environ["THEANO_FLAGS"] = "device=gpu,floatX=float32"
import theano
import theano.tensor as th


Using gpu device 0: GeForce GTX 580 (CNMeM is disabled, CuDNN not available)

In [2]:
import rnn.theano.lstm as lstm
reload(lstm)


Out[2]:
<module 'rnn.theano.lstm' from 'rnn/theano/lstm.pyc'>

In [ ]:


In [15]:
m = 10
b = 30
l = 100

n = 5

In [4]:
x = th.tensor3()
y0 = th.matrix()
c0 = th.matrix()
y = th.ivector()

In [5]:
layer = lstm.LSTM(m, n)

In [6]:
yh = th.nnet.softmax(layer.foldl(y0, c0, x)[0])
err = th.sum(th.nnet.categorical_crossentropy(yh, y))

In [7]:
lr = th.scalar()

glayer = th.grad(err, layer.weights)
update = [(layer.weights, layer.weights-lr*glayer)]

In [8]:
train = theano.function([y0, c0, x, y, lr], [err, yh], updates=update)
predict = theano.function([y0, c0, x], [yh])

In [9]:
import numpy as np

In [16]:
X = np.random.randn(l, b, m).astype(theano.config.floatX)
Y = np.random.randint(0, n, (b)).astype(np.int32)
y0_ = np.zeros((b,n), dtype=theano.config.floatX)
c0_ = np.zeros((b,n), dtype=theano.config.floatX)

In [17]:
predict(y0_, c0_, X)


Out[17]:
[array([[ 0.17801453,  0.22625414,  0.15585496,  0.25424045,  0.18563591],
        [ 0.185261  ,  0.18194644,  0.18853532,  0.27313384,  0.17112339],
        [ 0.1946509 ,  0.19229355,  0.17791733,  0.30004901,  0.13508922],
        [ 0.17894717,  0.15347485,  0.24696073,  0.28369337,  0.13692389],
        [ 0.15722714,  0.14678574,  0.28056678,  0.24534455,  0.17007579],
        [ 0.16964795,  0.19446643,  0.20784672,  0.28686753,  0.14117138],
        [ 0.18922624,  0.17783906,  0.20874043,  0.26831841,  0.15587588],
        [ 0.15668611,  0.21235274,  0.22828807,  0.2427893 ,  0.15988377],
        [ 0.19309187,  0.22749022,  0.20609356,  0.24919999,  0.12412445],
        [ 0.22340846,  0.16644832,  0.18779045,  0.2553643 ,  0.16698849],
        [ 0.15513174,  0.13535428,  0.2764253 ,  0.24119923,  0.19188948],
        [ 0.19817697,  0.16263026,  0.22173324,  0.27020767,  0.1472518 ],
        [ 0.1399356 ,  0.2049952 ,  0.19378562,  0.2615639 ,  0.19971965],
        [ 0.23154528,  0.15633705,  0.16334178,  0.27659845,  0.1721774 ],
        [ 0.19148476,  0.14480698,  0.20234974,  0.27604881,  0.18530975],
        [ 0.20843679,  0.14664908,  0.22666186,  0.25999689,  0.15825544],
        [ 0.19224828,  0.18683563,  0.17821126,  0.27534598,  0.16735885],
        [ 0.16371675,  0.17290021,  0.21690346,  0.29980329,  0.14667633],
        [ 0.19812414,  0.18637107,  0.23970519,  0.226588  ,  0.14921154],
        [ 0.22007923,  0.18216033,  0.19044976,  0.26155415,  0.14575656],
        [ 0.18203114,  0.15309109,  0.20254709,  0.29932168,  0.16300902],
        [ 0.23180506,  0.15235218,  0.19005816,  0.27092242,  0.15486221],
        [ 0.21471363,  0.13517074,  0.21009588,  0.26059136,  0.1794284 ],
        [ 0.19527583,  0.1628816 ,  0.20068347,  0.30209848,  0.13906066],
        [ 0.18708183,  0.12622648,  0.28791779,  0.26495939,  0.13381454],
        [ 0.22680911,  0.15667757,  0.20340282,  0.25432184,  0.15878867],
        [ 0.21839745,  0.14569473,  0.20607105,  0.28747129,  0.14236546],
        [ 0.17392595,  0.18601185,  0.19767053,  0.30049756,  0.14189407],
        [ 0.16098939,  0.27181339,  0.17180403,  0.22553228,  0.16986093],
        [ 0.20749767,  0.16969191,  0.19416402,  0.29329914,  0.13534722]], dtype=float32)]

In [18]:
print Y


[4 0 2 1 3 2 0 2 0 1 3 0 1 1 3 0 1 4 1 4 4 2 2 4 3 4 4 0 3 1]

In [42]:
iters = 1000
errs = np.zeros(iters)
for i in xrange(iters):
    err, yh = train(y0_, c0_, X, Y, 0.99999**i)
    errs[i] = err
    if i % 10 == 0:
        print '\r{:4>0}/{}, err={}'.format(i,iters, err),
print ''
print errs[0], errs[-1]
print yh
print Y


990/1000, err=24.8164558411 
25.7662658691 24.8113059998
[[ 0.12957083  0.13746351  0.1186552   0.12569484  0.48861557]
 [ 0.5474866   0.11246251  0.10019912  0.12539458  0.1144572 ]
 [ 0.19242294  0.16377382  0.30452904  0.18684906  0.15242516]
 [ 0.14766367  0.39366099  0.10266787  0.2258137   0.1301938 ]
 [ 0.13587016  0.12014776  0.1185615   0.48076296  0.14465767]
 [ 0.12042652  0.10412783  0.4530766   0.09781446  0.22455461]
 [ 0.48983553  0.13170192  0.11837449  0.12514646  0.13494164]
 [ 0.1880586   0.15625277  0.32888764  0.17520382  0.15159723]
 [ 0.50924623  0.13278016  0.10655316  0.12567276  0.12574768]
 [ 0.14450714  0.42698535  0.09308267  0.10990848  0.22551629]
 [ 0.15114671  0.15111208  0.15030198  0.39401531  0.15342396]
 [ 0.43786058  0.13492467  0.11165528  0.14558657  0.16997293]
 [ 0.14901631  0.40320969  0.09428317  0.10501931  0.24847157]
 [ 0.09655762  0.44952098  0.08505963  0.14166337  0.22719845]
 [ 0.12552445  0.11538129  0.12459189  0.51410496  0.12039746]
 [ 0.58003908  0.10193831  0.10125732  0.10711207  0.10965326]
 [ 0.11768177  0.4129214   0.10016314  0.10758384  0.26164985]
 [ 0.11354247  0.13858815  0.09146377  0.09809825  0.55830735]
 [ 0.18055901  0.43746448  0.11716236  0.12620303  0.13861112]
 [ 0.12247901  0.10552665  0.09517079  0.14479683  0.53202677]
 [ 0.1662024   0.13476075  0.10414568  0.10502124  0.48986986]
 [ 0.16969931  0.16580224  0.34089386  0.14310078  0.18050377]
 [ 0.20001459  0.20166634  0.31800076  0.1410412   0.13927712]
 [ 0.11827745  0.13211924  0.10292062  0.15556604  0.49111664]
 [ 0.18360581  0.12321974  0.11453134  0.45392945  0.12471365]
 [ 0.110064    0.10663614  0.10269879  0.11836657  0.56223452]
 [ 0.15261897  0.15053883  0.09507066  0.14706279  0.4547087 ]
 [ 0.52914119  0.12045871  0.11333455  0.11852827  0.11853725]
 [ 0.2189431   0.20496713  0.17072055  0.19994693  0.20542225]
 [ 0.14299501  0.42781773  0.12979244  0.16751756  0.13187735]]
[4 0 2 1 3 2 0 2 0 1 3 0 1 1 3 0 1 4 1 4 4 2 2 4 3 4 4 0 3 1]

In [43]:
print np.argmax(yh, axis=1)
sum(np.argmax(yh, axis=1)==Y)/float(len(Y))


[4 0 2 1 3 2 0 2 0 1 3 0 1 1 3 0 1 4 1 4 4 2 2 4 3 4 4 0 0 1]
Out[43]:
0.96666666666666667

In [ ]: