Example #1
0
 def f(h, x):
     # As forward computation is executed multiple times in
     # check_double_backward, use a fixed flag.
     xp_str = 'numpy' if xp is numpy else 'cupy'
     with mock.patch('{}.random.rand'.format(xp_str),
                     return_value=flag_x) as mock_rand:
         y = functions.zoneout(h, x, self.ratio)
         mock_rand.assert_called_once_with(*x.shape)
     return y * y
Example #2
0
 def f(h, x):
     # As forward computation is executed multiple times in
     # check_double_backward, use a fixed flag.
     xp_str = 'numpy' if xp is numpy else 'cupy'
     with mock.patch(
             '{}.random.rand'.format(xp_str),
             return_value=flag_x) as mock_rand:
         y = functions.zoneout(h, x, self.ratio)
         mock_rand.assert_called_once_with(*x.shape)
     return y
Example #3
0
 def check_forward(self, h_data, x_data):
     h = chainer.Variable(h_data)
     x = chainer.Variable(x_data)
     h_next = functions.zoneout(h, x, self.ratio)
     if self.ratio == 0:
         h_next_expect = x_data
     elif self.ratio == 1:
         h_next_expect = h_data
     else:
         h_next_expect = _zoneout(h_data, x_data, h_next.creator)
     testing.assert_allclose(h_next.data, h_next_expect)
Example #4
0
 def check_forward(self, h_data, x_data):
     h = chainer.Variable(h_data)
     x = chainer.Variable(x_data)
     h_next = functions.zoneout(h, x, self.ratio)
     if self.ratio == 0:
         h_next_expect = x_data
     elif self.ratio == 1:
         h_next_expect = h_data
     else:
         h_next_expect = _zoneout(h_data, x_data, h_next.creator)
     testing.assert_allclose(h_next.data, h_next_expect)
Example #5
0
    def check_backward(self, h_data, x_data, y_grad):
        h = chainer.Variable(h_data)
        x = chainer.Variable(x_data)
        y = functions.zoneout(h, x, self.ratio)
        d = {'creator': y.creator}
        y.grad = y_grad
        y.backward()

        def f():
            creator = d['creator']
            y = _zoneout(h_data, x_data, creator)
            return y,
        gh, gx, = gradient_check.numerical_grad(f, (h.data, x.data,),
                                                (y.grad,))
        testing.assert_allclose(gh, h.grad, atol=1e-3)
        testing.assert_allclose(gx, x.grad, atol=1e-3)
Example #6
0
    def check_backward(self, h_data, x_data, y_grad):
        h = chainer.Variable(h_data)
        x = chainer.Variable(x_data)
        y = functions.zoneout(h, x, self.ratio)
        d = {'creator': y.creator}
        y.grad = y_grad
        y.backward()

        def f():
            creator = d['creator']
            y = _zoneout(h_data, x_data, creator)
            return y,
        gh, gx, = gradient_check.numerical_grad(f, (h.data, x.data,),
                                                (y_grad,))
        testing.assert_allclose(gh, h.grad, atol=1e-3)
        testing.assert_allclose(gx, x.grad, atol=1e-3)