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

Function test_basic

imperative/python/test/unit/autodiff/test_grad_manager.py:18–42  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16
17
18def test_basic():
19 x = mge.tensor([1.0, 3.0, 5.0]).reshape(1, 3)
20 w = mge.tensor([2.0, 4.0, 6.0]).reshape(3, 1)
21 b = mge.tensor(-1.0)
22
23 gm = GradManager().attach([w, b])
24 gm.record()
25
26 p = F.matmul(x, w)
27 y = p + b
28
29 gm.backward(y)
30 gm.release() # is not necessary
31 np.testing.assert_equal(w.grad.numpy(), [[1], [3], [5]])
32 np.testing.assert_equal(b.grad.numpy(), [1])
33
34 w.grad = None
35 b.grad = None
36 with gm:
37 p = F.matmul(x, w)
38 y = p + b
39 gm.backward(y)
40
41 np.testing.assert_equal(w.grad.numpy(), [[1], [3], [5]])
42 np.testing.assert_equal(b.grad.numpy(), [1])
43
44
45def test_dy():

Callers

nothing calls this directly

Calls 9

GradManagerClass · 0.90
tensorMethod · 0.80
assert_equalMethod · 0.80
reshapeMethod · 0.45
attachMethod · 0.45
recordMethod · 0.45
backwardMethod · 0.45
releaseMethod · 0.45
numpyMethod · 0.45

Tested by

no test coverage detected