def _test_slice(test_case, device): np_arr = np.random.randn(3, 6, 9).astype(np.float32) x = flow.Tensor(np_arr, device=flow.device(device)) tup_list = [[None, None, None], [0, 5, 2], [0, 6, 3]] y = flow.slice(x, slice_tup_list=tup_list) tmp = np_arr[0:3, 0:5, 0:6] np_out = tmp[::1, ::2, ::3] test_case.assertTrue(np.array_equal(y.numpy(), np_out))
def _test_slice_backward(test_case, device): np_arr = np.random.randn(3, 6, 9).astype(np.float32) x = flow.Tensor(np_arr, device=flow.device(device), requires_grad=True) tup_list = [[None, None, None], [0, 5, 2], [0, 6, 3]] y = flow.slice(x, slice_tup_list=tup_list) z = y.sum() z.backward() np_grad = np.zeros((3, 6, 9)) np_grad[0:3, 0:5, 0:6][::1, ::2, ::3] = 1 test_case.assertTrue(np.array_equal(x.grad.numpy(), np_grad))