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 anynumpy.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.
- >>> 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']
- array(0)
- >>> with open('model.zip', 'rb') as f:
- ... foo_1, foo_2, array = load(f)
- >>> array
- 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:
- 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.pklutils.
CompatUnpickler
(_file)[source] - Allow to reload in python 3 some pickled numpy ndarray.
New in version 0.8.
Examples
- with open(fname, 'rb') as fp:
- if PY3:
- u = CompatUnpickler(fp, encoding="latin1")
- else:
- u = CompatUnpickler(fp)
- mat = u.load()
See also
当前内容版权归 deeplearning 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 deeplearning .