Пример #1
0
    def setUp(self):
        if self.file_type == 'filename':
            fd, path = tempfile.mkstemp()
            os.close(fd)
            self.file = path
        elif self.file_type == 'bytesio':
            self.file = six.BytesIO()
        else:
            assert False

        child = link.Chain()
        with child.init_scope():
            child.linear = links.Linear(2, 3)
            child.Wc = chainer.Parameter(shape=(2, 3))

        self.parent = link.Chain()
        with self.parent.init_scope():
            self.parent.child = child
            self.parent.Wp = chainer.Parameter(shape=(2, 3))

        self.optimizer = optimizers.AdaDelta()
        self.optimizer.setup(self.parent)

        self.parent.cleargrads()
        self.optimizer.update()  # init all states

        self.savez = numpy.savez_compressed if self.compress else numpy.savez
Пример #2
0
    def test_deserialize_hierarchy(self):
        # Load a link
        child = link.Chain()
        with child.init_scope():
            child.linear2 = links.Linear(2, 3)
        target = link.Chain()
        with target.init_scope():
            target.linear = links.Linear(3, 2)
            target.child = child
        target_child_W = numpy.copy(child.linear2.W.data)
        target_child_b = numpy.copy(child.linear2.b.data)

        self.deserializer.load(target)

        # Check
        numpy.testing.assert_array_equal(
            self.source.linear.W.data, target.linear.W.data)
        numpy.testing.assert_array_equal(
            self.source.linear.W.data, target.linear.W.data)
        numpy.testing.assert_array_equal(
            self.source.linear.b.data, target.linear.b.data)
        numpy.testing.assert_array_equal(
            target.child.linear2.W.data, target_child_W)
        numpy.testing.assert_array_equal(
            target.child.linear2.b.data, target_child_b)
Пример #3
0
    def setUp(self):
        if self.file_type == 'filename':
            fd, path = tempfile.mkstemp()
            os.close(fd)
            self.file = path
        elif self.file_type == 'bytesio':
            self.file = six.BytesIO()
        else:
            assert False

        # Create and save a link
        child = link.Chain()
        with child.init_scope():
            child.linear = links.Linear(2, 3)
        parent = link.Chain()
        with parent.init_scope():
            parent.linear = links.Linear(3, 2)
            parent.child = child
        npz.save_npz(self.file, parent)
        self.source = parent

        if self.file_type == 'bytesio':
            self.file.seek(0)

        self.npzfile = numpy.load(self.file)
        self.deserializer = npz.NpzDeserializer(self.npzfile, strict=False)
Пример #4
0
    def setUp(self):
        fd, path = tempfile.mkstemp()
        os.close(fd)
        self.temp_file_path = path
        child = link.Chain(child_linear=links.Linear(2, 3))
        parent = link.Chain(
            parent_linear=links.Linear(3, 2), child=child)
        npz.save_npz(path, parent, self.compress)

        self.source_child = child
        self.source_parent = parent
Пример #5
0
    def setUp(self):
        fd, path = tempfile.mkstemp()
        os.close(fd)
        self.temp_file_path = path

        child = link.Chain(linear=links.Linear(2, 3))
        child.add_param('Wc', (2, 3))
        self.parent = link.Chain(child=child)
        self.parent.add_param('Wp', (2, 3))

        self.optimizer = optimizers.AdaDelta()
        self.optimizer.setup(self.parent)
Пример #6
0
    def setUp(self):
        fd, path = tempfile.mkstemp()
        os.close(fd)
        self.temp_file_path = path

        child = link.Chain(linear=links.Linear(2, 3))
        parent = link.Chain(linear=links.Linear(3, 2), child=child)
        hdf5.save_hdf5(self.temp_file_path, parent)
        self.source = parent

        self.hdf5file = h5py.File(path, 'r')
        self.deserializer = hdf5.HDF5Deserializer(self.hdf5file, strict=False)
Пример #7
0
    def setUp(self):
        fd, path = tempfile.mkstemp()
        os.close(fd)
        self.temp_file_path = path

        child = link.Chain(linear=links.Linear(2, 3))
        parent = link.Chain(linear=links.Linear(3, 2), child=child)
        npz.save_npz(self.temp_file_path, parent)
        self.source = parent

        self.npzfile = numpy.load(path)
        self.deserializer = npz.NpzDeserializer(self.npzfile, strict=False)
Пример #8
0
    def setUp(self):
        fd, path = tempfile.mkstemp()
        os.close(fd)
        self.temp_file_path = path
        child = link.Chain()
        with child.init_scope():
            child.child_linear = links.Linear(2, 3)
        parent = link.Chain()
        with parent.init_scope():
            parent.parent_linear = links.Linear(3, 2)
            parent.child = child
        npz.save_npz(path, parent, self.compress)

        self.source_child = child
        self.source_parent = parent
Пример #9
0
    def test_deserialize_hierarchy(self):
        child = link.Chain(linear2=links.Linear(2, 3))
        target = link.Chain(linear=links.Linear(3, 2), child=child)
        target_child_W = numpy.copy(child.linear2.W.data)
        target_child_b = numpy.copy(child.linear2.b.data)
        self.deserializer.load(target)

        numpy.testing.assert_array_equal(self.source.linear.W.data,
                                         target.linear.W.data)
        numpy.testing.assert_array_equal(self.source.linear.b.data,
                                         target.linear.b.data)
        numpy.testing.assert_array_equal(target.child.linear2.W.data,
                                         target_child_W)
        numpy.testing.assert_array_equal(target.child.linear2.b.data,
                                         target_child_b)
Пример #10
0
 def test_load_with_path(self):
     target = link.Chain()
     with target.init_scope():
         target.child_linear = links.Linear(2, 3)
     npz.load_npz(self.temp_file_path, target, 'child/')
     numpy.testing.assert_array_equal(self.source_child.child_linear.W.data,
                                      target.child_linear.W.data)
Пример #11
0
 def test_load_without_path(self):
     target = link.Chain()
     with target.init_scope():
         target.parent_linear = links.Linear(3, 2)
     npz.load_npz(self.temp_file_path, target, path='')
     numpy.testing.assert_array_equal(
         self.source_parent.parent_linear.W.data,
         target.parent_linear.W.data)
Пример #12
0
    def setUp(self):
        fd, path = tempfile.mkstemp()
        os.close(fd)
        self.temp_file_path = path

        child = link.Chain(linear=links.Linear(2, 3))
        child.add_param('Wc', (2, 3))
        self.parent = link.Chain(child=child)
        self.parent.add_param('Wp', (2, 3))

        self.optimizer = optimizers.AdaDelta()
        self.optimizer.setup(self.parent)

        self.parent.zerograds()
        self.optimizer.update()  # init all states

        self.savez = numpy.savez_compressed if self.compress else numpy.savez
Пример #13
0
    def setUp(self):
        fd, path = tempfile.mkstemp()
        os.close(fd)
        self.temp_file_path = path

        child = link.Chain()
        with child.init_scope():
            child.linear2 = links.Linear(2, 3)
        parent = link.Chain()
        with parent.init_scope():
            parent.linear = links.Linear(3, 2)
            parent.child = child
        npz.save_npz(self.temp_file_path, parent)
        self.source = parent

        self.npzfile = numpy.load(path)
        self.deserializer = npz.NpzDeserializer(self.npzfile,
                                                ignore_names=self.ignore_names)
Пример #14
0
 def test_load_npz_ignore_names(self):
     chain = link.Chain()
     with chain.init_scope():
         chain.x = chainer.variable.Parameter(shape=())
         chain.yy = chainer.variable.Parameter(shape=(2, 3))
     npz.load_npz(
         self.temp_file_path, chain, ignore_names=self.ignore_names)
     self.assertEqual(chain.x.data, self.x)
     self.assertFalse(numpy.all(chain.yy.data == self.yy))
Пример #15
0
    def setUp(self):
        fd, path = tempfile.mkstemp()
        os.close(fd)
        self.temp_file_path = path

        child = link.Chain()
        with child.init_scope():
            child.linear = links.Linear(2, 3)
            child.Wc = chainer.Parameter(shape=(2, 3))

        self.parent = link.Chain()
        with self.parent.init_scope():
            self.parent.child = child
            self.parent.Wp = chainer.Parameter(shape=(2, 3))

        self.optimizer = optimizers.AdaDelta()
        self.optimizer.setup(self.parent)

        self.parent.cleargrads()
        self.optimizer.update()  # init states
Пример #16
0
    def setUp(self):
        if self.file_type == 'filename':
            fd, path = tempfile.mkstemp()
            os.close(fd)
            self.file = path
        elif self.file_type == 'bytesio':
            self.file = six.BytesIO()
        else:
            assert False

        child = link.Chain()
        with child.init_scope():
            child.child_linear = links.Linear(2, 3)
        parent = link.Chain()
        with parent.init_scope():
            parent.parent_linear = links.Linear(3, 2)
            parent.child = child
        npz.save_npz(self.file, parent, self.compress)

        if self.file_type == 'bytesio':
            self.file.seek(0)

        self.source_child = child
        self.source_parent = parent