Serializers¶
Serialization in NumPy NPZ format¶
NumPy serializers can be used in arbitrary environments that Chainer runs with.
It consists of asymmetric serializer/deserializer due to the fact that numpy.savez() does not support online serialization.
Therefore, serialization requires two-step manipulation: first packing the objects into a flat dictionary, and then serializing it into npz format.
-
class
chainer.serializers.DictionarySerializer(target=None, path='')[source]¶ Serializer for dictionary.
This is the standard serializer in Chainer. The hierarchy of objects are simply mapped to a flat dictionary with keys representing the paths to objects in the hierarchy.
Note
Despite of its name, this serializer DOES NOT serialize the object into external files. It just build a flat dictionary of arrays that can be fed into
numpy.savez()andnumpy.savez_compressed(). If you want to use this serializer directly, you have to manually send a resulting dictionary to one of these functions.Parameters: Variables: target (dict) – The target dictionary. Once the serialization completes, this dictionary can be fed into
numpy.savez()ornumpy.savez_compressed()to serialize it in the NPZ format.
-
class
chainer.serializers.NpzDeserializer(npz, path='', strict=True)[source]¶ Deserializer for NPZ format.
This is the standard deserializer in Chainer. This deserializer can be used to read an object serialized by
save_npz().Parameters: - npz – npz file object.
- path – The base path that the deserialization starts from.
- strict (bool) – If
True, the deserializer raises an error when an expected value is not found in the given NPZ file. Otherwise, it ignores the value and skip deserialization.
-
chainer.serializers.save_npz(filename, obj, compression=True)[source]¶ Saves an object to the file in NPZ format.
This is a short-cut function to save only one object into an NPZ file.
Parameters:
-
chainer.serializers.load_npz(filename, obj)[source]¶ Loads an object from the file in NPZ format.
This is a short-cut function to load from an .npz file that contains only one object.
Parameters: - filename (str) – Name of the file to be loaded.
- obj – Object to be deserialized. It must support serialization protocol.
Serialization in HDF5 format¶
-
class
chainer.serializers.HDF5Serializer(group, compression=4)[source]¶ Serializer for HDF5 format.
This is the standard serializer in Chainer. The chain hierarchy is simply mapped to HDF5 hierarchical groups.
Parameters: - group (h5py.Group) – The group that this serializer represents.
- compression (int) – Gzip compression level.
-
class
chainer.serializers.HDF5Deserializer(group, strict=True)[source]¶ Deserializer for HDF5 format.
This is the standard deserializer in Chainer. This deserializer can be used to read an object serialized by
HDF5Serializer.Parameters: - group (h5py.Group) – The group that the deserialization starts from.
- strict (bool) – If
True, the deserializer raises an error when an expected value is not found in the given HDF5 file. Otherwise, it ignores the value and skip deserialization.
-
chainer.serializers.save_hdf5(filename, obj, compression=4)[source]¶ Saves an object to the file in HDF5 format.
This is a short-cut function to save only one object into an HDF5 file. If you want to save multiple objects to one HDF5 file, use
HDF5Serializerdirectly by passing appropriateh5py.Groupobjects.Parameters:
-
chainer.serializers.load_hdf5(filename, obj)[source]¶ Loads an object from the file in HDF5 format.
This is a short-cut function to load from an HDF5 file that contains only one object. If you want to load multiple objects from one HDF5 file, use
HDF5Deserializerdirectly by passing appropriateh5py.Groupobjects.Parameters: - filename (str) – Name of the file to be loaded.
- obj – Object to be deserialized. It must support serialization protocol.