misc.pkl_utils - Tools for serialization.

  • theano.misc.pklutils.dump(_obj, file_handler, protocol=2, persistent_id=)[source]
  • Pickles an object to a zip file using external persistence.

Parameters:

  • 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-inpickle, the default is set to 2 instead of 0 for Python 2. ThePython 3 default (level 3) is maintained.
  • persistent_id (callable) – The callable that persists certain objects in theobject hierarchy to separate files inside of the zip file. For example,PersistentNdarrayID saves any numpy.ndarray to aseparate NPY file inside of the zip file.

New in version 0.8.

Note

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

  1. >>> import theano
  2. >>> foo_1 = theano.shared(0, name='foo')
  3. >>> foo_2 = theano.shared(1, name='foo')
  4. >>> with open('model.zip', 'wb') as f:
  5. ... dump((foo_1, foo_2, numpy.array(2)), f)
  6. >>> numpy.load('model.zip').keys()
  7. ['foo', 'foo_2', 'array_0', 'pkl']
  8. >>> numpy.load('model.zip')['foo']
  9. array(0)
  10. >>> with open('model.zip', 'rb') as f:
  11. ... foo_1, foo_2, array = load(f)
  12. >>> array
  13. array(2)

  • theano.misc.pklutils.load(_f, persistent_load=)[source]
  • Load a file that was dumped to a zip file.

Parameters:

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

New in version 0.8.

  • class theano.misc.pklutils.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:

  1. fn_args = dict(inputs=inputs,
  2. outputs=outputs,
  3. updates=updates)
  4. dest_pkl = 'my_test.pkl'
  5. f = open(dest_pkl, 'wb')
  6. strip_pickler = StripPickler(f, protocol=-1)
  7. strip_pickler.dump(fn_args)
  8. f.close()
  • class theano.misc.pklutils.CompatUnpickler(_file)[source]
  • Allow to reload in python 3 some pickled numpy ndarray.

New in version 0.8.

Examples

  1. with open(fname, 'rb') as fp:
  2. if PY3:
  3. u = CompatUnpickler(fp, encoding="latin1")
  4. else:
  5. u = CompatUnpickler(fp)
  6. mat = u.load()

See also

Loading and Saving