def test_square(test_case): input_arr = np.random.randn(9, 4, 5, 6) np_out = np.square(input_arr) x = flow.Tensor(input_arr) of_out = flow.square(x) test_case.assertTrue( np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5, equal_nan=True))
def _test_square(test_case, shape, device): np_arr = np.random.randn(*shape) np_out = np.square(np_arr) x = flow.Tensor(np_arr, device=flow.device(device)) of_out = flow.square(x) test_case.assertTrue( np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5, equal_nan=True))
def _test_square_backward(test_case, shape, device): np_arr = np.random.randn(*shape) np_out = np.square(np_arr) x = flow.Tensor(np_arr, device=flow.device(device), requires_grad=True) y = flow.square(x) z = y.sum() z.backward() np_grad = 2 * np_arr test_case.assertTrue( np.allclose(x.grad.numpy(), np_grad, 1e-5, 1e-5, equal_nan=True))