We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Partial deserialization of links
We sometimes want to deserialize a link only partially, i.e. we want to load a smaller set of parameters to a larger link. It happens when we do some surgery to neural nets (e.g. fine-tuning), in which case the set of parameters in the source network does not match to the set of parameters in the target network.

One possible design is to introduce a `strict` option to the deserializer, and if that flag is False, any mismatch of the sets of parameters is ignored. I think it is safer to turn on this flag by default (I mean, we should not change the default behavior).

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `chainer/serializers/npz.py`
Content:
```
1 import numpy
2 
3 from chainer import cuda
4 from chainer import serializer
5 
6 
7 class DictionarySerializer(serializer.Serializer):
8 
9     """Serializer for dictionary.
10 
11     This is the standard serializer in Chainer. The hierarchy of objects are
12     simply mapped to a flat dictionary with keys representing the paths to
13     objects in the hierarchy.
14 
15     .. note::
16        Despite of its name, this serializer DOES NOT serialize the
17        object into external files. It just build a flat dictionary of arrays
18        that can be fed into :func:`numpy.savez` and
19        :func:`numpy.savez_compressed`. If you want to use this serializer
20        directly, you have to manually send a resulting dictionary to one of
21        these functions.
22 
23     Args:
24         target (dict): The dictionary that this serializer saves the objects
25             to. If target is None, then a new dictionary is created.
26         path (str): The base path in the hierarchy that this serializer
27             indicates.
28 
29     Attributes:
30         target (dict): The target dictionary. Once the serialization completes,
31             this dictionary can be fed into :func:`numpy.savez` or
32             :func:`numpy.savez_compressed` to serialize it in the NPZ format.
33 
34     """
35 
36     def __init__(self, target=None, path=''):
37         self.target = {} if target is None else target
38         self.path = path
39 
40     def __getitem__(self, key):
41         key = key.strip('/')
42         return DictionarySerializer(self.target, self.path + key + '/')
43 
44     def __call__(self, key, value):
45         key = key.lstrip('/')
46         ret = value
47         if isinstance(value, cuda.ndarray):
48             value = value.get()
49         arr = numpy.asarray(value)
50         self.target[self.path + key] = arr
51         return ret
52 
53 
54 def save_npz(filename, obj, compression=True):
55     """Saves an object to the file in NPZ format.
56 
57     This is a short-cut function to save only one object into an NPZ file.
58 
59     Args:
60         filename (str): Target file name.
61         obj: Object to be serialized. It must support serialization protocol.
62         compression (bool): If ``True``, compression in the resulting zip file
63             is enabled.
64 
65     """
66     s = DictionarySerializer()
67     s.save(obj)
68     with open(filename, 'wb') as f:
69         if compression:
70             numpy.savez_compressed(f, **s.target)
71         else:
72             numpy.savez(f, **s.target)
73 
74 
75 class NpzDeserializer(serializer.Deserializer):
76 
77     """Deserializer for NPZ format.
78 
79     This is the standard deserializer in Chainer. This deserializer can be used
80     to read an object serialized by :func:`save_npz`.
81 
82     Args:
83         npz: `npz` file object.
84         path: The base path that the deserialization starts from.
85 
86     """
87 
88     def __init__(self, npz, path=''):
89         self.npz = npz
90         self.path = path
91 
92     def __getitem__(self, key):
93         key = key.strip('/')
94         return NpzDeserializer(self.npz, self.path + key + '/')
95 
96     def __call__(self, key, value):
97         key = key.lstrip('/')
98         dataset = self.npz[self.path + key]
99         if value is None:
100             return dataset
101         elif isinstance(value, numpy.ndarray):
102             numpy.copyto(value, dataset)
103         elif isinstance(value, cuda.ndarray):
104             value.set(numpy.asarray(dataset))
105         else:
106             value = type(value)(numpy.asarray(dataset))
107         return value
108 
109 
110 def load_npz(filename, obj):
111     """Loads an object from the file in NPZ format.
112 
113     This is a short-cut function to load from an `.npz` file that contains only
114     one object.
115 
116     Args:
117         filename (str): Name of the file to be loaded.
118         obj: Object to be deserialized. It must support serialization protocol.
119 
120     """
121     with numpy.load(filename) as f:
122         d = NpzDeserializer(f)
123         d.load(obj)
124
```

Path: `chainer/serializers/hdf5.py`
Content:
```
1 import numpy
2 
3 from chainer import cuda
4 from chainer import serializer
5 
6 
7 try:
8     import h5py
9     _available = True
10 except ImportError:
11     _available = False
12 
13 
14 def _check_available():
15     if not _available:
16         msg = '''h5py is not installed on your environment.
17 Please install h5py to activate hdf5 serializers.
18 
19   $ pip install h5py'''
20         raise RuntimeError(msg)
21 
22 
23 class HDF5Serializer(serializer.Serializer):
24 
25     """Serializer for HDF5 format.
26 
27     This is the standard serializer in Chainer. The chain hierarchy is simply
28     mapped to HDF5 hierarchical groups.
29 
30     Args:
31         group (h5py.Group): The group that this serializer represents.
32         compression (int): Gzip compression level.
33 
34     """
35 
36     def __init__(self, group, compression=4):
37         _check_available()
38 
39         self.group = group
40         self.compression = compression
41 
42     def __getitem__(self, key):
43         name = self.group.name + '/' + key
44         return HDF5Serializer(self.group.require_group(name), self.compression)
45 
46     def __call__(self, key, value):
47         ret = value
48         if isinstance(value, cuda.ndarray):
49             value = cuda.to_cpu(value)
50         arr = numpy.asarray(value)
51         compression = None if arr.size <= 1 else self.compression
52         self.group.create_dataset(key, data=arr, compression=compression)
53         return ret
54 
55 
56 def save_hdf5(filename, obj, compression=4):
57     """Saves an object to the file in HDF5 format.
58 
59     This is a short-cut function to save only one object into an HDF5 file. If
60     you want to save multiple objects to one HDF5 file, use
61     :class:`HDF5Serializer` directly by passing appropriate :class:`h5py.Group`
62     objects.
63 
64     Args:
65         filename (str): Target file name.
66         obj: Object to be serialized. It must support serialization protocol.
67         compression (int): Gzip compression level.
68 
69     """
70     _check_available()
71     with h5py.File(filename, 'w') as f:
72         s = HDF5Serializer(f, compression=compression)
73         s.save(obj)
74 
75 
76 class HDF5Deserializer(serializer.Deserializer):
77 
78     """Deserializer for HDF5 format.
79 
80     This is the standard deserializer in Chainer. This deserializer can be used
81     to read an object serialized by :class:`HDF5Serializer`.
82 
83     Args:
84         group (h5py.Group): The group that the deserialization starts from.
85 
86     """
87 
88     def __init__(self, group):
89         _check_available()
90         self.group = group
91 
92     def __getitem__(self, key):
93         name = self.group.name + '/' + key
94         return HDF5Deserializer(self.group.require_group(name))
95 
96     def __call__(self, key, value):
97         dataset = self.group[key]
98         if value is None:
99             return numpy.asarray(dataset)
100         elif isinstance(value, numpy.ndarray):
101             dataset.read_direct(value)
102         elif isinstance(value, cuda.ndarray):
103             value.set(numpy.asarray(dataset))
104         else:
105             value = type(value)(numpy.asarray(dataset))
106         return value
107 
108 
109 def load_hdf5(filename, obj):
110     """Loads an object from the file in HDF5 format.
111 
112     This is a short-cut function to load from an HDF5 file that contains only
113     one object. If you want to load multiple objects from one HDF5 file, use
114     :class:`HDF5Deserializer` directly by passing appropriate
115     :class:`h5py.Group` objects.
116 
117     Args:
118         filename (str): Name of the file to be loaded.
119         obj: Object to be deserialized. It must support serialization protocol.
120 
121     """
122     _check_available()
123     with h5py.File(filename, 'r') as f:
124         d = HDF5Deserializer(f)
125         d.load(obj)
126
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/chainer/serializers/hdf5.py b/chainer/serializers/hdf5.py
--- a/chainer/serializers/hdf5.py
+++ b/chainer/serializers/hdf5.py
@@ -82,18 +82,26 @@
 
     Args:
         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.
 
     """
 
-    def __init__(self, group):
+    def __init__(self, group, strict=True):
         _check_available()
         self.group = group
+        self.strict = strict
 
     def __getitem__(self, key):
         name = self.group.name + '/' + key
         return HDF5Deserializer(self.group.require_group(name))
 
     def __call__(self, key, value):
+        if not self.strict and key not in self.group:
+            return value
+
+        self.group.keys
         dataset = self.group[key]
         if value is None:
             return numpy.asarray(dataset)
diff --git a/chainer/serializers/npz.py b/chainer/serializers/npz.py
--- a/chainer/serializers/npz.py
+++ b/chainer/serializers/npz.py
@@ -82,20 +82,27 @@
     Args:
         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.
 
     """
 
-    def __init__(self, npz, path=''):
+    def __init__(self, npz, path='', strict=True):
         self.npz = npz
         self.path = path
+        self.strict = strict
 
     def __getitem__(self, key):
         key = key.strip('/')
         return NpzDeserializer(self.npz, self.path + key + '/')
 
     def __call__(self, key, value):
-        key = key.lstrip('/')
-        dataset = self.npz[self.path + key]
+        key = self.path + key.lstrip('/')
+        if not self.strict and key not in self.npz:
+            return value
+
+        dataset = self.npz[key]
         if value is None:
             return dataset
         elif isinstance(value, numpy.ndarray):

{"golden_diff": "diff --git a/chainer/serializers/hdf5.py b/chainer/serializers/hdf5.py\n--- a/chainer/serializers/hdf5.py\n+++ b/chainer/serializers/hdf5.py\n@@ -82,18 +82,26 @@\n \n     Args:\n         group (h5py.Group): The group that the deserialization starts from.\n+        strict (bool): If ``True``, the deserializer raises an error when an\n+            expected value is not found in the given HDF5 file. Otherwise,\n+            it ignores the value and skip deserialization.\n \n     \"\"\"\n \n-    def __init__(self, group):\n+    def __init__(self, group, strict=True):\n         _check_available()\n         self.group = group\n+        self.strict = strict\n \n     def __getitem__(self, key):\n         name = self.group.name + '/' + key\n         return HDF5Deserializer(self.group.require_group(name))\n \n     def __call__(self, key, value):\n+        if not self.strict and key not in self.group:\n+            return value\n+\n+        self.group.keys\n         dataset = self.group[key]\n         if value is None:\n             return numpy.asarray(dataset)\ndiff --git a/chainer/serializers/npz.py b/chainer/serializers/npz.py\n--- a/chainer/serializers/npz.py\n+++ b/chainer/serializers/npz.py\n@@ -82,20 +82,27 @@\n     Args:\n         npz: `npz` file object.\n         path: The base path that the deserialization starts from.\n+        strict (bool): If ``True``, the deserializer raises an error when an\n+            expected value is not found in the given NPZ file. Otherwise,\n+            it ignores the value and skip deserialization.\n \n     \"\"\"\n \n-    def __init__(self, npz, path=''):\n+    def __init__(self, npz, path='', strict=True):\n         self.npz = npz\n         self.path = path\n+        self.strict = strict\n \n     def __getitem__(self, key):\n         key = key.strip('/')\n         return NpzDeserializer(self.npz, self.path + key + '/')\n \n     def __call__(self, key, value):\n-        key = key.lstrip('/')\n-        dataset = self.npz[self.path + key]\n+        key = self.path + key.lstrip('/')\n+        if not self.strict and key not in self.npz:\n+            return value\n+\n+        dataset = self.npz[key]\n         if value is None:\n             return dataset\n         elif isinstance(value, numpy.ndarray):\n", "issue": "Partial deserialization of links\nWe sometimes want to deserialize a link only partially, i.e. we want to load a smaller set of parameters to a larger link. It happens when we do some surgery to neural nets (e.g. fine-tuning), in which case the set of parameters in the source network does not match to the set of parameters in the target network.\r\n\r\nOne possible design is to introduce a `strict` option to the deserializer, and if that flag is False, any mismatch of the sets of parameters is ignored. I think it is safer to turn on this flag by default (I mean, we should not change the default behavior).\n", "before_files": [{"content": "import numpy\n\nfrom chainer import cuda\nfrom chainer import serializer\n\n\nclass DictionarySerializer(serializer.Serializer):\n\n    \"\"\"Serializer for dictionary.\n\n    This is the standard serializer in Chainer. The hierarchy of objects are\n    simply mapped to a flat dictionary with keys representing the paths to\n    objects in the hierarchy.\n\n    .. note::\n       Despite of its name, this serializer DOES NOT serialize the\n       object into external files. It just build a flat dictionary of arrays\n       that can be fed into :func:`numpy.savez` and\n       :func:`numpy.savez_compressed`. If you want to use this serializer\n       directly, you have to manually send a resulting dictionary to one of\n       these functions.\n\n    Args:\n        target (dict): The dictionary that this serializer saves the objects\n            to. If target is None, then a new dictionary is created.\n        path (str): The base path in the hierarchy that this serializer\n            indicates.\n\n    Attributes:\n        target (dict): The target dictionary. Once the serialization completes,\n            this dictionary can be fed into :func:`numpy.savez` or\n            :func:`numpy.savez_compressed` to serialize it in the NPZ format.\n\n    \"\"\"\n\n    def __init__(self, target=None, path=''):\n        self.target = {} if target is None else target\n        self.path = path\n\n    def __getitem__(self, key):\n        key = key.strip('/')\n        return DictionarySerializer(self.target, self.path + key + '/')\n\n    def __call__(self, key, value):\n        key = key.lstrip('/')\n        ret = value\n        if isinstance(value, cuda.ndarray):\n            value = value.get()\n        arr = numpy.asarray(value)\n        self.target[self.path + key] = arr\n        return ret\n\n\ndef save_npz(filename, obj, compression=True):\n    \"\"\"Saves an object to the file in NPZ format.\n\n    This is a short-cut function to save only one object into an NPZ file.\n\n    Args:\n        filename (str): Target file name.\n        obj: Object to be serialized. It must support serialization protocol.\n        compression (bool): If ``True``, compression in the resulting zip file\n            is enabled.\n\n    \"\"\"\n    s = DictionarySerializer()\n    s.save(obj)\n    with open(filename, 'wb') as f:\n        if compression:\n            numpy.savez_compressed(f, **s.target)\n        else:\n            numpy.savez(f, **s.target)\n\n\nclass NpzDeserializer(serializer.Deserializer):\n\n    \"\"\"Deserializer for NPZ format.\n\n    This is the standard deserializer in Chainer. This deserializer can be used\n    to read an object serialized by :func:`save_npz`.\n\n    Args:\n        npz: `npz` file object.\n        path: The base path that the deserialization starts from.\n\n    \"\"\"\n\n    def __init__(self, npz, path=''):\n        self.npz = npz\n        self.path = path\n\n    def __getitem__(self, key):\n        key = key.strip('/')\n        return NpzDeserializer(self.npz, self.path + key + '/')\n\n    def __call__(self, key, value):\n        key = key.lstrip('/')\n        dataset = self.npz[self.path + key]\n        if value is None:\n            return dataset\n        elif isinstance(value, numpy.ndarray):\n            numpy.copyto(value, dataset)\n        elif isinstance(value, cuda.ndarray):\n            value.set(numpy.asarray(dataset))\n        else:\n            value = type(value)(numpy.asarray(dataset))\n        return value\n\n\ndef load_npz(filename, obj):\n    \"\"\"Loads an object from the file in NPZ format.\n\n    This is a short-cut function to load from an `.npz` file that contains only\n    one object.\n\n    Args:\n        filename (str): Name of the file to be loaded.\n        obj: Object to be deserialized. It must support serialization protocol.\n\n    \"\"\"\n    with numpy.load(filename) as f:\n        d = NpzDeserializer(f)\n        d.load(obj)\n", "path": "chainer/serializers/npz.py"}, {"content": "import numpy\n\nfrom chainer import cuda\nfrom chainer import serializer\n\n\ntry:\n    import h5py\n    _available = True\nexcept ImportError:\n    _available = False\n\n\ndef _check_available():\n    if not _available:\n        msg = '''h5py is not installed on your environment.\nPlease install h5py to activate hdf5 serializers.\n\n  $ pip install h5py'''\n        raise RuntimeError(msg)\n\n\nclass HDF5Serializer(serializer.Serializer):\n\n    \"\"\"Serializer for HDF5 format.\n\n    This is the standard serializer in Chainer. The chain hierarchy is simply\n    mapped to HDF5 hierarchical groups.\n\n    Args:\n        group (h5py.Group): The group that this serializer represents.\n        compression (int): Gzip compression level.\n\n    \"\"\"\n\n    def __init__(self, group, compression=4):\n        _check_available()\n\n        self.group = group\n        self.compression = compression\n\n    def __getitem__(self, key):\n        name = self.group.name + '/' + key\n        return HDF5Serializer(self.group.require_group(name), self.compression)\n\n    def __call__(self, key, value):\n        ret = value\n        if isinstance(value, cuda.ndarray):\n            value = cuda.to_cpu(value)\n        arr = numpy.asarray(value)\n        compression = None if arr.size <= 1 else self.compression\n        self.group.create_dataset(key, data=arr, compression=compression)\n        return ret\n\n\ndef save_hdf5(filename, obj, compression=4):\n    \"\"\"Saves an object to the file in HDF5 format.\n\n    This is a short-cut function to save only one object into an HDF5 file. If\n    you want to save multiple objects to one HDF5 file, use\n    :class:`HDF5Serializer` directly by passing appropriate :class:`h5py.Group`\n    objects.\n\n    Args:\n        filename (str): Target file name.\n        obj: Object to be serialized. It must support serialization protocol.\n        compression (int): Gzip compression level.\n\n    \"\"\"\n    _check_available()\n    with h5py.File(filename, 'w') as f:\n        s = HDF5Serializer(f, compression=compression)\n        s.save(obj)\n\n\nclass HDF5Deserializer(serializer.Deserializer):\n\n    \"\"\"Deserializer for HDF5 format.\n\n    This is the standard deserializer in Chainer. This deserializer can be used\n    to read an object serialized by :class:`HDF5Serializer`.\n\n    Args:\n        group (h5py.Group): The group that the deserialization starts from.\n\n    \"\"\"\n\n    def __init__(self, group):\n        _check_available()\n        self.group = group\n\n    def __getitem__(self, key):\n        name = self.group.name + '/' + key\n        return HDF5Deserializer(self.group.require_group(name))\n\n    def __call__(self, key, value):\n        dataset = self.group[key]\n        if value is None:\n            return numpy.asarray(dataset)\n        elif isinstance(value, numpy.ndarray):\n            dataset.read_direct(value)\n        elif isinstance(value, cuda.ndarray):\n            value.set(numpy.asarray(dataset))\n        else:\n            value = type(value)(numpy.asarray(dataset))\n        return value\n\n\ndef load_hdf5(filename, obj):\n    \"\"\"Loads an object from the file in HDF5 format.\n\n    This is a short-cut function to load from an HDF5 file that contains only\n    one object. If you want to load multiple objects from one HDF5 file, use\n    :class:`HDF5Deserializer` directly by passing appropriate\n    :class:`h5py.Group` objects.\n\n    Args:\n        filename (str): Name of the file to be loaded.\n        obj: Object to be deserialized. It must support serialization protocol.\n\n    \"\"\"\n    _check_available()\n    with h5py.File(filename, 'r') as f:\n        d = HDF5Deserializer(f)\n        d.load(obj)\n", "path": "chainer/serializers/hdf5.py"}], "after_files": [{"content": "import numpy\n\nfrom chainer import cuda\nfrom chainer import serializer\n\n\nclass DictionarySerializer(serializer.Serializer):\n\n    \"\"\"Serializer for dictionary.\n\n    This is the standard serializer in Chainer. The hierarchy of objects are\n    simply mapped to a flat dictionary with keys representing the paths to\n    objects in the hierarchy.\n\n    .. note::\n       Despite of its name, this serializer DOES NOT serialize the\n       object into external files. It just build a flat dictionary of arrays\n       that can be fed into :func:`numpy.savez` and\n       :func:`numpy.savez_compressed`. If you want to use this serializer\n       directly, you have to manually send a resulting dictionary to one of\n       these functions.\n\n    Args:\n        target (dict): The dictionary that this serializer saves the objects\n            to. If target is None, then a new dictionary is created.\n        path (str): The base path in the hierarchy that this serializer\n            indicates.\n\n    Attributes:\n        target (dict): The target dictionary. Once the serialization completes,\n            this dictionary can be fed into :func:`numpy.savez` or\n            :func:`numpy.savez_compressed` to serialize it in the NPZ format.\n\n    \"\"\"\n\n    def __init__(self, target=None, path=''):\n        self.target = {} if target is None else target\n        self.path = path\n\n    def __getitem__(self, key):\n        key = key.strip('/')\n        return DictionarySerializer(self.target, self.path + key + '/')\n\n    def __call__(self, key, value):\n        key = key.lstrip('/')\n        ret = value\n        if isinstance(value, cuda.ndarray):\n            value = value.get()\n        arr = numpy.asarray(value)\n        self.target[self.path + key] = arr\n        return ret\n\n\ndef save_npz(filename, obj, compression=True):\n    \"\"\"Saves an object to the file in NPZ format.\n\n    This is a short-cut function to save only one object into an NPZ file.\n\n    Args:\n        filename (str): Target file name.\n        obj: Object to be serialized. It must support serialization protocol.\n        compression (bool): If ``True``, compression in the resulting zip file\n            is enabled.\n\n    \"\"\"\n    s = DictionarySerializer()\n    s.save(obj)\n    with open(filename, 'wb') as f:\n        if compression:\n            numpy.savez_compressed(f, **s.target)\n        else:\n            numpy.savez(f, **s.target)\n\n\nclass NpzDeserializer(serializer.Deserializer):\n\n    \"\"\"Deserializer for NPZ format.\n\n    This is the standard deserializer in Chainer. This deserializer can be used\n    to read an object serialized by :func:`save_npz`.\n\n    Args:\n        npz: `npz` file object.\n        path: The base path that the deserialization starts from.\n        strict (bool): If ``True``, the deserializer raises an error when an\n            expected value is not found in the given NPZ file. Otherwise,\n            it ignores the value and skip deserialization.\n\n    \"\"\"\n\n    def __init__(self, npz, path='', strict=True):\n        self.npz = npz\n        self.path = path\n        self.strict = strict\n\n    def __getitem__(self, key):\n        key = key.strip('/')\n        return NpzDeserializer(self.npz, self.path + key + '/')\n\n    def __call__(self, key, value):\n        key = self.path + key.lstrip('/')\n        if not self.strict and key not in self.npz:\n            return value\n\n        dataset = self.npz[key]\n        if value is None:\n            return dataset\n        elif isinstance(value, numpy.ndarray):\n            numpy.copyto(value, dataset)\n        elif isinstance(value, cuda.ndarray):\n            value.set(numpy.asarray(dataset))\n        else:\n            value = type(value)(numpy.asarray(dataset))\n        return value\n\n\ndef load_npz(filename, obj):\n    \"\"\"Loads an object from the file in NPZ format.\n\n    This is a short-cut function to load from an `.npz` file that contains only\n    one object.\n\n    Args:\n        filename (str): Name of the file to be loaded.\n        obj: Object to be deserialized. It must support serialization protocol.\n\n    \"\"\"\n    with numpy.load(filename) as f:\n        d = NpzDeserializer(f)\n        d.load(obj)\n", "path": "chainer/serializers/npz.py"}, {"content": "import numpy\n\nfrom chainer import cuda\nfrom chainer import serializer\n\n\ntry:\n    import h5py\n    _available = True\nexcept ImportError:\n    _available = False\n\n\ndef _check_available():\n    if not _available:\n        msg = '''h5py is not installed on your environment.\nPlease install h5py to activate hdf5 serializers.\n\n  $ pip install h5py'''\n        raise RuntimeError(msg)\n\n\nclass HDF5Serializer(serializer.Serializer):\n\n    \"\"\"Serializer for HDF5 format.\n\n    This is the standard serializer in Chainer. The chain hierarchy is simply\n    mapped to HDF5 hierarchical groups.\n\n    Args:\n        group (h5py.Group): The group that this serializer represents.\n        compression (int): Gzip compression level.\n\n    \"\"\"\n\n    def __init__(self, group, compression=4):\n        _check_available()\n\n        self.group = group\n        self.compression = compression\n\n    def __getitem__(self, key):\n        name = self.group.name + '/' + key\n        return HDF5Serializer(self.group.require_group(name), self.compression)\n\n    def __call__(self, key, value):\n        ret = value\n        if isinstance(value, cuda.ndarray):\n            value = cuda.to_cpu(value)\n        arr = numpy.asarray(value)\n        compression = None if arr.size <= 1 else self.compression\n        self.group.create_dataset(key, data=arr, compression=compression)\n        return ret\n\n\ndef save_hdf5(filename, obj, compression=4):\n    \"\"\"Saves an object to the file in HDF5 format.\n\n    This is a short-cut function to save only one object into an HDF5 file. If\n    you want to save multiple objects to one HDF5 file, use\n    :class:`HDF5Serializer` directly by passing appropriate :class:`h5py.Group`\n    objects.\n\n    Args:\n        filename (str): Target file name.\n        obj: Object to be serialized. It must support serialization protocol.\n        compression (int): Gzip compression level.\n\n    \"\"\"\n    _check_available()\n    with h5py.File(filename, 'w') as f:\n        s = HDF5Serializer(f, compression=compression)\n        s.save(obj)\n\n\nclass HDF5Deserializer(serializer.Deserializer):\n\n    \"\"\"Deserializer for HDF5 format.\n\n    This is the standard deserializer in Chainer. This deserializer can be used\n    to read an object serialized by :class:`HDF5Serializer`.\n\n    Args:\n        group (h5py.Group): The group that the deserialization starts from.\n        strict (bool): If ``True``, the deserializer raises an error when an\n            expected value is not found in the given HDF5 file. Otherwise,\n            it ignores the value and skip deserialization.\n\n    \"\"\"\n\n    def __init__(self, group, strict=True):\n        _check_available()\n        self.group = group\n        self.strict = strict\n\n    def __getitem__(self, key):\n        name = self.group.name + '/' + key\n        return HDF5Deserializer(self.group.require_group(name))\n\n    def __call__(self, key, value):\n        if not self.strict and key not in self.group:\n            return value\n\n        self.group.keys\n        dataset = self.group[key]\n        if value is None:\n            return numpy.asarray(dataset)\n        elif isinstance(value, numpy.ndarray):\n            dataset.read_direct(value)\n        elif isinstance(value, cuda.ndarray):\n            value.set(numpy.asarray(dataset))\n        else:\n            value = type(value)(numpy.asarray(dataset))\n        return value\n\n\ndef load_hdf5(filename, obj):\n    \"\"\"Loads an object from the file in HDF5 format.\n\n    This is a short-cut function to load from an HDF5 file that contains only\n    one object. If you want to load multiple objects from one HDF5 file, use\n    :class:`HDF5Deserializer` directly by passing appropriate\n    :class:`h5py.Group` objects.\n\n    Args:\n        filename (str): Name of the file to be loaded.\n        obj: Object to be deserialized. It must support serialization protocol.\n\n    \"\"\"\n    _check_available()\n    with h5py.File(filename, 'r') as f:\n        d = HDF5Deserializer(f)\n        d.load(obj)\n", "path": "chainer/serializers/hdf5.py"}]}