Mercurial > pylearn
annotate examples/theano_update.py @ 451:d99fefbc9324
Added a KL-divergence.
author | Joseph Turian <turian@gmail.com> |
---|---|
date | Thu, 04 Sep 2008 14:46:30 -0400 |
parents | 200a5b0e24ea |
children |
rev | line source |
---|---|
433
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
1 import theano |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
2 from theano import tensor |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
3 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
4 import numpy |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
5 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
6 # Two scalar symbolic variables |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
7 a = tensor.scalar() |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
8 b = tensor.scalar() |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
9 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
10 # Definition of output symbolic variable |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
11 c = a * b |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
12 # Definition of the function computing it |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
13 fprop = theano.function([a,b], [c]) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
14 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
15 # Initialize numerical variables |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
16 a_val = numpy.array(12.) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
17 b_val = numpy.array(2.) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
18 print 'a_val =', a_val |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
19 print 'b_val =', b_val |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
20 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
21 # Numerical value of output is returned by the call to "fprop" |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
22 c_val = fprop(a_val, b_val) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
23 print 'c_val =', c_val |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
24 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
25 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
26 # Definition of simple update (increment by one) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
27 new_b = b + 1 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
28 update = theano.function([b], [new_b]) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
29 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
30 # New numerical value of b is returned by the call to "update" |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
31 b_val = update(b_val) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
32 print 'new b_val =', b_val |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
33 # We can use the new value in "fprop" |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
34 c_val = fprop(a_val, b_val) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
35 print 'c_val =', c_val |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
36 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
37 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
38 # Definition of in-place update (increment by one) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
39 re_new_b = tensor.add_inplace(b, 1.) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
40 re_update = theano.function([b], [re_new_b]) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
41 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
42 # "re_update" can be used the same way as "update" |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
43 b_val = re_update(b_val) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
44 print 'new b_val =', b_val |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
45 # We can use the new value in "fprop" |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
46 c_val = fprop(a_val, b_val) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
47 print 'c_val =', c_val |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
48 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
49 # It is not necessary to keep the return value when the update is done in place |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
50 re_update(b_val) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
51 print 'new b_val =', b_val |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
52 c_val = fprop(a_val, b_val) |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
53 print 'c_val =', c_val |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
54 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
55 |
200a5b0e24ea
Example showing parameter updates.
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
diff
changeset
|
56 |