-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_learners.py
More file actions
43 lines (30 loc) · 897 Bytes
/
test_learners.py
File metadata and controls
43 lines (30 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import numpy as np
import cPickle as cp
import theano
import theano.tensor as T
from matplotlib import pyplot as pp
import time
from learning_algs import SGD_Momentum_Learner
ndims=8000
init_x=np.random.randn(ndims)*2.0
c=np.random.randn(ndims)
M=np.random.randn(ndims, ndims)
shared_c=theano.shared(c.astype(np.float32))
shared_M=theano.shared(M.astype(np.float32))
shared_x=theano.shared(init_x.astype(np.float32))
Mx=T.dot(shared_M, shared_x)
loss=T.dot(shared_c.T, shared_x) + T.dot(Mx.T, Mx)
params=[shared_x]
nt=1000
lrate=[1e-6]
momentum_coeff=[0.95]
learner=SGD_Momentum_Learner(params, loss, momentum_coeff, lrate)
t0=time.time()
for i in range(nt):
loss_now=learner.get_current_loss()
learner.perform_learning_step()
#print loss_now
print 'Time: ', time.time()-t0
print 'Final loss: ', loss_now
losshist=np.asarray(learner.loss_history)
pp.plot(losshist[:,1]); pp.show()