MCPcopy Create free account
hub / github.com/AtlasAnalyticsLab/AdaFisher / SGD

Class SGD

optimizers/sgd.py:77–189  ·  view source on GitHub ↗

r"""Implements stochastic gradient descent (optionally with momentum). Nesterov momentum is based on the formula from `On the importance of initialization and momentum in deep learning`__. Args: params (iterable): iterable of parameters to optimize or dicts defining

Source from the content-addressed store, hash-verified

75
76
77class SGD(Optimizer):
78 r"""Implements stochastic gradient descent (optionally with momentum).
79
80 Nesterov momentum is based on the formula from
81 `On the importance of initialization and momentum in deep learning`__.
82
83 Args:
84 params (iterable): iterable of parameters to optimize or dicts defining
85 parameter groups
86 lr (float): learning rate
87 momentum (float, optional): momentum factor (default: 0)
88 weight_decay (float, optional): weight decay (L2 penalty) (default: 0)
89 dampening (float, optional): dampening for momentum (default: 0)
90 nesterov (bool, optional): enables Nesterov momentum (default: False)
91
92 Example:
93 >>> optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
94 >>> optimizer.zero_grad()
95 >>> loss_fn(model(input), target).backward()
96 >>> optimizer.step()
97
98 __ http://www.cs.toronto.edu/%7Ehinton/absps/momentum.pdf
99
100 .. note::
101 The implementation of SGD with Momentum/Nesterov subtly differs from
102 Sutskever et. al. and implementations in some other frameworks.
103
104 Considering the specific case of Momentum, the update can be written as
105
106 .. math::
107 \begin{aligned}
108 v_{t+1} & = \mu * v_{t} + g_{t+1}, \\
109 p_{t+1} & = p_{t} - \text{lr} * v_{t+1},
110 \end{aligned}
111
112 where :math:`p`, :math:`g`, :math:`v` and :math:`\mu` denote the
113 parameters, gradient, velocity, and momentum respectively.
114
115 This is in contrast to Sutskever et. al. and
116 other frameworks which employ an update of the form
117
118 .. math::
119 \begin{aligned}
120 v_{t+1} & = \mu * v_{t} + \text{lr} * g_{t+1}, \\
121 p_{t+1} & = p_{t} - v_{t+1}.
122 \end{aligned}
123
124 The Nesterov version is analogously modified.
125 """
126
127 def __init__(self, params, lr=required, momentum=0, dampening=0,
128 weight_decay=0, nesterov=False):
129 if lr is not required and lr < 0.0:
130 raise ValueError("Invalid learning rate: {}".format(lr))
131 if momentum < 0.0:
132 raise ValueError("Invalid momentum value: {}".format(momentum))
133 if weight_decay < 0.0:
134 raise ValueError(

Callers 2

get_optimizer_schedulerFunction · 0.90
mainFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected