def test_log_nan_value(test_case): arr = np.array([-0.7168, -0.5471, -0.8933, -1.4428, -0.1190]) input = flow.Tensor(arr, dtype=flow.float32) np_out = np.full((5, ), np.nan) of_out = flow.log(input) test_case.assertTrue( np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5, equal_nan=True))
def _test_log(test_case, shape, device): np_arr = np.abs(np.random.randn(*shape)) input = flow.Tensor(np_arr, dtype=flow.float32, device=flow.device(device)) of_out = flow.log(input) np_out = np.log(np_arr) test_case.assertTrue( np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5, equal_nan=True))
def test_log(test_case): input = flow.Tensor(np.random.randn(2, 3, 4, 5), dtype=flow.float32) of_out = flow.log(input) np_out = np.log(input.numpy()) test_case.assertTrue( np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5, equal_nan=True)) test_case.assertTrue( np.allclose(input.log().numpy(), np_out, equal_nan=True))
def _test_log_backward(test_case, shape, device): x = flow.Tensor( np.random.randn(*shape), dtype=flow.float32, device=flow.device(device), requires_grad=True, ) y = flow.log(x) z = y.sum() z.backward() np_grad = 1 / x.numpy() test_case.assertTrue( np.allclose(x.grad.numpy(), np_grad, 1e-5, 1e-5, equal_nan=True))