Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
103 lines (74 sloc) 2.8 KB
import numpy
import chainer
from chainer.backends import cuda
from chainer import optimizer
from chainer import types
import typing_extensions as tpe
class AdaGradHyperparameter(tpe.Protocol):
"""Protocol class for hyperparameter of AdaGrad.
This is only for PEP 544 compliant static type checkers.
lr = None # type: float
eps = None # type: float
_default_hyperparam = optimizer.Hyperparameter() # type: AdaGradHyperparameter # NOQA = 0.001
_default_hyperparam.eps = 1e-8
class AdaGradRule(optimizer.UpdateRule):
"""Update rule of AdaGrad.
See :class:`~chainer.optimizers.AdaGrad` for the default values of the
parent_hyperparam (~chainer.optimizer.Hyperparameter): Hyperparameter
that provides the default values.
lr (float): Learning rate.
eps (float): Small value for the numerical stability.
_kernel = None
def __init__(self, parent_hyperparam=None, lr=None, eps=None):
super(AdaGradRule, self).__init__(
parent_hyperparam or _default_hyperparam)
if lr is not None: = lr
if eps is not None:
self.hyperparam.eps = eps
def init_state(self, param):
with chainer.using_device(param.device):
self.state['h'] = param.device.xp.zeros_like(
def update_core_cpu(self, param):
grad = param.grad
if grad is None:
lr =
eps = self.hyperparam.eps
h = self.state['h']
h += grad * grad -= lr * grad / (numpy.sqrt(h) + eps)
def update_core_gpu(self, param):
grad = param.grad
if grad is None:
if AdaGradRule._kernel is None:
AdaGradRule._kernel = cuda.elementwise(
'T grad, T lr, T eps',
'T param, T h',
'''h += grad * grad;
param -= lr * grad / (sqrt(h) + eps);''',
AdaGradRule._kernel(grad,, self.hyperparam.eps,, self.state['h'])
class AdaGrad(optimizer.GradientMethod):
"""AdaGrad optimizer.
lr (float): Learning rate.
eps (float): Small value for the numerical stability.
def __init__(self,, eps=_default_hyperparam.eps):
super(AdaGrad, self).__init__() = lr
self.hyperparam.eps = eps
lr = optimizer.HyperparameterProxy('lr')
eps = optimizer.HyperparameterProxy('eps')
def create_update_rule(self):
return AdaGradRule(self.hyperparam)
You can’t perform that action at this time.