Skip to content

Instantly share code, notes, and snippets.

@bayerj
Created August 28, 2013 11:20
Show Gist options
  • Select an option

  • Save bayerj/6364981 to your computer and use it in GitHub Desktop.

Select an option

Save bayerj/6364981 to your computer and use it in GitHub Desktop.
Tell whether an expression is deterministic, ie it does not use a random number generator.
import theano, theano.tensor as T
from theano.tensor.shared_randomstreams import RandomStreams
def theano_expr_bfs(expr):
stack = [expr]
while True:
if not stack:
break
expr = stack.pop()
stack += expr.owner.inputs if hasattr(expr.owner, 'inputs') else []
yield expr
def tell_deterministic(expr):
return all(not hasattr(i, 'rng') for i in theano_expr_bfs(expr))
if __name__ == '__main__':
print tell_deterministic(T.matrix())
rng = RandomStreams()
inpt = T.vector()
samples = rng.normal(size=inpt.shape)
expr = inpt + samples
print tell_deterministic(expr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment