Mercurial > pylearn
annotate pylearn/shared/layers/tests/test_sigmoidal_layer.py @ 1447:fbe470217937
Use .get_value() and .set_value() of shared instead of the .value property
author | Pascal Lamblin <lamblinp@iro.umontreal.ca> |
---|---|
date | Wed, 16 Mar 2011 20:20:02 -0400 |
parents | 912be602c3ac |
children |
rev | line source |
---|---|
834 | 1 import numpy |
2 from pylearn.shared.layers import SigmoidalLayer, LogisticRegression | |
3 from theano import tensor | |
893
912be602c3ac
fix import for the move of shared var outside of the sandbox.
Frederic Bastien <nouiz@nouiz.org>
parents:
834
diff
changeset
|
4 from theano.compile import shared, pfunc |
834 | 5 |
6 def test_w_random(dtype='float64'): | |
7 if dtype == 'float64': | |
8 x = tensor.dmatrix() | |
9 else: | |
10 x = tensor.fmatrix() | |
11 y = tensor.lvector() | |
12 rng = numpy.random.RandomState(23455) | |
13 | |
14 bsize=10 | |
15 n_in = 10 | |
16 n_hid = 12 | |
17 n_out = 2 | |
18 n_iter=100 | |
19 | |
20 layer = SigmoidalLayer.new(rng, x, n_in, n_hid, squash_fn='tanh', dtype=dtype) | |
21 out = LogisticRegression.new(layer.output, n_hid, 2) | |
22 cost = out.nll(y).sum() | |
23 params = out.params+layer.params | |
24 updates = [(p, p - numpy.asarray(0.01, dtype=dtype)*gp) for p,gp in zip(params, tensor.grad(cost, params)) ] | |
25 f = pfunc([x, y], cost, updates=updates) | |
26 | |
1447
fbe470217937
Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
893
diff
changeset
|
27 w0 = layer.w.get_value(borrow=False) |
fbe470217937
Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
893
diff
changeset
|
28 b0 = layer.b.get_value(borrow=False) |
834 | 29 |
30 xval = numpy.asarray(rng.rand(bsize, n_in), dtype=dtype) | |
31 yval = numpy.asarray(rng.randint(0,2,bsize), dtype='int64') | |
32 f0 = f(xval, yval) | |
33 for i in xrange(n_iter): | |
34 fN = f(xval, yval) | |
35 print i, 'rval', fN | |
36 | |
37 assert f0 > 6 | |
1447
fbe470217937
Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
893
diff
changeset
|
38 assert fN < 2 |
834 | 39 |
1447
fbe470217937
Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
893
diff
changeset
|
40 assert numpy.all(w0 != layer.w.get_value(borrow=True)) |
fbe470217937
Use .get_value() and .set_value() of shared instead of the .value property
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
893
diff
changeset
|
41 assert numpy.all(b0 != layer.b.get_value(borrow=True)) |