You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2019/11/20 10:28:52 UTC

[GitHub] [incubator-mxnet] liuzh91 opened a new issue #16869: Cannot manipulate gradients during training in estimator

liuzh91 opened a new issue #16869: Cannot manipulate gradients during training in estimator
URL: https://github.com/apache/incubator-mxnet/issues/16869
 
 
   **Description**
   In current estimator framework, api fit_batch() forward the training batch through the network, compute the loss and gradients and update network parameters with gradients. The default 
   fit_batch() function is shown as below:
   
   ```
   def fit_batch(self, train_batch, batch_axis=0):
           data, label = self._get_data_and_label(train_batch, self.context, batch_axis)
           batch_size = train_batch[0].shape[batch_axis]
   
           with autograd.record():
               pred = [self.net(x) for x in data]
               loss = [self.loss(y_hat, y) for y_hat, y in zip(pred, label)]
   
           for l in loss:
               l.backward()
   
           self.trainer.step(batch_size)
   
           return data, label, pred, loss`
   ```
   The problem is in some use cases like gradient accumulation, users may not update gradient at each batch. They apply gradients on network parameters periodically. There is no way to implement the gradient accumulation feature in customized estimators.
   
   One possible option is to create a handler to update/apply gradients at the end of each batch, but it violates the definition of handlers since handlers are not supposed to apply gradients on network parameters.  Gradient updates should be processed in the fit_batch() api.
   
   So a viable way to resolve the problem is to decompose the fit_batch() function into two methods:
   -  computations of loss/gradient  
   -  applying gradients on network parameters 
   
   between these two methods we could add some customized api to process gradients, e.g., gradient clipping or gradient accumulation.
   
    

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


With regards,
Apache Git Services