def test_get_item_backward(self): x = Variable(np.array([[1, 2, 3], [4, 5, 6]])) y = F.get_item(x, 1) y.backward() expecteds = Variable(np.array([[0, 0, 0], [1, 1, 1]])) for result, expected in zip(x.grad.data, expecteds.data): assert all(result == expected)
def test_forward1(self): x_data = np.arange(12).reshape((2, 2, 3)) x = Variable(x_data) y = F.get_item(x, 0) self.assertTrue(array_allclose(y.data, x_data[0]))
def test_backward2(self): x_data = np.arange(12).reshape(4, 3) slices = slice(1, 3) f = lambda x: F.get_item(x, slices) gradient_check(f, x_data)
def test_backward1(self): x_data = np.array([[1, 2, 3], [4, 5, 6]]) slices = 1 f = lambda x: F.get_item(x, slices) gradient_check(f, x_data)
def test_forward3(self): x_data = np.arange(12).reshape((2, 2, 3)) x = Variable(x_data) y = F.get_item(x, (Ellipsis, 2)) self.assertTrue(array_allclose(y.data, x_data[..., 2]))
def test_forward2(self): x_data = np.arange(12).reshape((2, 2, 3)) x = Variable(x_data) y = F.get_item(x, (0, 0, slice(0, 2, 1))) self.assertTrue(array_allclose(y.data, x_data[0, 0, 0:2:1]))
def test_backward(self): x = Variable(np.array([[1, 2, 3], [4, 5, 6]])) ind = [1, 1, 0] y = get_item(x, ind) y.backward() assert_equal(x.grad.data, [[1, 1, 1], [2, 2, 2]])
def test_forward(self): x = Variable(np.array([[1, 2, 3], [4, 5, 6]])) ind = [1, 1, 0] y = get_item(x, ind) assert_equal(y.data, [[4, 5, 6], [4, 5, 6], [1, 2, 3]])
def test_backward2(self): x_data = np.arange(12).reshape(4, 3) slices = slice(1, 3) y_grad = np.ones(x_data[slices].shape) f = lambda x: F.get_item(x, slices) check_backward(f, x_data, y_grad=y_grad)
def test_backward1(self): x_data = np.array([[1, 2, 3], [4, 5, 6]]) slices = 1 y_grad = np.ones(x_data[slices].shape) f = lambda x: F.get_item(x, slices) check_backward(f, x_data, y_grad=y_grad)
def test_get_item_forward(self): x = Variable(np.array([[1, 2, 3], [4, 5, 6]])) y = F.get_item(x, 1) excepted = Variable(np.array([4, 5, 6])) assert all(y.data == excepted.data)