Skip to content

Instantly share code, notes, and snippets.

@mgermain
Last active August 29, 2015 14:08
Show Gist options
  • Save mgermain/2ceba111d93cdb717b28 to your computer and use it in GitHub Desktop.
Save mgermain/2ceba111d93cdb717b28 to your computer and use it in GitHub Desktop.
Theano MRG_RandomStreams backup test
import numpy as np
from theano.sandbox.rng_mrg import MRG_RandomStreams
rng = MRG_RandomStreams(seed=1234)
mult = rng.multinomial(pvals=[[.3,.3,.3]])
# Backup random state
rst = rng.rstate.copy()
rst_upd = [s[0].get_value() for s in rng.state_updates]
# Use the random
print "Using random"
for i in range(5):
print mult.eval()
# Reset random state
rng.rstate = rst
for i in range(len(rng.state_updates)):
rng.state_updates[i][0].set_value(rst_upd[i])
# Re-Use the random to make sure it was reseted
print "Re-Using random"
for i in range(5):
print mult.eval()
# Find Nan
# len(np.where(np.isnan(rst_upd) == True))
@mgermain
Copy link
Author

mgermain commented Nov 6, 2014

Output:
Using random
[[0 1 0]]
[[0 1 0]]
[[1 0 0]]
[[0 1 0]]
[[0 1 0]]
Re-Using random
[[0 1 0]]
[[0 1 0]]
[[1 0 0]]
[[0 1 0]]
[[0 1 0]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment