def _test_pow(test_case, shape, device): input = flow.Tensor(np.random.randn(*shape), dtype=flow.float32, device=flow.device(device)) of_out = flow.pow(input, 2.1) np_out = np.power(input.numpy(), 2.1) test_case.assertTrue( np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5, equal_nan=True))
def _test_pow_elementwise_impl(test_case, shape, scalar, device): np_input_x = 10 * np.random.rand(*shape) np_input_y = np.random.randint(1, 3, shape) + np.random.randn(*shape) of_input_x = flow.Tensor(np_input_x, dtype=flow.float32, device=flow.device(device)) of_input_y = flow.Tensor(np_input_y, dtype=flow.float32, device=flow.device(device)) of_out = flow.pow(of_input_x, of_input_y) np_out = np.power(np_input_x, np_input_y) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))
def _test_pow_backward(test_case, shape, device): x = flow.Tensor( np.random.randn(*shape), dtype=flow.float32, device=flow.device(device), requires_grad=True, ) y = flow.pow(x, 2.34) z = y.sum() z.backward() np_grad = 2.34 * x.numpy()**(2.34 - 1) test_case.assertTrue( np.allclose(x.grad.numpy(), np_grad, 1e-5, 1e-5, equal_nan=True))
def test_y_grad(): of_input_x = flow.Tensor( np_input_x, dtype=flow.float32, device=flow.device(device) ) of_input_y = flow.Tensor( np_input_y, dtype=flow.float32, device=flow.device(device), requires_grad=True, ) of_out = flow.pow(of_input_x, of_input_y) of_out_sum = of_out.sum() of_out_sum.backward() test_case.assertTrue( np.allclose(of_input_y.grad.numpy(), np_y_grad, 1e-4, 1e-4) )
def test_pow(test_case): input = flow.Tensor(np.array([1, 2, 3, 4, 5, 6]), dtype=flow.float32) of_out = flow.pow(input, 2.1) np_out = np.power(input.numpy(), 2.1) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5))
def _test_pow_scalar_impl(test_case, shape, scalar, device): np_input = 10 * np.random.rand(*shape) of_input = flow.Tensor(np_input, dtype=flow.float32, device=flow.device(device)) of_out = flow.pow(of_input, scalar) np_out = np.power(np_input, scalar) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))