misc.pkl_utils - Tools for serialization.

theano.misc.pkl_utils.dump(obj, file_handler, protocol=2, persistent_id=<class 'theano.misc.pkl_utils.PersistentSharedVariableID'>)[source]

Pickles an object to a zip file using external persistence.

  • obj (object) – The object to pickle.
  • file_handler (file) – The file handle to save the object to.
  • protocol (int, optional) – The pickling protocol to use. Unlike Python’s built-in pickle, the default is set to 2 instead of 0 for Python 2. The Python 3 default (level 3) is maintained.
  • persistent_id (callable) – The callable that persists certain objects in the object hierarchy to separate files inside of the zip file. For example, PersistentNdarrayID saves any numpy.ndarray to a separate NPY file inside of the zip file.

New in version 0.8.


The final file is simply a zipped file containing at least one file, pkl, which contains the pickled object. It can contain any other number of external objects. Note that the zip files are compatible with NumPy’s numpy.load() function.

>>> import theano
>>> foo_1 = theano.shared(0, name='foo')
>>> foo_2 = theano.shared(1, name='foo')
>>> with open('model.zip', 'wb') as f:
...     dump((foo_1, foo_2, np.array(2)), f)
>>> np.load('model.zip').keys()
['foo', 'foo_2', 'array_0', 'pkl']
>>> np.load('model.zip')['foo']
>>> with open('model.zip', 'rb') as f:
...     foo_1, foo_2, array = load(f)
>>> array
theano.misc.pkl_utils.load(f, persistent_load=<class 'theano.misc.pkl_utils.PersistentNdarrayLoad'>)[source]

Load a file that was dumped to a zip file.

  • f (file) – The file handle to the zip file to load the object from.
  • persistent_load (callable, optional) – The persistent loading function to use for unpickling. This must be compatible with the persisten_id function used when pickling.

New in version 0.8.

class theano.misc.pkl_utils.StripPickler(file, protocol=0, extra_tag_to_remove=None)[source]

Subclass of Pickler that strips unnecessary attributes from Theano objects.

New in version 0.8.

Example of use:

fn_args = dict(inputs=inputs,
dest_pkl = 'my_test.pkl'
f = open(dest_pkl, 'wb')
strip_pickler = StripPickler(f, protocol=-1)
class theano.misc.pkl_utils.CompatUnpickler(file)[source]

Allow to reload in python 3 some pickled numpy ndarray.

New in version 0.8.


with open(fname, 'rb') as fp:
    if PY3:
        u = CompatUnpickler(fp, encoding="latin1")
        u = CompatUnpickler(fp)
    mat = u.load()