@@ -0,0 +1,1945 @@
+from chainer import cuda%0Afrom chainer import optimizer%0A%0A%0A_default_hyperparam = optimizer.Hyperparameter()%0A_default_hyperparam.lr = 0.01%0A_default_hyperparam.momentum = 0.9%0A%0A%0Aclass CorrectedMomentumSGDRule(optimizer.UpdateRule):%0A%0A    # use update rule used in frameworks like Torch.%0A%0A    def __init__(self, parent_hyperparam=None, lr=None, momentum=None):%0A        super(CorrectedMomentumSGDRule, self).__init__(%0A            parent_hyperparam or _default_hyperparam)%0A        if lr is not None:%0A            self.hyperparam.lr = lr%0A        if momentum is not None:%0A            self.hyperparam.momentum = momentum%0A%0A    def init_state(self, param):%0A        xp = cuda.get_array_module(param.data)%0A        with cuda.get_device_from_array(param.data):%0A            self.state%5B'v'%5D = xp.zeros_like(param.data)%0A%0A    def update_core_cpu(self, param):%0A        grad = param.grad%0A        if grad is None:%0A            return%0A        v = self.state%5B'v'%5D%0A        v *= self.hyperparam.momentum%0A        v -= self.hyperparam.lr * grad%0A        param.data += v%0A%0A    def update_core_gpu(self, param):%0A        grad = param.grad%0A        if grad is None:%0A            return%0A        cuda.elementwise(%0A            'T grad, T lr, T momentum',%0A            'T param, T v',%0A            '''v = momentum * v - grad;%0A               param += lr * v;''',%0A            'momentum_sgd')(%0A                grad, self.hyperparam.lr, self.hyperparam.momentum,%0A                param.data, self.state%5B'v'%5D)%0A%0A%0Aclass CorrectedMomentumSGD(optimizer.GradientMethod):%0A%0A    def __init__(self, lr=_default_hyperparam.lr,%0A                 momentum=_default_hyperparam.momentum):%0A        super(CorrectedMomentumSGD, self).__init__()%0A        self.hyperparam.lr = lr%0A        self.hyperparam.momentum = momentum%0A%0A    lr = optimizer.HyperparameterProxy('lr')%0A    momentum = optimizer.HyperparameterProxy('momentum')%0A%0A    def create_update_rule(self):%0A        return CorrectedMomentumSGDRule(self.hyperparam)%0A

add corrected_momentum_sgd