Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
80 lines (56 sloc) 2.12 KB
from chainer.backends import cuda
from chainer.backends import intel64
from chainer import optimizer
from chainer import types
import typing_extensions as tpe
class SGDHyperparameter(tpe.Protocol):
"""Protocol class for hyperparameter of vanilla stochastic gradient descent.
This is only for PEP 544 compliant static type checkers.
lr = None # type: float
_default_hyperparam = optimizer.Hyperparameter() # type: SGDHyperparameter # NOQA = 0.01
class SGDRule(optimizer.UpdateRule):
"""Update rule of vanilla stochastic gradient descent.
See :class:`~chainer.optimizers.SGD` for the default values of the
parent_hyperparam (~chainer.optimizer.Hyperparameter): Hyperparameter
that provides the default values.
lr (float): Learning rate.
_kernel = None
def __init__(self, parent_hyperparam=None, lr=None):
super(SGDRule, self).__init__(
parent_hyperparam or _default_hyperparam)
if lr is not None: = lr
def update_core_cpu(self, param):
grad = param.grad
if grad is None:
if isinstance(, intel64.mdarray):,, grad)
else: -= * grad
def update_core_gpu(self, param):
grad = param.grad
if grad is None:
if SGDRule._kernel is None:
SGDRule._kernel = cuda.elementwise(
'T grad, T lr', 'T param',
'param -= lr * grad', 'sgd')
class SGD(optimizer.GradientMethod):
"""Vanilla Stochastic Gradient Descent.
lr (float): Learning rate.
def __init__(self,
super(SGD, self).__init__() = lr
lr = optimizer.HyperparameterProxy('lr')
def create_update_rule(self):
return SGDRule(self.hyperparam)
You can’t perform that action at this time.