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

Class SGD

imperative/python/megengine/optimizer/sgd.py:13–114  ·  view source on GitHub ↗

r"""Implements stochastic gradient descent. This optimizer performs stochastic gradient descent with optional momentum and weight decay. Nesterov momentum is based on the formula from `"On the importance of initialization and momentum in deep learning" <http://www.cs.toronto.edu/%7Ehin

Source from the content-addressed store, hash-verified

11
12
13class SGD(Optimizer):
14 r"""Implements stochastic gradient descent.
15
16 This optimizer performs stochastic gradient descent with optional momentum and weight decay.
17
18 Nesterov momentum is based on the formula from
19 `"On the importance of initialization and momentum in deep learning" <http://www.cs.toronto.edu/%7Ehinton/absps/momentum.pdf>`_.
20
21 Args:
22 params (Union[Iterable[Parameter], dict]): Iterable of parameters to optimize or dicts defining
23 parameter groups.
24 lr (float): Learning rate.
25 momentum (float): Momentum factor. Default: 0.0.
26 nesterov (bool): Enables Nesterov momentum. Default: False.
27 weight_decay (float): Weight decay (L2 penalty). Default: 0.0.
28
29 Returns:
30 An instance of the SGD optimizer.
31
32 Note:
33 This optimizer does not guarantee that the interval does not include the stop value in cases
34 where the step is not an integer and floating-point rounding errors affect the length of the output tensor.
35 """
36
37 def __init__(
38 self,
39 params: Union[Iterable[Parameter], dict],
40 lr: float,
41 momentum: float = 0.0,
42 nesterov: bool = False,
43 weight_decay: float = 0.0,
44 ):
45 assert lr >= 0.0, "Invalid learning rate: {}".format(lr)
46 assert momentum >= 0.0, "Invalid momentum value: {}".format(momentum)
47 assert weight_decay >= 0.0, "Invalid weight_decay value: {}".format(
48 weight_decay
49 )
50 assert not nesterov or momentum > 0.0, "Nesterov momentum requires a momentum"
51
52 defaults = dict(lr=lr, momentum=momentum, weight_decay=weight_decay)
53 super().__init__(params, defaults)
54 self.nesterov = nesterov
55 self._disable_type_convert = True
56
57 def _create_state(self, param_group):
58 if param_group["momentum"] != 0.0:
59 for param in param_group["params"]:
60 self._add_state(param, "momentum_buffer")
61
62 def _updates(self, param_group):
63 lr = param_group["lr"]
64 weight_decay = param_group["weight_decay"]
65 momentum = param_group["momentum"]
66
67 # since `conver_inputs` is disabled for param updates,
68 # scalar should be explicitly tansforred to tensor
69
70 _lr = tensor(lr, dtype="float32")

Callers 6

update_modelFunction · 0.90
workerFunction · 0.90
workerFunction · 0.90
test_ViTmode_trace_trainFunction · 0.90
test_training_convergeFunction · 0.90
test_multi_step_lrFunction · 0.90

Calls

no outgoing calls

Tested by 6

update_modelFunction · 0.72
workerFunction · 0.72
workerFunction · 0.72
test_ViTmode_trace_trainFunction · 0.72
test_training_convergeFunction · 0.72
test_multi_step_lrFunction · 0.72