# 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() and numpy.savez_compressed(). If you want to use this serializer directly, you have to manually send a resulting dictionary to one of these functions.

Parameters: target (dict) – The dictionary that this serializer saves the objects to. If target is None, then a new dictionary is created. path (str) – The base path in the hierarchy that this serializer indicates. target (dict) – The target dictionary. Once the serialization completes, this dictionary can be fed into numpy.savez() or numpy.savez_compressed() to serialize it in the NPZ format.
class chainer.serializers.NpzDeserializer(npz, path='')[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.
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: filename (str) – Target file name. obj – Object to be serialized. It must support serialization protocol. compression (bool) – If True, compression in the resulting zip file is enabled.
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)[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.
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 HDF5Serializer directly by passing appropriate h5py.Group objects.

Parameters: filename (str) – Target file name. obj – Object to be serialized. It must support serialization protocol. compression (int) – Gzip compression level.
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 HDF5Deserializer directly by passing appropriate h5py.Group objects.

Parameters: filename (str) – Name of the file to be loaded. obj – Object to be deserialized. It must support serialization protocol.