def test_shape(self): x = Variable(np.random.randn(1, 2, 3)) y = x.reshape((6, )) y.backward(retain_grad=True) self.assertEqual(x.shape, x.grad.shape) self.assertEqual(y.shape, (6, )) a = Variable(np.array([[1, 2, 3], [4, 5, 6]])) b = F.transpose(a) c = a.T b.backward() self.assertEqual(a.grad.shape, a.shape) self.assertEqual(b.shape, c.shape)
def test_reshape(self): x = Variable(np.arange(12).reshape(3, 4)) y = x.reshape((6, 2)) self.assertEqual(y.shape, (6, 2))
def test_reshape_method_of_variable(self): x = Variable(np.array([1, 2, 3, 4, 5, 6])) y = x.reshape(2, 3) y.backward() assert_array_equal(y.data, np.array([[1, 2, 3], [4, 5, 6]])) assert_array_equal(x.grad.data, np.array([1, 1, 1, 1, 1, 1]))