()
| 87 | |
| 88 | |
| 89 | def test_grad_2(): |
| 90 | x_np = np.random.rand(10).astype("float32") |
| 91 | x = as_tensor(x_np) |
| 92 | |
| 93 | with Grad() as grad: |
| 94 | grad.wrt(x, callback=save_to(x)) |
| 95 | y = mul(x, x) |
| 96 | y = mul(y, y) |
| 97 | grad(y, as_tensor(np.ones_like(x_np))) |
| 98 | |
| 99 | np.testing.assert_almost_equal(x.grad.numpy(), 4 * x_np ** 3, decimal=6) |
| 100 | |
| 101 | |
| 102 | @pytest.mark.require_higher_order_directive() |