def test_cos(test_case): input = flow.Tensor(np.random.randn(1, 3, 6), dtype=flow.float32) of_out = flow.cos(input) np_out = np.cos(input.numpy()) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5)) test_case.assertTrue( np.allclose(input.cos().numpy(), np_out, 1e-5, 1e-5))
def _test_cos(test_case, shape, device): input = flow.Tensor(np.random.randn(*shape), dtype=flow.float32, device=flow.device(device)) of_out = flow.cos(input) np_out = np.cos(input.numpy()) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5))
def _test_cos_backward(test_case, shape, device): x = flow.Tensor( np.random.randn(*shape), dtype=flow.float32, device=flow.device(device), requires_grad=True, ) y = flow.cos(x) z = y.sum() z.backward() np_grad = -np.sin(x.numpy()) test_case.assertTrue(np.allclose(x.grad.numpy(), np_grad, 1e-5, 1e-5))