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

Function test_detach

imperative/python/test/integration/test_grad_detach.py:23–39  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

21
22
23def test_detach():
24 net = Simple()
25
26 optim = optimizer.SGD(net.parameters(), lr=1.0)
27 optim.clear_grad()
28 gm = ad.GradManager().attach(net.parameters())
29
30 dshape = (10, 10)
31 data = tensor(np.ones(dshape).astype(np.float32))
32 with gm:
33 loss = net(data).sum()
34 gm.backward(loss)
35 optim.step()
36 np.testing.assert_equal(net.a.numpy(), np.array([1.0]).astype(np.float32))
37 np.testing.assert_equal(
38 net.b.numpy(), np.array([1.0 - 10.0 * 10.0]).astype(np.float32)
39 )

Callers

nothing calls this directly

Calls 14

parametersMethod · 0.80
onesMethod · 0.80
assert_equalMethod · 0.80
arrayMethod · 0.80
SimpleClass · 0.70
SGDMethod · 0.45
clear_gradMethod · 0.45
attachMethod · 0.45
GradManagerMethod · 0.45
astypeMethod · 0.45
sumMethod · 0.45
backwardMethod · 0.45

Tested by

no test coverage detected