Created
October 22, 2014 20:31
-
-
Save rizar/e661d3a83e42587fe38a to your computer and use it in GitHub Desktop.
Theano Scan Updates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import theano | |
from theano.tensor.shared_randomstreams import RandomStreams | |
r = RandomStreams(1) | |
y, u = theano.scan(lambda : r.binomial(size=(2, 2)), n_steps=3) | |
f = theano.function([], [y]) | |
# It still gives me three different sequence of random matrices! | |
print f() | |
print f() | |
print f() |
Here's an example which shows that passing updates gives you the correct answer, while not passing it gives you the wrong one:
import theano
from theano.tensor.shared_randomstreams import RandomStreams
r = RandomStreams(1)
y, u = theano.scan(lambda : r.normal(), n_steps=3)
f = theano.function([], y)
# This is what you get when don't pass updates
f()
print f()
r = RandomStreams(1)
y, u = theano.scan(lambda : r.normal(), n_steps=3)
f = theano.function([], y, updates=u)
# And this is what you get when you do
f()
print f()
# This is what it should be
r = RandomStreams(1)
f = theano.function([], r.normal(size=(6,))[3:])
print f()
[ 0.46301228 -1.47958577 -1.48030508]
[-1.48030508 -0.47802526 0.38352579]
[-1.48030508 -0.47802526 0.38352579]
Sorry, have not seen this response before. Looks like it actually gives us "better" random :)
We can attach updates
to the tag of the Apply node and find it then when exploring the computation graph.
And by the way: in a more complex situation I could not even call a compiled without updates function...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
But, if you do
f = theano.function([], [y], updates=u)
the results are different... So I guess that even if you don't passu
,scan
changes the state of the RNG somehow, but incorrectly maybe?