chainer.serializers.DictionarySerializer¶
- 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
~DictionarySerializer.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.
Methods
- __call__(key, value)[source]¶
Serializes or deserializes a value by given name.
This operator saves or loads a value by given name.
If this is a serializer, then the value is simply saved at the key. Note that some type information might be missed depending on the implementation (and the target file format).
If this is a deserializer, then the value is loaded by the key. The deserialization differently works on scalars and arrays. For scalars, the
value
argument is used just for determining the type of restored value to be converted, and the converted value is returned. For arrays, the restored elements are directly copied into thevalue
argument. String values are treated like scalars.Note
Serializers and deserializers are required to correctly handle the
None
value. Whenvalue
isNone
, serializers save it in format-dependent ways, and deserializers just return the loaded value. When the savedNone
value is loaded by a deserializer, it should quietly return theNone
value without modifying thevalue
object.- Parameters
key (str) – Name of the serialization entry.
value (scalar, numpy.ndarray, cupy.ndarray, None, or str) – Object to be (de)serialized.
None
is only supported by deserializers.
- Returns
Serialized or deserialized value.
- __getitem__(key)[source]¶
Gets a child serializer.
This operator creates a _child_ serializer represented by the given key.
- Parameters
key (str) – Name of the child serializer.
- save(obj)[source]¶
Saves an object by this serializer.
This is equivalent to
obj.serialize(self)
.- Parameters
obj – Target object to be serialized.
- __eq__(value, /)¶
Return self==value.
- __ne__(value, /)¶
Return self!=value.
- __lt__(value, /)¶
Return self<value.
- __le__(value, /)¶
Return self<=value.
- __gt__(value, /)¶
Return self>value.
- __ge__(value, /)¶
Return self>=value.