()
| 12 | |
| 13 | |
| 14 | def test_single_input(): |
| 15 | data_shape = (9, 2, 6) |
| 16 | av = np.random.random(data_shape).astype(np.float32) |
| 17 | |
| 18 | class MulFunc(Function): |
| 19 | def forward(self, a): |
| 20 | self.a = a |
| 21 | return a * 10 |
| 22 | |
| 23 | def backward(self, grad_o): |
| 24 | return grad_o * 10 |
| 25 | |
| 26 | class Simple(Module): |
| 27 | def __init__(self, a): |
| 28 | super().__init__() |
| 29 | self.a = Parameter(a, dtype=np.float32) |
| 30 | self.layer1 = MulFunc() |
| 31 | |
| 32 | def forward(self): |
| 33 | x = self.layer1(self.a) |
| 34 | return x |
| 35 | |
| 36 | net = Simple(av) |
| 37 | gm = ad.GradManager().attach(net.parameters()) |
| 38 | opt = optimizer.SGD(net.parameters(), lr=1.0) |
| 39 | |
| 40 | opt.clear_grad() |
| 41 | with gm: |
| 42 | loss = net() |
| 43 | gm.backward(loss.sum()) |
| 44 | opt.step() |
| 45 | |
| 46 | np.testing.assert_almost_equal(loss.numpy(), (av * 10)) |
| 47 | np.testing.assert_almost_equal(net.a.numpy(), (av - 10)) |
| 48 | |
| 49 | |
| 50 | def test_multi_input(): |
nothing calls this directly
no test coverage detected