Skip to content

Instantly share code, notes, and snippets.

@rizar
Created October 22, 2014 20:31
Show Gist options
  • Select an option

  • Save rizar/e661d3a83e42587fe38a to your computer and use it in GitHub Desktop.

Select an option

Save rizar/e661d3a83e42587fe38a to your computer and use it in GitHub Desktop.
Theano Scan Updates
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()
@bartvm
Copy link
Copy Markdown

bartvm commented Oct 22, 2014

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]

@rizar
Copy link
Copy Markdown
Author

rizar commented Nov 12, 2014

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.

@rizar
Copy link
Copy Markdown
Author

rizar commented Nov 12, 2014

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