misc.pkl_utils
- Tools for serialization.¶
-
theano.misc.pkl_utils.
dump
(obj, file_handler, protocol=2, persistent_id=<class 'theano.misc.pkl_utils.PersistentSharedVariableID'>)¶ 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-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 anynumpy.ndarray
to a separate NPY file inside of the zip file.
Note
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', 'w') as f: ... dump((foo_1, foo_2, numpy.array(2)), f) >>> numpy.load('model.zip').keys() ['foo', 'foo_2', 'array_0', 'pkl'] >>> numpy.load('model.zip')['foo'] array(0) >>> with open('model.zip') as f: ... foo_1, foo_2, array = load(f) >>> array array(2)
-
theano.misc.pkl_utils.
load
(f, persistent_load=<class 'theano.misc.pkl_utils.PersistentNdarrayLoad'>)¶ 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 for unpickling. This must be compatible with the persisten_id function used when pickling.
-
class
theano.misc.pkl_utils.
StripPickler
(file, protocol=0, extra_tag_to_remove=None)¶ Subclass of Pickler that strips unnecessary attributes from Theano objects.
Example of use:
fn_args = dict(inputs=inputs, outputs=outputs, updates=updates) dest_pkl = 'my_test.pkl' f = open(dest_pkl, 'wb') strip_pickler = StripPickler(f, protocol=-1) strip_pickler.dump(fn_args) f.close()
-
class
theano.misc.pkl_utils.
CompatUnpickler
(file)¶ Allow to reload in python 3 some pickled numpy ndarray.
Examples
- with open(fname, ‘rb’) as fp:
- if PY3:
- u = CompatUnpickler(fp, encoding=”latin1”)
- else:
- u = CompatUnpickler(fp)
mat = u.load()
See also