Skip to content

Commit

Permalink
merge 2949 for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mattya committed Jul 3, 2017
1 parent 17d3b07 commit f3986dd
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 34 deletions.
4 changes: 3 additions & 1 deletion tests/chainer_tests/optimizers_tests/test_optimizers.py
Expand Up @@ -23,7 +23,9 @@
class TestOptimizerHyperparameter(unittest.TestCase):

def setUp(self):
self.target = chainer.Link(w=())
self.target = chainer.Link()
with self.target.init_scope():
self.target.w = chainer.Parameter()

def create(self, *args, **kwargs):
self.optimizer = self.impl(*args, **kwargs)
Expand Down
18 changes: 14 additions & 4 deletions tests/chainer_tests/serializers_tests/test_hdf5.py
Expand Up @@ -221,8 +221,13 @@ def setUp(self):
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)
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

Expand All @@ -236,8 +241,13 @@ def tearDown(self):
os.remove(self.temp_file_path)

def test_deserialize_hierarchy(self):
child = link.Chain(linear2=links.Linear(2, 3))
target = link.Chain(linear=links.Linear(3, 2), child=child)
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)
Expand Down
115 changes: 90 additions & 25 deletions tests/chainer_tests/test_link.py
Expand Up @@ -240,7 +240,12 @@ def test_copyparams(self):
gy = self.link.y.grad.copy()
gu = self.link.u.grad.copy()

l = chainer.Link(x=(2, 3), y=2, u=(2, 3), v=(3, 2))
l = chainer.Link()
with l.init_scope():
l.x = chainer.Parameter(shape=(2, 3))
l.y = chainer.Parameter(shape=2)
l.u = chainer.Parameter(shape=(2, 3))
l.v = chainer.Parameter(shape=(3, 2))
l.x.data.fill(2)
l.x.grad.fill(3)
l.y.data.fill(4)
Expand Down Expand Up @@ -283,7 +288,12 @@ def test_zerograds(self):
numpy.testing.assert_array_equal(self.link.v.grad, gv_expect)

def test_addgrads(self):
l = chainer.Link(x=(2, 3), y=2, u=(2, 3), v=None)
l = chainer.Link()
with l.init_scope():
l.x = chainer.Parameter(shape=(2, 3))
l.y = chainer.Parameter(shape=2)
l.u = chainer.Parameter(shape=(2, 3))
l.v = chainer.Parameter()
l.x.grad.fill(1)
l.y.grad.fill(2)
l.u.grad.fill(3)
Expand All @@ -304,7 +314,10 @@ def test_addgrads(self):

def test_serialize(self):
serializer = mock.MagicMock(return_value=3)
l = chainer.Link(x=(2, 3), y=2)
l = chainer.Link()
with l.init_scope():
l.x = chainer.Parameter(shape=(2, 3))
l.y = chainer.Parameter(shape=2)
l.add_persistent('z', 1)
l.serialize(serializer)
self.assertEqual(serializer.call_count, 3)
Expand All @@ -315,7 +328,10 @@ def test_serialize(self):

def test_serialize_param_shape_placeholder(self):
serializer = mock.MagicMock(return_value=3)
l = chainer.Link(y=2, x=None)
l = chainer.Link()
with l.init_scope():
l.y = chainer.Parameter(shape=2)
l.x = chainer.Parameter()
l.x.initialize((2, 3))
l.add_persistent('z', 1)
l.serialize(serializer)
Expand All @@ -328,7 +344,9 @@ def test_serialize_param_shape_placeholder(self):
def test_serialize_deserialize_to_uninitialized_param(self):
ret = numpy.random.rand(2, 3).astype('f')
serializer = mock.MagicMock(return_value=ret)
l = chainer.Link(x=None)
l = chainer.Link()
with l.init_scope():
l.x = chainer.Parameter()
l.serialize(serializer)
self.assertEqual(serializer.call_count, 1)
serializer.assert_any_call('x', None)
Expand Down Expand Up @@ -573,11 +591,23 @@ def test_children(self):
self.assertEqual({id(c) for c in children}, {id(self.c1), id(self.l3)})

def test_copyparams(self):
l1 = chainer.Link(x=(2, 3))
l2 = chainer.Link(x=2)
l3 = chainer.Link(x=3)
c1 = chainer.Chain(l1=l1, l2=l2)
c2 = chainer.Chain(c1=c1, l3=l3)
l1 = chainer.Link()
with l1.init_scope():
l1.x = chainer.Parameter(shape=(2, 3))
l2 = chainer.Link()
with l2.init_scope():
l2.x = chainer.Parameter(shape=2)
l3 = chainer.Link()
with l3.init_scope():
l3.x = chainer.Parameter(shape=3)
c1 = chainer.Chain()
with c1.init_scope():
c1.l1 = l1
c1.l2 = l2
c2 = chainer.Chain()
with c2.init_scope():
c2.c1 = c1
c2.l3 = l3
l1.x.data.fill(0)
l2.x.data.fill(1)
l3.x.data.fill(2)
Expand All @@ -601,11 +631,23 @@ def test_zerograds(self):
numpy.testing.assert_array_equal(self.l3.x.grad, numpy.zeros(3))

def test_addgrads(self):
l1 = chainer.Link(x=(2, 3))
l2 = chainer.Link(x=2)
l3 = chainer.Link(x=3)
c1 = chainer.Chain(l1=l1, l2=l2)
c2 = chainer.Chain(c1=c1, l3=l3)
l1 = chainer.Link()
with l1.init_scope():
l1.x = chainer.Parameter(shape=(2, 3))
l2 = chainer.Link()
with l2.init_scope():
l2.x = chainer.Parameter(shape=2)
l3 = chainer.Link()
with l3.init_scope():
l3.x = chainer.Parameter(shape=3)
c1 = chainer.Chain()
with c1.init_scope():
c1.l1 = l1
c1.l2 = l2
c2 = chainer.Chain()
with c2.init_scope():
c2.c1 = c1
c2.l3 = l3
l1.x.grad.fill(1)
l2.x.grad.fill(2)
l3.x.grad.fill(3)
Expand Down Expand Up @@ -637,9 +679,16 @@ def test_serialize(self):
class TestChainList(unittest.TestCase):

def setUp(self):
self.l1 = chainer.Link(x=(2, 3), y=None)
self.l2 = chainer.Link(x=2)
self.l3 = chainer.Link(x=3)
self.l1 = chainer.Link()
with self.l1.init_scope():
self.l1.x = chainer.Parameter(shape=(2, 3))
self.l1.y = chainer.Parameter()
self.l2 = chainer.Link()
with self.l2.init_scope():
self.l2.x = chainer.Parameter(shape=2)
self.l3 = chainer.Link()
with self.l3.init_scope():
self.l3.x = chainer.Parameter(shape=3)
self.c1 = chainer.ChainList(self.l1)
self.c1.add_link(self.l2)
self.c2 = chainer.ChainList(self.c1)
Expand Down Expand Up @@ -834,9 +883,16 @@ def test_children(self):
(id(self.l1), id(self.l2)))

def test_copyparams(self):
l1 = chainer.Link(x=(2, 3), y=None)
l2 = chainer.Link(x=2)
l3 = chainer.Link(x=3)
l1 = chainer.Link()
with l1.init_scope():
l1.x = chainer.Parameter(shape=(2, 3))
l1.y = chainer.Parameter()
l2 = chainer.Link()
with l2.init_scope():
l2.x = chainer.Parameter(shape=2)
l3 = chainer.Link()
with l3.init_scope():
l3.x = chainer.Parameter(shape=3)
c1 = chainer.ChainList(l1, l2)
c2 = chainer.ChainList(c1, l3)
l1.x.data.fill(0)
Expand Down Expand Up @@ -866,9 +922,16 @@ def test_cleargrads(self):
self.assertIsNone(self.l1.y.grad)

def test_addgrads(self):
l1 = chainer.Link(x=(2, 3), y=(2, 3))
l2 = chainer.Link(x=2)
l3 = chainer.Link(x=3)
l1 = chainer.Link()
with self.l1.init_scope():
l1.x = chainer.Parameter(shape=(2, 3))
l1.y = chainer.Parameter(shape=(2, 3))
l2 = chainer.Link()
with l2.init_scope():
l2.x = chainer.Parameter(shape=2)
l3 = chainer.Link()
with l3.init_scope():
l3.x = chainer.Parameter(shape=3)
c1 = chainer.ChainList(l1, l2)
c2 = chainer.ChainList(c1, l3)
l1.x.grad.fill(1)
Expand All @@ -888,7 +951,9 @@ def test_addgrads(self):
numpy.testing.assert_array_equal(self.l3.x.grad, numpy.zeros(3))

def test_serialize(self):
l1 = chainer.Link(y=(1, 1))
l1 = chainer.Link()
with l1.init_scope():
l1.y = chainer.Parameter(shape=(1, 1))

l2 = chainer.Link()
with l2.init_scope():
Expand Down
Expand Up @@ -15,10 +15,10 @@ class SimpleNet(chainer.Chain):
insize = 5

def __init__(self):
super(SimpleNet, self).__init__(
conv=chainer.links.Convolution2D(2, 2, 3),
fc=chainer.links.Linear(18, 2),
)
super(SimpleNet, self).__init__()
with self.init_scope():
self.conv = chainer.links.Convolution2D(2, 2, 3)
self.fc = chainer.links.Linear(18, 2)
self.train = True

def clear(self):
Expand Down

0 comments on commit f3986dd

Please sign in to comment.