Created
August 28, 2013 11:20
-
-
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.
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, 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