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

Function run_frozen_bn

imperative/python/test/integration/test_bn.py:15–59  ·  view source on GitHub ↗
(BNModule, is_training, use_trace, use_symbolic)

Source from the content-addressed store, hash-verified

13
14
15def run_frozen_bn(BNModule, is_training, use_trace, use_symbolic):
16 nchannel = 3
17 m = BNModule(nchannel, freeze=True)
18 if is_training:
19 m.train()
20 else:
21 m.eval()
22 var = 4.0
23 bias = 1.0
24 shape = (1, nchannel, 1, 1)
25 m.running_var[...] = var * F.ones(shape)
26 m.running_mean[...] = bias * F.ones(shape)
27
28 saved_var = m.running_var.numpy()
29 saved_mean = m.running_mean.numpy()
30 saved_wt = m.weight.numpy()
31 saved_bias = m.bias.numpy()
32
33 gm = ad.GradManager().attach(m.parameters())
34 optim = optimizer.SGD(m.parameters(), lr=1.0)
35 optim.clear_grad()
36
37 data = np.random.random((6, nchannel, 2, 2)).astype("float32")
38
39 def train_fn(d):
40 for _ in range(3):
41 with gm:
42 loss = m(d).mean()
43 gm.backward(loss)
44 optim.step()
45 return loss
46
47 if use_trace:
48 train_fn = trace(train_fn, symbolic=use_symbolic)
49
50 for _ in range(3):
51 loss = train_fn(megengine.tensor(data))
52 if not is_training:
53 np.testing.assert_equal(m.running_var.numpy(), saved_var)
54 np.testing.assert_equal(m.running_mean.numpy(), saved_mean)
55 np.testing.assert_almost_equal(
56 loss.numpy(), ((data - bias) / np.sqrt(var)).mean(), 5
57 )
58 np.testing.assert_equal(m.weight.numpy(), saved_wt)
59 np.testing.assert_equal(m.bias.numpy(), saved_bias)
60
61
62@pytest.mark.parametrize("is_training", [False, True])

Callers 2

test_frozen_bnFunction · 0.85
workerFunction · 0.85

Calls 15

traceClass · 0.90
train_fnFunction · 0.85
onesMethod · 0.80
parametersMethod · 0.80
tensorMethod · 0.80
assert_equalMethod · 0.80
trainMethod · 0.45
evalMethod · 0.45
numpyMethod · 0.45
attachMethod · 0.45
GradManagerMethod · 0.45
SGDMethod · 0.45

Tested by

no test coverage detected