title: How to deal with mini-batch loss in Pytorch?
body: <p>I feed mini-batch data to model, and I just want to know how to deal with the loss. Could I accumulate the loss, then call the backward like:</p>
  
  <pre><code>    ...
      def neg_log_likelihood(self, sentences, tags, length):
          self.batch_size = sentences.size(0)
  
          logits = self.__get_lstm_features(sentences, length)
          real_path_score = torch.zeros(1)
          total_score = torch.zeros(1)
          if USE_GPU:
              real_path_score = real_path_score.cuda()
              total_score = total_score.cuda()
  
          for logit, tag, leng in zip(logits, tags, length):
              logit = logit[:leng]
              tag = tag[:leng]
              real_path_score += self.real_path_score(logit, tag)
              total_score += self.total_score(logit, tag)
          return total_score - real_path_score
      ...
  loss = model.neg_log_likelihood(sentences, tags, length)
  loss.backward()
  optimizer.step()
  </code></pre>
  
  <p>I wonder that if the accumulation could lead to gradient explosion?</p>
  
  <p>So, should I call the backward in loop:</p>
  
  <pre><code>for sentence, tag , leng in zip(sentences, tags, length):
      loss = model.neg_log_likelihood(sentence, tag, leng)
      loss.backward()
      optimizer.step()
  </code></pre>
  
  <p>Or, use the mean loss just like the <strong>reduce_mean</strong> in tensorflow</p>
  
  <pre><code>loss = reduce_mean(losses)
  loss.backward()
  </code></pre>