Created
May 21, 2012 18:57
-
-
Save mrocklin/2763972 to your computer and use it in GitHub Desktop.
An example that shows how to pickle a Theano Env object. All callables must be removed first. We then demonstrate that this stripped down Env may still be compiled into a callable function.
This file contains 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 | |
import theano.tensor as T | |
import pickle | |
import numpy as np | |
# Create a simple example | |
x = T.matrix('x') | |
y = x + x | |
env = theano.Env([x], [y]) | |
# Remove callables from the env | |
env.__dict__ = dict([ (key, val) for key, val in env.__dict__.items() | |
if not callable(val)]) | |
# Pickle and reload | |
s = pickle.dumps(env) | |
env2 = pickle.loads(s) | |
# Optimize and compile the Env | |
mode = theano.compile.mode.get_default_mode() | |
mode.optimizer.optimize(env2) | |
link = mode.linker.accept(env2) | |
fn = link.make_function() | |
# Test | |
assert (fn(np.ones((5,5), dtype = np.float32)) == np.ones((5,5)) * 2).all() | |
~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment