# HG changeset patch # User Pascal Lamblin # Date 1217539535 14400 # Node ID 200a5b0e24ea93527b433e65c6fdf719487c66b9 # Parent 8e4d2ebd816ad9371dcee71d3244734251f9d730 Example showing parameter updates. diff -r 8e4d2ebd816a -r 200a5b0e24ea examples/theano_update.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/theano_update.py Thu Jul 31 17:25:35 2008 -0400 @@ -0,0 +1,56 @@ +import theano +from theano import tensor + +import numpy + +# Two scalar symbolic variables +a = tensor.scalar() +b = tensor.scalar() + +# Definition of output symbolic variable +c = a * b +# Definition of the function computing it +fprop = theano.function([a,b], [c]) + +# Initialize numerical variables +a_val = numpy.array(12.) +b_val = numpy.array(2.) +print 'a_val =', a_val +print 'b_val =', b_val + +# Numerical value of output is returned by the call to "fprop" +c_val = fprop(a_val, b_val) +print 'c_val =', c_val + + +# Definition of simple update (increment by one) +new_b = b + 1 +update = theano.function([b], [new_b]) + +# New numerical value of b is returned by the call to "update" +b_val = update(b_val) +print 'new b_val =', b_val +# We can use the new value in "fprop" +c_val = fprop(a_val, b_val) +print 'c_val =', c_val + + +# Definition of in-place update (increment by one) +re_new_b = tensor.add_inplace(b, 1.) +re_update = theano.function([b], [re_new_b]) + +# "re_update" can be used the same way as "update" +b_val = re_update(b_val) +print 'new b_val =', b_val +# We can use the new value in "fprop" +c_val = fprop(a_val, b_val) +print 'c_val =', c_val + +# It is not necessary to keep the return value when the update is done in place +re_update(b_val) +print 'new b_val =', b_val +c_val = fprop(a_val, b_val) +print 'c_val =', c_val + + +