Ejemplo n.º 1
0
    def test_copyparams(self):
        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)

        self.c2.copyparams(c2)

        numpy.testing.assert_array_equal(self.l1.x.data, l1.x.data)
        numpy.testing.assert_array_equal(self.l2.x.data, l2.x.data)
        numpy.testing.assert_array_equal(self.l3.x.data, l3.x.data)
Ejemplo n.º 2
0
    def test_addgrads(self):
        l1 = chainer.Link()
        with 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)
        l2.x.grad.fill(2)
        l3.x.grad.fill(3)
        l1.y.grad.fill(4)

        self.l1.x.grad.fill(-1)
        self.l1.y.cleargrad()
        self.l2.x.grad.fill(-2)
        self.l3.x.grad.fill(-3)

        self.c2.addgrads(c2)
        numpy.testing.assert_array_equal(self.l1.x.grad, numpy.zeros((2, 3)))
        numpy.testing.assert_array_equal(self.l1.y.grad, l1.y.grad)
        numpy.testing.assert_array_equal(self.l2.x.grad, numpy.zeros(2))
        numpy.testing.assert_array_equal(self.l3.x.grad, numpy.zeros(3))
Ejemplo n.º 3
0
    def test_addgrads(self):
        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)

        self.l1.x.grad.fill(-1)
        self.l2.x.grad.fill(-2)
        self.l3.cleargrads()

        self.c2.addgrads(c2)
        numpy.testing.assert_array_equal(self.l1.x.grad, numpy.zeros((2, 3)))
        numpy.testing.assert_array_equal(self.l2.x.grad, numpy.zeros(2))
        numpy.testing.assert_array_equal(self.l3.x.grad, numpy.full(3, 3.))
Ejemplo n.º 4
0
 def setUp(self):
     self.l1 = chainer.Link(x=(2, 3))
     self.l2 = chainer.Link(x=2)
     self.l3 = chainer.Link(x=3)
     self.c1 = chainer.ChainList(self.l1)
     self.c1.add_link(self.l2)
     self.c2 = chainer.ChainList(self.c1, self.l3)
Ejemplo n.º 5
0
def test_namedpersistent():
    # This test case is adopted from
    # https://github.com/chainer/chainer/pull/6788

    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)
    l2.add_persistent('l2_a', numpy.array([1, 2, 3], dtype=numpy.float32))

    l3 = chainer.Link()
    with l3.init_scope():
        l3.x = chainer.Parameter()
    l3.add_persistent('l3_a', numpy.array([1, 2, 3], dtype=numpy.float32))

    c1 = chainer.Chain()
    with c1.init_scope():
        c1.l1 = l1
    c1.add_link('l2', l2)
    c1.add_persistent('c1_a', numpy.array([1, 2, 3], dtype=numpy.float32))

    c2 = chainer.Chain()
    with c2.init_scope():
        c2.c1 = c1
        c2.l3 = l3
    c2.add_persistent('c2_a', numpy.array([1, 2, 3], dtype=numpy.float32))
    namedpersistent = list(chainerrl.misc.namedpersistent(c2))
    assert ([(name, id(p)) for name, p in namedpersistent
             ] == [('/c2_a', id(c2.c2_a)), ('/c1/c1_a', id(c2.c1.c1_a)),
                   ('/c1/l2/l2_a', id(c2.c1.l2.l2_a)),
                   ('/l3/l3_a', id(c2.l3.l3_a))])
Ejemplo n.º 6
0
 def setUp(self):
     self.l1 = chainer.Link(x=(2, 3))
     self.l1.add_uninitialized_param('y')
     self.l2 = chainer.Link(x=2)
     self.l3 = chainer.Link(x=3)
     self.c1 = chainer.ChainList(self.l1)
     self.c1.add_link(self.l2)
     self.c2 = chainer.ChainList(self.c1, self.l3)
Ejemplo n.º 7
0
 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.c1 = chainer.ChainList(self.l1)
     self.c1.add_link(self.l2)
     self.c2 = chainer.ChainList(self.c1)
     self.c2.append(self.l3)
Ejemplo n.º 8
0
    def setUp(self):
        self.l1 = chainer.Link(x=(2, 3))
        self.l2 = chainer.Link(x=2)
        self.l3 = chainer.Link(x=3)

        self.c1 = chainer.Chain(l1=self.l1)
        self.c1.add_link('l2', self.l2)
        self.c2 = chainer.Chain(c1=self.c1, l3=self.l3)
    def setUp(self):
        if self.accfun is None:
            self.link = SklearnWrapperClassifier(chainer.Link())
        else:
            self.link = SklearnWrapperClassifier(chainer.Link(),
                                                 accfun=self.accfun)
        self.link.compute_accuracy = self.compute_accuracy

        self.x = numpy.random.uniform(-1, 1, (5, 10)).astype(numpy.float32)
        self.t = numpy.random.randint(3, size=5).astype(numpy.int32)
Ejemplo n.º 10
0
    def setUp(self):
        self.l1 = chainer.Link(x=(2, 3))
        self.l2 = chainer.Link(x=2)
        self.l3 = chainer.Link(x=None)

        self.c1 = chainer.Chain(l1=self.l1)
        self.c1.add_link('l2', self.l2)
        self.c2 = chainer.Chain(c1=self.c1)
        with self.c2.init_scope():
            self.c2.l3 = self.l3
Ejemplo n.º 11
0
 def setUp(self):
     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)
     self.c2.append(self.l3)
Ejemplo n.º 12
0
    def test_copyparams(self):
        l1 = chainer.Link(x=(2, 3))
        l2 = chainer.Link(x=2)
        l3 = chainer.Link(x=3)
        c1 = chainer.ChainList(l1, l2)
        c2 = chainer.ChainList(c1, l3)
        l1.x.data.fill(0)
        l2.x.data.fill(1)
        l3.x.data.fill(2)

        self.c2.copyparams(c2)

        numpy.testing.assert_array_equal(self.l1.x.data, l1.x.data)
        numpy.testing.assert_array_equal(self.l2.x.data, l2.x.data)
        numpy.testing.assert_array_equal(self.l3.x.data, l3.x.data)
Ejemplo n.º 13
0
    def __init__(self, n_hidden=512, ch=512,
                 channel_evolution=(512, 512, 512, 512, 256, 128, 64, 32, 16), conditional=False):
        super(ProgressiveGenerator, self).__init__()
        self.n_hidden = n_hidden
        self.max_stage = (len(channel_evolution) - 1) * 2
        with self.init_scope():
            self.c0 = EqualizedConv2d(n_hidden, ch, 4, 1, 3)
            if conditional:
                self.c1 = EqualizedConv2d(ch, ch, 3, 1, 1)
            else:
                self.c1 = EqualizedConv2d(ch, ch, 3, 1, 1)
            bs = [
                chainer.Link()  # dummy
            ]
            outs = [

            ]
            if conditional:
                outs.append(EqualizedConv2d(channel_evolution[0], 3, 1, 1, 0))
            else:
                outs.append(EqualizedConv2d(channel_evolution[0], 3, 1, 1, 0))

            for i in range(1, len(channel_evolution)):
                if conditional:
                    bs.append(GeneratorBlock(channel_evolution[i - 1] * 2, channel_evolution[i]))
                    outs.append(EqualizedConv2d(channel_evolution[i], 3, 1, 1, 0))
                else:
                    bs.append(GeneratorBlock(channel_evolution[i - 1], channel_evolution[i]))
                    outs.append(EqualizedConv2d(channel_evolution[i], 3, 1, 1, 0))
            self.bs = chainer.ChainList(*bs)
            self.outs = chainer.ChainList(*outs)
Ejemplo n.º 14
0
 def test_has_uninitialized_params(self):
     l = chainer.Link(y=2)
     self.assertFalse(l.has_uninitialized_params)
     l.add_uninitialized_param('x')
     self.assertTrue(l.has_uninitialized_params)
     l.add_param('x', (2, 3))
     self.assertFalse(l.has_uninitialized_params)
Ejemplo n.º 15
0
 def setUp(self):
     self.target = chainer.Link()
     self.iterator = DummyIterator([(numpy.array(1), numpy.array(2))])
     self.optimizer = DummyOptimizer()
     self.optimizer.setup(self.target)
     self.updater = training.updaters.StandardUpdater(
         self.iterator, self.optimizer)
Ejemplo n.º 16
0
def test():
    model = chainer.Link()
    dataset = [((numpy.ones(
        (2, 5, 5)) * i).astype(numpy.float32), numpy.int32(0))
               for i in range(100)]

    batch_size = 5
    devices = (0, )
    iters = [
        chainer.iterators.SerialIterator(i, batch_size)
        for i in chainer.datasets.split_dataset_n_random(
            dataset, len(devices))
    ]
    optimizer = chainer.optimizers.SGD(lr=1.0)
    optimizer.setup(model)

    # Initialize CUDA context.
    cuda.cupy.cuda.runtime.runtimeGetVersion()

    try:
        mpu.MultiprocessParallelUpdater(iters, optimizer, devices=devices)
    except RuntimeError as e:
        assert 'CUDA context' in str(e)
        return

    assert False
Ejemplo n.º 17
0
    def check_call(self, gpu, label_key, args, kwargs, model_args,
                   model_kwargs):
        init_kwargs = {'label_key': label_key}
        if self.accfun is not None:
            init_kwargs['accfun'] = self.accfun
        link = links.Classifier(chainer.Link(), **init_kwargs)

        if gpu:
            xp = cuda.cupy
            link.to_gpu()
        else:
            xp = numpy

        link.compute_accuracy = self.compute_accuracy

        y = chainer.Variable(self.y)
        link.predictor = mock.MagicMock(return_value=y)

        loss = link(*args, **kwargs)
        link.predictor.assert_called_with(*model_args, **model_kwargs)

        self.assertTrue(hasattr(link, 'y'))
        self.assertIsNotNone(link.y)

        self.assertTrue(hasattr(link, 'loss'))
        xp.testing.assert_allclose(link.loss.data, loss.data)

        self.assertTrue(hasattr(link, 'accuracy'))
        if self.compute_accuracy:
            self.assertIsNotNone(link.accuracy)
        else:
            self.assertIsNone(link.accuracy)
Ejemplo n.º 18
0
    def test_device(self, model_initial_backend_config, model_backend_config,
                    input_backend_config):
        model_initial_device = model_initial_backend_config.device
        device = model_backend_config.device
        input_device = input_backend_config.device

        model = chainer.Link()
        model.to_device(model_initial_device)
        optimizer = DummyOptimizer()
        optimizer.setup(model)
        iterator = DummyIterator([numpy.array(1), numpy.array(2)])

        updater = training.updaters.StandardUpdater(iterator,
                                                    optimizer,
                                                    device=device,
                                                    input_device=input_device)

        assert updater.device is device
        assert updater.input_device is input_device

        # Check the model device.
        assert model.device == device

        updater.update_core()

        assert optimizer.update.call_count == 1
        args, kwargs = optimizer.update.call_args
        assert len(args) == 2
        assert len(kwargs) == 0
        loss, v1 = args

        # Check the input device.
        assert backend.get_device_from_array(v1) == input_device
def test_link_as_torch_model():
    # initialized parameter
    a_arr = numpy.ones((3,  2), 'float32')
    a_chainer_param = chainer.Parameter(a_arr)
    # 0-size parameter
    b_arr = numpy.ones((2, 0, 1), 'float32')
    b_chainer_param = chainer.Parameter(b_arr)

    link = chainer.Link()
    with link.init_scope():
        link.a = a_chainer_param
        link.b = b_chainer_param

    # Conversion
    torched = cpm.LinkAsTorchModel(link)
    params = list(torched.parameters())
    assert len(params) == 2
    assert isinstance(params[0], torch.nn.Parameter)
    assert isinstance(params[1], torch.nn.Parameter)
    assert params[0].shape == (3, 2)
    assert params[1].shape == (2, 0, 1)
    assert (params[0].data.numpy() == numpy.ones((3, 2))).all()

    # Test memory sharing
    params[0].data[...] = torch.tensor(numpy.arange(6).reshape((3, 2)))
    assert (a_chainer_param.array == numpy.arange(6).reshape((3, 2))).all()
Ejemplo n.º 20
0
 def setUp(self):
     x_shape_0 = 2
     x_shape_1 = numpy.int64(3)
     self.link = chainer.Link(x=((x_shape_0, x_shape_1), 'd'), y=2)
     self.p = numpy.array([1, 2, 3], dtype='f')
     self.link.add_persistent('p', self.p)
     self.link.name = 'a'
Ejemplo n.º 21
0
 def __init__(self,
              ch=512,
              pooling_comp=1.0,
              channel_evolution=(512, 512, 512, 512, 256, 128, 64, 32, 16),
              first_channel=3,
              use_both_conditional_and_latent=False):
     super().__init__()
     self.use_both_conditional_and_latent = use_both_conditional_and_latent
     self.max_stage = (len(channel_evolution) - 1) * 2
     self.pooling_comp = pooling_comp  # compensation of ave_pool is 0.5-Lipshitz
     with self.init_scope():
         ins = [
             EqualizedConv2d(first_channel, channel_evolution[0], 1, 1, 0)
         ]
         bs = [
             chainer.Link()  # dummy
         ]
         for i in range(1, len(channel_evolution)):
             ins.append(
                 EqualizedConv2d(first_channel, channel_evolution[i], 1, 1,
                                 0))
             bs.append(
                 DiscriminatorBlock(channel_evolution[i],
                                    channel_evolution[i - 1], pooling_comp))
         self.ins = chainer.ChainList(*ins)
         self.bs = chainer.ChainList(*bs)
         self.out0 = EqualizedConv2d(ch + 1, ch, 3, 1, 1)
         self.out1 = EqualizedConv2d(ch, ch, 4, 1, 0)
         self.out2 = EqualizedLinear(ch, 1)
Ejemplo n.º 22
0
    def check_call(self, gpu, label_key, args, kwargs, model_args,
                   model_kwargs, metrics_fun, compute_metrics):
        init_kwargs = {'label_key': label_key}
        if metrics_fun is not None:
            init_kwargs['metrics_fun'] = metrics_fun
        link = Classifier(chainer.Link(), **init_kwargs)

        if gpu:
            xp = cuda.cupy
            link.to_gpu()
        else:
            xp = numpy

        link.compute_metrics = compute_metrics

        y = chainer.Variable(self.y)
        link.predictor = mock.MagicMock(return_value=y)

        loss = link(*args, **kwargs)
        link.predictor.assert_called_with(*model_args, **model_kwargs)

        assert hasattr(link, 'y')
        assert link.y is not None

        assert hasattr(link, 'loss')
        xp.testing.assert_allclose(link.loss.data, loss.data)

        assert hasattr(link, 'metrics')
        if compute_metrics:
            assert link.metrics is not None
        else:
            assert link.metrics is None
Ejemplo n.º 23
0
    def test_copyparams(self):
        self.link.x.grad.fill(0)
        self.link.y.grad.fill(1)
        self.link.u.initialize((2, 3))
        self.link.u.data.fill(0)
        self.link.u.grad.fill(1)
        self.link.v.zerograd()
        gx = self.link.x.grad.copy()
        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.x.data.fill(2)
        l.x.grad.fill(3)
        l.y.data.fill(4)
        l.y.grad.fill(5)
        l.u.data.fill(6)
        l.u.grad.fill(7)
        l.v.data.fill(8)
        l.v.grad.fill(9)

        self.link.copyparams(l)
        numpy.testing.assert_array_equal(self.link.x.data, l.x.data)
        numpy.testing.assert_array_equal(self.link.x.grad, gx)
        numpy.testing.assert_array_equal(self.link.y.data, l.y.data)
        numpy.testing.assert_array_equal(self.link.y.grad, gy)
        numpy.testing.assert_array_equal(self.link.u.data, l.u.data)
        numpy.testing.assert_array_equal(self.link.u.grad, gu)
        numpy.testing.assert_array_equal(self.link.v.data, l.v.data)
        numpy.testing.assert_array_equal(self.link.v.grad, 0)
Ejemplo n.º 24
0
    def test_addgrads(self):
        l = chainer.Link()
        with l.init_scope():
            l.x = chainer.Parameter(shape=(2, 3),
                                    initializer=initializers.NaN('d'))
            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)

        self.link.x.grad.fill(-1)
        self.link.y.grad.fill(-2)
        self.link.u.cleargrad()

        self.link.addgrads(l)

        gx_expect = numpy.zeros_like(l.x.grad)
        gy_expect = numpy.zeros_like(l.y.grad)
        gu_expect = l.u.grad
        numpy.testing.assert_array_equal(self.link.x.grad, gx_expect)
        numpy.testing.assert_array_equal(self.link.y.grad, gy_expect)
        numpy.testing.assert_array_equal(self.link.u.grad, gu_expect)
        self.assertIsNone(self.link.v.grad, None)
Ejemplo n.º 25
0
    def test_copyparams(self):
        self.link.x.grad.fill(0)
        self.link.y.grad.fill(1)
        self.link.u.initialize((2, 3))
        self.link.u.data.fill(0)
        self.link.u.grad.fill(1)
        self.link.v.cleargrad()
        gx = self.link.x.grad.copy()
        gy = self.link.y.grad.copy()
        gu = self.link.u.grad.copy()

        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)
        l.y.grad.fill(5)
        l.u.data.fill(6)
        l.u.grad.fill(7)
        l.v.data.fill(8)
        l.v.grad.fill(9)

        self.link.copyparams(l)
        numpy.testing.assert_array_equal(self.link.x.data, l.x.data)
        numpy.testing.assert_array_equal(self.link.x.grad, gx)
        numpy.testing.assert_array_equal(self.link.y.data, l.y.data)
        numpy.testing.assert_array_equal(self.link.y.grad, gy)
        numpy.testing.assert_array_equal(self.link.u.data, l.u.data)
        numpy.testing.assert_array_equal(self.link.u.grad, gu)
        numpy.testing.assert_array_equal(self.link.v.data, l.v.data)
        numpy.testing.assert_array_equal(self.link.v.grad, None)
Ejemplo n.º 26
0
    def __init__(self,
                 n_layers,
                 in_size,
                 out_size,
                 dropout_rate,
                 name="",
                 use_cudnn=True):
        weights = []
        direction = 1  # ここでは,からなず一方向ずつ構築するので1にする
        t_name = name
        if name is not "":
            t_name = '%s_' % name

        for i in range(n_layers):
            for di in range(direction):
                weight = chainer.Link()
                for j in range(8):
                    if i == 0 and j < 4:
                        w_in = in_size
                    elif i > 0 and j < 4:
                        w_in = out_size * direction
                    else:
                        w_in = out_size
                    weight.add_param('%sw%d' % (t_name, j), (out_size, w_in))
                    weight.add_param('%sb%d' % (t_name, j), (out_size, ))
                    getattr(weight, '%sw%d' %
                            (t_name, j)).data[...] = np.random.normal(
                                0, np.sqrt(1. / w_in), (out_size, w_in))
                    getattr(weight, '%sb%d' % (t_name, j)).data[...] = 0
                weights.append(weight)

        super(NStepLSTMpp, self).__init__(*weights)

        self.n_layers = n_layers
        self.dropout_rate = dropout_rate
        self.use_cudnn = use_cudnn
        self.out_size = out_size
        self.direction = direction
        self.ws = [[
            getattr(w, '%sw0' % t_name),
            getattr(w, '%sw1' % t_name),
            getattr(w, '%sw2' % t_name),
            getattr(w, '%sw3' % t_name),
            getattr(w, '%sw4' % t_name),
            getattr(w, '%sw5' % t_name),
            getattr(w, '%sw6' % t_name),
            getattr(w, '%sw7' % t_name)
        ] for w in self]
        self.bs = [[
            getattr(w, '%sb0' % t_name),
            getattr(w, '%sb1' % t_name),
            getattr(w, '%sb2' % t_name),
            getattr(w, '%sb3' % t_name),
            getattr(w, '%sb4' % t_name),
            getattr(w, '%sb5' % t_name),
            getattr(w, '%sb6' % t_name),
            getattr(w, '%sb7' % t_name)
        ] for w in self]
Ejemplo n.º 27
0
 def setUp(self):
     x_shape_0 = 2
     x_shape_1 = numpy.int64(3)
     self.link = chainer.Link(x=((x_shape_0, x_shape_1), 'd'), y=2)
     self.p = numpy.array([1, 2, 3], dtype='f')
     self.link.add_persistent('p', self.p)
     self.link.name = 'a'
     if cuda.available:
         self.current_device_id = cuda.cupy.cuda.get_device_id()
Ejemplo n.º 28
0
 def test_copyparams_uninitialized(self):
     l = chainer.Link(x=(2, 3))
     l.add_uninitialized_param('y')
     self.link.x.data.fill(2)
     self.link.y.data.fill(4)
     l.copyparams(self.link)
     numpy.testing.assert_array_equal(l.x.data, self.link.x.data)
     self.assertTrue(hasattr(l, 'y'))
     numpy.testing.assert_array_equal(l.y.data, self.link.y.data)
Ejemplo n.º 29
0
    def test_addgrads(self):
        l1 = chainer.Link(x=(2, 3))
        l2 = chainer.Link(x=2)
        l3 = chainer.Link(x=3)
        c1 = chainer.ChainList(l1, l2)
        c2 = chainer.ChainList(c1, l3)
        l1.x.grad.fill(1)
        l2.x.grad.fill(2)
        l3.x.grad.fill(3)

        self.l1.x.grad.fill(-1)
        self.l2.x.grad.fill(-2)
        self.l3.x.grad.fill(-3)

        self.c2.addgrads(c2)
        numpy.testing.assert_array_equal(self.l1.x.grad, numpy.zeros((2, 3)))
        numpy.testing.assert_array_equal(self.l2.x.grad, numpy.zeros(2))
        numpy.testing.assert_array_equal(self.l3.x.grad, numpy.zeros(3))
Ejemplo n.º 30
0
 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.serialize(serializer)
     self.assertEqual(serializer.call_count, 1)
     serializer.assert_any_call('x', None)
     self.assertIsInstance(l.x.data, numpy.ndarray)
     numpy.testing.assert_array_equal(l.x.data, ret)