()
| 48 | |
| 49 | |
| 50 | def test_multi_input(): |
| 51 | data_shape = (9, 2, 6) |
| 52 | av = np.random.random(data_shape).astype(np.float32) |
| 53 | bv = np.random.random(data_shape).astype(np.float32) |
| 54 | |
| 55 | class MulFunc(Function): |
| 56 | def forward(self, a, b): |
| 57 | self.a = a |
| 58 | self.b = b |
| 59 | return a * b |
| 60 | |
| 61 | def backward(self, grad_o): |
| 62 | return grad_o * self.b * 2, grad_o * self.a * 3 |
| 63 | |
| 64 | class Simple(Module): |
| 65 | def __init__(self, a, b): |
| 66 | super().__init__() |
| 67 | self.a = Parameter(a, dtype=np.float32) |
| 68 | self.b = Parameter(b, dtype=np.float32) |
| 69 | self.layer1 = MulFunc() |
| 70 | |
| 71 | def forward(self): |
| 72 | x = self.layer1(self.a, self.b) |
| 73 | return x |
| 74 | |
| 75 | net = Simple(av, bv) |
| 76 | gm = ad.GradManager().attach(net.parameters()) |
| 77 | opt = optimizer.SGD(net.parameters(), lr=1.0) |
| 78 | |
| 79 | opt.clear_grad() |
| 80 | with gm: |
| 81 | loss = net() |
| 82 | gm.backward(loss.sum()) |
| 83 | opt.step() |
| 84 | |
| 85 | np.testing.assert_almost_equal(loss.numpy(), (av * bv)) |
| 86 | np.testing.assert_almost_equal(net.a.numpy(), (av - 2 * bv)) |
| 87 | np.testing.assert_almost_equal(net.b.numpy(), (bv - 3 * av)) |
| 88 | |
| 89 | |
| 90 | def test_multi_output(): |
nothing calls this directly
no test coverage detected