Python chainer.link.Chain() Examples
The following are 13
code examples of chainer.link.Chain().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
chainer.link
, or try the search function
.
Example #1
Source File: test_npz.py From chainer with MIT License | 6 votes |
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)
Example #2
Source File: test_npz.py From chainer with MIT License | 6 votes |
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)
Example #3
Source File: test_npz.py From chainer with MIT License | 6 votes |
def test_deserialize_ignore_names(self): 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_W = numpy.copy(target.linear.W.data) target_child_b = numpy.copy(child.linear2.b.data) self.deserializer.load(target) numpy.testing.assert_array_equal( self.source.linear.b.data, target.linear.b.data) numpy.testing.assert_array_equal( self.source.child.linear2.W.data, target.child.linear2.W.data) numpy.testing.assert_array_equal( target.linear.W.data, target_W) numpy.testing.assert_array_equal( target.child.linear2.b.data, target_child_b)
Example #4
Source File: test_hdf5.py From chainer with MIT License | 6 votes |
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) parent = link.Chain() with parent.init_scope(): parent.linear = links.Linear(3, 2) parent.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)
Example #5
Source File: test_hdf5.py From chainer with MIT License | 6 votes |
def test_deserialize_hierarchy(self): 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) 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)
Example #6
Source File: create_chainer_model.py From chainer-dfi with MIT License | 6 votes |
def copy_model(src, dst): assert isinstance(src, link.Chain) assert isinstance(dst, link.Chain) for child in src.children(): if child.name not in dst.__dict__: continue dst_child = dst[child.name] if type(child) != type(dst_child): continue if isinstance(child, link.Chain): copy_model(child, dst_child) if isinstance(child, link.Link): match = True for a, b in zip(child.namedparams(), dst_child.namedparams()): if a[0] != b[0]: match = False break if a[1].data.shape != b[1].data.shape: match = False break if not match: print 'Ignore %s because of parameter mismatch' % child.name continue for a, b in zip(child.namedparams(), dst_child.namedparams()): b[1].data = a[1].data print 'Copy %s' % child.name
Example #7
Source File: create_chainer_model.py From chainer-fast-neuralstyle with MIT License | 6 votes |
def copy_model(src, dst): assert isinstance(src, link.Chain) assert isinstance(dst, link.Chain) for child in src.children(): if child.name not in dst.__dict__: continue dst_child = dst[child.name] if type(child) != type(dst_child): continue if isinstance(child, link.Chain): copy_model(child, dst_child) if isinstance(child, link.Link): match = True for a, b in zip(child.namedparams(), dst_child.namedparams()): if a[0] != b[0]: match = False break if a[1].data.shape != b[1].data.shape: match = False break if not match: print('Ignore %s because of parameter mismatch' % child.name) continue for a, b in zip(child.namedparams(), dst_child.namedparams()): b[1].data = a[1].data print('Copy %s' % child.name)
Example #8
Source File: utils.py From Semantic-Segmentation-using-Adversarial-Networks with MIT License | 5 votes |
def copy_chainermodel(src, dst): from chainer import link assert isinstance(src, link.Chain) assert isinstance(dst, link.Chain) print('Copying layers %s -> %s:' % (src.__class__.__name__, dst.__class__.__name__)) for child in src.children(): if child.name not in dst.__dict__: continue dst_child = dst[child.name] if type(child) != type(dst_child): continue if isinstance(child, link.Chain): copy_chainermodel(child, dst_child) if isinstance(child, link.Link): match = True for a, b in zip(child.namedparams(), dst_child.namedparams()): if a[0] != b[0]: match = False break if a[1].data.shape != b[1].data.shape: match = False break if not match: print('Ignore %s because of parameter mismatch.' % child.name) continue for a, b in zip(child.namedparams(), dst_child.namedparams()): b[1].data = a[1].data print(' layer: %s -> %s' % (child.name, dst_child.name)) # ----------------------------------------------------------------------------- # Data Util # -----------------------------------------------------------------------------
Example #9
Source File: test_npz.py From chainer with MIT License | 5 votes |
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))
Example #10
Source File: test_npz.py From chainer with MIT License | 5 votes |
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)
Example #11
Source File: test_npz.py From chainer with MIT License | 5 votes |
def test_load_with_path(self): target = link.Chain() with target.init_scope(): target.child_linear = links.Linear(2, 3) npz.load_npz(self.file, target, 'child/') numpy.testing.assert_array_equal( self.source_child.child_linear.W.data, target.child_linear.W.data)
Example #12
Source File: test_npz.py From chainer with MIT License | 5 votes |
def test_load_without_path(self): target = link.Chain() with target.init_scope(): target.parent_linear = links.Linear(3, 2) npz.load_npz(self.file, target, path='') numpy.testing.assert_array_equal( self.source_parent.parent_linear.W.data, target.parent_linear.W.data)
Example #13
Source File: test_npz.py From chainer with MIT License | 5 votes |
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