misc.pkl_utils - Tools for serialization.

misc.pkl_utils - Tools for serialization.#

aesara.misc.pkl_utils.dump(obj, file_handler, protocol=4, persistent_id=<class 'aesara.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 aesara
>>> foo_1 = aesara.shared(0, name='foo')
>>> foo_2 = aesara.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
aesara.misc.pkl_utils.load(f, persistent_load=<class 'aesara.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 aesara.misc.pkl_utils.StripPickler(file, protocol=0, extra_tag_to_remove=None)[source]#

Subclass of Pickler that strips unnecessary attributes from Aesara objects.


fn_args = dict(inputs=inputs,

outputs=outputs, updates=updates)

dest_pkl = ‘my_test.pkl’ with open(dest_pkl, ‘wb’) as f:

strip_pickler = StripPickler(f, protocol=-1) strip_pickler.dump(fn_args)