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,))
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))
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,))
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, ))
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,))
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))
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))
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, ))
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))
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, ))
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)
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)
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()
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, ))
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()
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,))
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))
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) )
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)
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)
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)
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)
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)
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)