()
| 16 | |
| 17 | |
| 18 | def 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 | |
| 45 | def test_dy(): |
nothing calls this directly
no test coverage detected