Exemple #1
0
 def check_broadcast_backward(self, xp):
     x = xp.arange(12, dtype=self.dtype).reshape((3, 4)).copy()
     v = Variable(x)
     y = as_strided(v, (2, 3, 4), (0, 4, 1), 0)
     y.grad = xp.ones((2, 3, 4), dtype=self.dtype)
     with self.assertRaises(TypeError):
         gx, = grad((y,), (v,))
Exemple #2
0
 def check_unstride_backward(self, xp):
     x = xp.arange(12, dtype=self.dtype).reshape((3, 4))[::-1]
     v = chainer.Variable(x)
     y = F.as_strided(v, (12, ), (1, ), 0)
     y.grad = xp.ones((12, ), dtype=self.dtype)
     gx, = chainer.grad((y, ), (v, ))
     testing.assert_allclose(gx.array, xp.ones(x.shape, dtype=self.dtype))
Exemple #3
0
 def check_unstride_backward(self, xp):
     x = xp.arange(12, dtype=self.dtype).reshape((3, 4))[::-1]
     v = Variable(x)
     y = as_strided(v, (12,), (1,), 0)
     y.grad = xp.ones((12,), dtype=self.dtype)
     with self.assertRaises(TypeError):
         gx, = grad((y,), (v,))
Exemple #4
0
 def check_flip_backward(self, xp):
     x = xp.arange(4, dtype=self.dtype)
     v = chainer.Variable(x)
     y = F.as_strided(v, (4, ), (-1, ), 3)
     y.grad = xp.ones((4, ), dtype=self.dtype)
     with self.assertRaises(TypeError):
         gx, = chainer.grad((y, ), (v, ))
Exemple #5
0
 def check_flip_backward(self, xp):
     x = xp.arange(4, dtype=self.dtype)
     v = Variable(x)
     y = as_strided(v, (4,), (-1,), 3)
     y.grad = xp.ones((4,), dtype=self.dtype)
     with self.assertRaises(TypeError):
         gx, = grad((y,), (v,))
Exemple #6
0
 def check_flip_backward(self, xp):
     x = xp.arange(4, dtype=self.dtype)
     v = Variable(x)
     y = as_strided(v, (4,), (-1,), 3)
     y.grad = xp.ones((4,), dtype=self.dtype)
     gx, = grad((y,), (v,))
     testing.assert_allclose(gx.array, xp.ones((4,), dtype=self.dtype))
Exemple #7
0
 def check_unstride_backward(self, xp):
     x = xp.arange(12, dtype=self.dtype).reshape((3, 4))[::-1]
     v = Variable(x)
     y = as_strided(v, (12,), (1,), 0)
     y.grad = xp.ones((12,), dtype=self.dtype)
     gx, = grad((y,), (v,))
     testing.assert_allclose(gx.array, xp.ones(x.shape, dtype=self.dtype))
Exemple #8
0
 def check_broadcast_backward(self, xp):
     x = xp.arange(12, dtype=self.dtype).reshape((3, 4)).copy()
     v = chainer.Variable(x)
     y = F.as_strided(v, (2, 3, 4), (0, 4, 1), 0)
     y.grad = xp.ones((2, 3, 4), dtype=self.dtype)
     with self.assertRaises(TypeError):
         gx, = chainer.grad((y, ), (v, ))
Exemple #9
0
 def check_flip_backward(self, xp):
     x = xp.arange(4, dtype=self.dtype)
     v = chainer.Variable(x)
     y = F.as_strided(v, (4, ), (-1, ), 3)
     y.grad = xp.ones((4, ), dtype=self.dtype)
     gx, = chainer.grad((y, ), (v, ))
     testing.assert_allclose(gx.array, xp.ones((4, ), dtype=self.dtype))
Exemple #10
0
 def check_unstride_backward(self, xp):
     x = xp.arange(12, dtype=self.dtype).reshape((3, 4))[::-1]
     v = chainer.Variable(x)
     y = F.as_strided(v, (12, ), (1, ), 0)
     y.grad = xp.ones((12, ), dtype=self.dtype)
     with self.assertRaises(TypeError):
         gx, = chainer.grad((y, ), (v, ))
Exemple #11
0
 def check_broadcast_backward(self, xp):
     x = xp.arange(12, dtype=self.dtype).reshape((3, 4)).copy()
     v = chainer.Variable(x)
     y = F.as_strided(v, (2, 3, 4), (0, 4, 1), 0)
     y.grad = xp.ones((2, 3, 4), dtype=self.dtype)
     gx, = chainer.grad((y, ), (v, ))
     testing.assert_allclose(gx.array,
                             xp.ones(x.shape, dtype=self.dtype) * 2)
Exemple #12
0
 def check_broadcast_backward(self, xp):
     x = xp.arange(12, dtype=self.dtype).reshape((3, 4)).copy()
     v = Variable(x)
     y = as_strided(v, (2, 3, 4), (0, 4, 1), 0)
     y.grad = xp.ones((2, 3, 4), dtype=self.dtype)
     gx, = grad((y,), (v,))
     testing.assert_allclose(gx.array,
                             xp.ones(x.shape, dtype=self.dtype) * 2)
Exemple #13
0
 def check_general_stride(self, xp):
     x = _stride_array(xp.arange(8, dtype=self.dtype), (3, 3), (-1, 2), 3)
     # [[3., 5., 7.], [2., 4., 6.], [1., 3., 5.]]
     v = chainer.Variable(x)
     y = F.as_strided(v, (3, 3), (1, 2), 0)
     # [[0., 2., 4.], [1., 3., 5.,], [2., 4., 6.]]
     y_expected = _stride_array(xp.arange(8, dtype=self.dtype), (3, 3),
                                (1, 2), 0)
     assert (y.array == y_expected).all()
Exemple #14
0
 def check_general_stride_backward(self, xp):
     x = _stride_array(xp.arange(8, dtype=self.dtype), (3, 3), (-1, 2), 3)
     # [[3., 5., 7.], [2., 4., 6.], [1., 3., 5.]]
     v = chainer.Variable(x)
     y = F.as_strided(v, (3, 3), (1, 2), 0)
     # [[0., 2., 4.], [1., 3., 5.,], [2., 4., 6.]]
     y.grad = xp.ones(y.shape, dtype=self.dtype)
     with self.assertRaises(TypeError):
         gx, = chainer.grad((y, ), (v, ))
Exemple #15
0
 def check_general_stride(self, xp):
     x = _stride_array(xp.arange(8, dtype=self.dtype), (3, 3), (-1, 2), 3)
     # [[3., 5., 7.], [2., 4., 6.], [1., 3., 5.]]
     v = Variable(x)
     y = as_strided(v, (3, 3), (1, 2), 0)
     # [[0., 2., 4.], [1., 3., 5.,], [2., 4., 6.]]
     y_expected = _stride_array(xp.arange(8, dtype=self.dtype),
                                (3, 3), (1, 2), 0)
     assert (y.array == y_expected).all()
Exemple #16
0
 def check_general_stride_backward(self, xp):
     x = _stride_array(xp.arange(8, dtype=self.dtype), (3, 3), (-1, 2), 3)
     # [[3., 5., 7.], [2., 4., 6.], [1., 3., 5.]]
     v = Variable(x)
     y = as_strided(v, (3, 3), (1, 2), 0)
     # [[0., 2., 4.], [1., 3., 5.,], [2., 4., 6.]]
     y.grad = xp.ones(y.shape, dtype=self.dtype)
     with self.assertRaises(TypeError):
         gx, = grad((y,), (v,))
Exemple #17
0
 def check_general_stride_backward(self, xp):
     x = _stride_array(xp.arange(8, dtype=self.dtype), (3, 3), (-1, 2), 3)
     # [[3., 5., 7.], [2., 4., 6.], [1., 3., 5.]]
     v = chainer.Variable(x)
     y = F.as_strided(v, (3, 3), (1, 2), 0)
     # [[0., 2., 4.], [1., 3., 5.,], [2., 4., 6.]]
     y.grad = xp.ones(y.shape, dtype=self.dtype)
     gx, = chainer.grad((y, ), (v, ))
     testing.assert_allclose(
         gx.array,
         xp.array([[0.5, 0.5, 0.], [2., 2., 1.], [1., 0.5, 0.5]],
                  dtype=self.dtype))
Exemple #18
0
 def check_general_stride_backward(self, xp):
     x = _stride_array(xp.arange(8, dtype=self.dtype), (3, 3), (-1, 2), 3)
     # [[3., 5., 7.], [2., 4., 6.], [1., 3., 5.]]
     v = Variable(x)
     y = as_strided(v, (3, 3), (1, 2), 0)
     # [[0., 2., 4.], [1., 3., 5.,], [2., 4., 6.]]
     y.grad = xp.ones(y.shape, dtype=self.dtype)
     gx, = grad((y,), (v,))
     testing.assert_allclose(gx.array,
                             xp.array([
                                 [0.5, 0.5, 0.],
                                 [2., 2., 1.],
                                 [1., 0.5, 0.5]
                             ], dtype=self.dtype)
                             )
Exemple #19
0
 def check_broadcast_forward(self, xp):
     x = xp.arange(12, dtype=self.dtype).reshape((3, 4)).copy()
     v = chainer.Variable(x)
     y = F.as_strided(v, (2, 3, 4), (0, 4, 1), 0)
     y_expected = _broadcast_to(xp, x, (2, 3, 4))
     testing.assert_allclose(y.array, y_expected)
Exemple #20
0
 def check_unstride_forward(self, xp):
     x = xp.arange(12, dtype=self.dtype).reshape((3, 4))[::-1]
     v = Variable(x)
     y = as_strided(v, (12,), (1,), 0)
     y_expected = xp.arange(12, dtype=self.dtype)
     testing.assert_allclose(y.array, y_expected)
Exemple #21
0
 def check_broadcast_forward(self, xp):
     x = xp.arange(12, dtype=self.dtype).reshape((3, 4)).copy()
     v = Variable(x)
     y = as_strided(v, (2, 3, 4), (0, 4, 1), 0)
     y_expected = _broadcast_to(xp, x, (2, 3, 4))
     testing.assert_allclose(y.array, y_expected)
Exemple #22
0
 def check_unstride_forward(self, xp):
     x = xp.arange(12, dtype=self.dtype).reshape((3, 4))[::-1]
     v = chainer.Variable(x)
     y = F.as_strided(v, (12, ), (1, ), 0)
     y_expected = xp.arange(12, dtype=self.dtype)
     testing.assert_allclose(y.array, y_expected)
Exemple #23
0
 def check_flip_forward(self, xp):
     x = xp.arange(4, dtype=self.dtype)
     v = chainer.Variable(x)
     y = F.as_strided(v, (4, ), (-1, ), 3)
     y_expected = x[::-1]
     testing.assert_allclose(y.array, y_expected)
Exemple #24
0
 def check_flip_forward(self, xp):
     x = xp.arange(4, dtype=self.dtype)
     v = Variable(x)
     y = as_strided(v, (4,), (-1,), 3)
     y_expected = x[::-1]
     testing.assert_allclose(y.array, y_expected)