シリアライザー

NumPy NPZ形式のシリアル化

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='')[ソース]

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.

注釈

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.

パラメータ:
  • 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='')[ソース]

Deserializer for NPZ format.

This is the standard deserializer in Chainer. This deserializer can be used to read an object serialized by save_npz().

パラメータ:
  • npznpz file object.
  • path – The base path that the deserialization starts from.
chainer.serializers.save_npz(filename, obj, compression=True)[ソース]

Saves an object to the file in NPZ format.

This is a short-cut function to save only one object into an NPZ file.

パラメータ:
  • 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)[ソース]

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.

パラメータ:
  • filename (str) – Name of the file to be loaded.
  • obj – Object to be deserialized. It must support serialization protocol.

HDF5形式のシリアル化

class chainer.serializers.HDF5Serializer(group, compression=4)[ソース]

Serializer for HDF5 format.

This is the standard serializer in Chainer. The chain hierarchy is simply mapped to HDF5 hierarchical groups.

パラメータ:
  • group (h5py.Group) – The group that this serializer represents.
  • compression (int) – Gzip compression level.
class chainer.serializers.HDF5Deserializer(group)[ソース]

Deserializer for HDF5 format.

This is the standard deserializer in Chainer. This deserializer can be used to read an object serialized by HDF5Serializer.

パラメータ:group (h5py.Group) – The group that the deserialization starts from.
chainer.serializers.save_hdf5(filename, obj, compression=4)[ソース]

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.

パラメータ:
  • 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)[ソース]

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.

パラメータ:
  • filename (str) – Name of the file to be loaded.
  • obj – Object to be deserialized. It must support serialization protocol.