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_r213756786
########## 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: Review comment: is .lower() required here? ---------------------------------------------------------------- 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