vandanavk commented on a change in pull request #12376: [MXNET-854] SVRG 
Optimization in Python Module API
URL: https://github.com/apache/incubator-mxnet/pull/12376#discussion_r213756853
 
 

 ##########
 File path: python/mxnet/contrib/svrg_optimization/svrg_optimizer.py
 ##########
 @@ -0,0 +1,133 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""A `SVRGOptimizer` encapsulates two optimizers to accommodate SVRG 
optimization logic.
+"""
+
+
+import mxnet as mx
+
+
+@mx.optimizer.register
+class AssignmentOptimizer(mx.optimizer.Optimizer):
+    """AssignmentOptimizer assigns gradients to be weights for SVRGModule full 
gradients
+    accumulation in the KVStore
+    """
+    def update(self, index, weight, grad, state):
+        weight[:] = grad
+
+
+@mx.optimizer.register
+class SVRGOptimizer(mx.optimizer.Optimizer):
+    """SVRGOptimizer is a wrapper class for two optimizers: one for 
accumulating full gradients and the other
+    one is the passed-in optimizer.
+
+    Parameters
+    ----------
+    default_optimizer: optimizer passed-in when invoke on mx.mod.init_optimizer
+    """
+
+    def __init__(self, default_optimizer, **kwargs):
+        # Reconstruct kwargs to identify additional params for default 
optimizer
+        default_param = self._check_params(**kwargs)
+        super(SVRGOptimizer, self).__init__(**default_param)
+        if isinstance(default_optimizer, str):
+            self.default_opt = mx.optimizer.create(default_optimizer, **kwargs)
+        else:
+            self.default_opt = default_optimizer
+        self.aux_opt = mx.optimizer.create(AssignmentOptimizer.__name__)
+
+
+    def _check_params(self, **kwargs):
+        optimizer_param = dict(kwargs)
+        base_params = ['rescale_grad', 'param_idx2name', 'wd', 
'clip_gradient', 'learning_rate', 'lr_scheduler', 'sym',
+                       'begin_num_update', 'multi_precision', 'param_dict']
+
+        default_params = {}
+        for key, _ in optimizer_param.items():
+            if key in base_params:
+                default_params[key] = optimizer_param[key]
+
+        return default_params
+
+    def update(self, index, weight, grad, state):
+        """Updates the given parameter using the corresponding gradient and 
state. If key contains 'full', update with
+        lr = -1 otherwise will use default optimizer.
+
+        Parameters
+        ----------
+        index : int
+            The unique index of the parameter into the individual learning
+            rates and weight decays. Learning rates and weight decay
+            may be set via `set_lr_mult()` and `set_wd_mult()`, respectively.
+        weight : NDArray
+            The parameter to be updated.
+        grad : NDArray
+            The gradient of the objective with respect to this parameter.
+        state : any obj
+            The state returned by `create_state()`.
+        """
+
+        name = self._check_index(index)
+
+        if "full".lower() in name:
+            self.aux_opt.update(index, weight, grad, state)
+        else:
+            # use the default optimizer
+            self.default_opt.update(index, weight, grad, state)
+
+    def create_state(self, index, weight):
+        """Creates auxiliary state for a given weight.
+        Some optimizers require additional states, e.g. as momentum, in 
addition
+        to gradients in order to update weights. This function creates state
+        for a given weight which will be used in `update`. This function is
+        called only once for each weight.
+
+        Parameters
+        ----------
+        index : int
+            An unique index to identify the weight.
+        weight : NDArray
+            The weight.
+        Returns
+        -------
+        state : any obj
+            The state associated with the weight.
+        """
+
+        name = self._check_index(index)
+        if "full".lower() in name:
 
 Review comment:
   Here too

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to