comparison examples/theano_update.py @ 433:200a5b0e24ea

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