Skip to content

Instantly share code, notes, and snippets.

@mrocklin
Created May 21, 2012 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrocklin/2763972 to your computer and use it in GitHub Desktop.
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.
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