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

Function test_adagrad

imperative/python/test/integration/test_optimizer.py:221–244  ·  view source on GitHub ↗
(monkeypatch, case, update_lr, inplace_mode)

Source from the content-addressed store, hash-verified

219@pytest.mark.parametrize("update_lr", [False, True])
220@pytest.mark.parametrize("inplace_mode", [False, True])
221def test_adagrad(monkeypatch, case, update_lr, inplace_mode):
222 class CheckValue:
223 def __init__(self, net, **kwarg):
224 self.s_slots = {}
225 for param in net.parameters():
226 self.s_slots[param] = np.zeros(param.shape).astype(np.float32)
227 for k, v in kwarg.items():
228 setattr(self, k, v)
229
230 def __call__(self, ori_params, new_params, step):
231 for param in new_params:
232 grad = param.grad.numpy()
233 if hasattr(self, "weight_decay") and self.weight_decay != 0.0:
234 grad = grad + ori_params[param] * self.weight_decay
235 self.s_slots[param] += grad ** 2
236 delta = grad / (self.s_slots[param] + self.eps) ** 0.5
237 delta *= -(self.lr / (1 + (step - 1) * self.lr_decay))
238 np.testing.assert_almost_equal(
239 param.numpy(), ori_params[param] + delta, decimal=6
240 )
241
242 with monkeypatch.context() as mk:
243 mk.setenv("MEGENGINE_INPLACE_UPDATE", str(int(inplace_mode)))
244 _test_optimizer("Adagrad", case, CheckValue, update_lr=update_lr)
245
246
247@pytest.mark.parametrize(

Callers

nothing calls this directly

Calls 3

strFunction · 0.85
_test_optimizerFunction · 0.85
contextMethod · 0.80

Tested by

no test coverage detected