MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / test_2nd_grad

Function test_2nd_grad

imperative/python/test/unit/core/test_autodiff.py:103–120  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

101
102@pytest.mark.require_higher_order_directive()
103def test_2nd_grad():
104 x_np = np.random.rand(10).astype("float32")
105 x = as_tensor(x_np)
106 ones = as_tensor(np.ones_like(x_np))
107
108 with Grad("grad2") as grad2:
109 with Grad("grad") as grad:
110 grad2.wrt(x, callback=save_to(x))
111 grad.wrt(x, callback=save_to(x))
112 y = cos(x)
113 grad(y, ones)
114 z = x.grad
115 np.testing.assert_almost_equal(x.grad.numpy(), -np.sin(x_np), decimal=5)
116
117 x.grad = None
118 grad2(z, ones)
119
120 np.testing.assert_almost_equal(x.grad.numpy(), -np.cos(x_np), decimal=5)
121
122
123def test_grad_with_tensor_wrapper():

Callers

nothing calls this directly

Calls 10

GradClass · 0.90
save_toFunction · 0.85
gradFunction · 0.85
wrtMethod · 0.80
as_tensorFunction · 0.70
cosFunction · 0.50
astypeMethod · 0.45
numpyMethod · 0.45
sinMethod · 0.45
cosMethod · 0.45

Tested by

no test coverage detected