Repository: chainer/chainer

Issue:
Remove deprecated methods of Optimizer
The following methods are deprecated. We should remove them in v2.

* `Optimizer.zero_grads`
* `Optimizer.compute_grads_norm`
* `Optimizer.clip_grads`
* `Optimizer.weight_decay`
* `Optimizer.accumulate_grads`



Discussion:

Resolving patch:
diff --git a/chainer/optimizer.py b/chainer/optimizer.py
--- a/chainer/optimizer.py
+++ b/chainer/optimizer.py
@@ -441,91 +441,6 @@ def serialize(self, serializer):
             if rule is not None:
                 rule.serialize(serializer[name])
 
-    def zero_grads(self):
-        """Fills all gradient arrays by zeros.
-
-        .. deprecated:: v1.5
-           Use the :meth:`chainer.Link.cleargrads` method for the target link
-           instead.
-
-        """
-        self.target.zerograds()
-
-    def compute_grads_norm(self):
-        """Computes the norm of whole gradients.
-
-        Returns:
-            float: L2 norm of whole gradients, i.e. square root of sum of
-            square of all gradient elements.
-
-        .. warning::
-
-            This method returns a CPU-computed value, which means that this
-            method synchronizes between CPU and GPU if at least one of the
-            gradients reside on the GPU.
-
-        .. deprecated:: v1.5
-
-        """
-        return numpy.sqrt(_sum_sqnorm(
-            [p.grad for p in self.target.params(False)]))
-
-    def clip_grads(self, maxnorm):
-        """Clips the norm of whole gradients up to the threshold.
-
-        Args:
-            maxnorm (float): Threshold of gradient L2 norm.
-
-        .. deprecated:: v1.5
-           Use the :class:`~chainer.optimizer.GradientClipping` hook function
-           instead.
-
-        """
-        self._call_hook(GradientClipping(maxnorm))
-
-    def weight_decay(self, decay):
-        """Applies weight decay to the parameter/gradient pairs.
-
-        Args:
-            decay (float): Coefficient of weight decay.
-
-        .. deprecated:: v1.5
-           Use the :class:`~chainer.optimizer.WeightDecay` hook function
-           instead.
-
-        """
-        self._call_hook(WeightDecay(decay))
-
-    def accumulate_grads(self, grads):
-        """Accumulates gradients from other source.
-
-        This method just adds given gradient arrays to gradients that this
-        optimizer holds. It is typically used in data-parallel optimization,
-        where gradients for different shards are computed in parallel and
-        aggregated by this method. This method correctly treats multiple GPU
-        devices.
-
-        Args:
-            grads (Iterable): Iterable of gradient arrays to be accumulated.
-
-        .. deprecated:: v1.5
-           Use the :meth:`chainer.Link.addgrads` method of the target link
-           instead.
-
-        """
-        for param, g_src in zip(self.target.params(False), grads):
-            g_dst = param.grad
-            if isinstance(g_dst, numpy.ndarray):
-                g_dst += cuda.to_cpu(g_src)
-                continue
-
-            with cuda.get_device(g_dst):
-                if (isinstance(g_src, cuda.ndarray) and
-                        g_dst.device != g_src.device):
-                    g_dst += cuda.copy(g_src, out_device=g_dst.device)
-                else:
-                    g_dst += cuda.to_gpu(g_src)
-
 
 class GradientMethod(Optimizer):
     """Base class of all single gradient-based optimizers.


Test patch:
diff --git a/tests/chainer_tests/test_optimizer.py b/tests/chainer_tests/test_optimizer.py
--- a/tests/chainer_tests/test_optimizer.py
+++ b/tests/chainer_tests/test_optimizer.py
@@ -448,10 +448,6 @@ def check_update(self):
         self.target[1].param.update_rule.update.assert_called_once_with(
             self.target[1].param)
 
-        self.optimizer.zero_grads()
-        self.assertTrue((cuda.to_cpu(self.target[0].param.grad) == 0).all())
-        self.assertTrue((cuda.to_cpu(self.target[1].param.grad) == 0).all())
-
     def test_update_cpu(self):
         self.setup_cpu()
         self.check_update()
@@ -461,87 +457,6 @@ def test_update_gpu(self):
         self.setup_gpu()
         self.check_update()
 
-    def check_accumulate_grads_from_cpu(self):
-        self.optimizer.accumulate_grads([np.arange(3)] * 2)
-        grad = self.target[0].param.grad
-        self.assertTrue((cuda.to_cpu(grad) == np.arange(3) * 2).all())
-
-    @attr.gpu
-    def check_accumulate_grads_from_gpu(self, src_id):
-        with cuda.Device(src_id):
-            self.optimizer.accumulate_grads([cuda.cupy.arange(3)] * 2)
-        grad = self.target[0].param.grad
-        self.assertTrue((cuda.to_cpu(grad) == np.arange(3) * 2).all())
-
-    def test_accumulate_grads_cpu_to_cpu(self):
-        self.setup_cpu()
-        self.check_accumulate_grads_from_cpu()
-
-    @attr.gpu
-    def test_accumulate_grads_cpu_to_gpu(self):
-        self.setup_gpu()
-        self.check_accumulate_grads_from_cpu()
-
-    @attr.gpu
-    def test_accumulate_grads_gpu_to_cpu(self):
-        self.setup_cpu()
-        self.check_accumulate_grads_from_gpu(cuda.Device().id)
-
-    @attr.gpu
-    def test_accumulate_grads_gpu_to_gpu(self):
-        device_id = cuda.Device().id
-        self.setup_gpu(device_id)
-        self.check_accumulate_grads_from_gpu(device_id)
-
-    @attr.multi_gpu(2)
-    def test_accumulate_grads_multigpu(self):
-        self.setup_gpu(0)
-        self.check_accumulate_grads_from_gpu(1)
-
-    def check_compute_grads_norm(self):
-        norm = self.optimizer.compute_grads_norm()
-        self.assertAlmostEqual(norm, np.sqrt(10))
-
-    def test_compute_grads_norm_cpu(self):
-        self.setup_cpu()
-        self.check_compute_grads_norm()
-
-    @attr.gpu
-    def test_compute_grads_norm_gpu(self):
-        self.setup_gpu()
-        self.check_compute_grads_norm()
-
-    def check_weight_decay(self):
-        self.optimizer.weight_decay(0.1)
-        g = cuda.to_cpu(self.target[0].param.grad)
-        expect = np.array([0.0, 1.1, 2.2], dtype=np.float32)
-        testing.assert_allclose(g, expect)
-
-    def test_weight_decay_cpu(self):
-        self.setup_cpu()
-        self.check_weight_decay()
-
-    @attr.gpu
-    def test_weight_decay_gpu(self):
-        self.setup_gpu()
-        self.check_weight_decay()
-
-    def check_clip_grads(self):
-        self.optimizer.clip_grads(1.0)
-        g0 = cuda.to_cpu(self.target[0].param.grad)
-        g1 = cuda.to_cpu(self.target[1].param.grad)
-        sqnorm = g0.dot(g0) + g1.dot(g1)
-        self.assertAlmostEqual(sqnorm, 1.0, delta=1.0e-5)
-
-    def test_clip_grads_cpu(self):
-        self.setup_cpu()
-        self.check_clip_grads()
-
-    @attr.gpu
-    def test_clip_grads_gpu(self):
-        self.setup_gpu()
-        self.check_clip_grads()
-
 
 class DummyOptimizer(chainer.GradientMethod):