title: Custom loss function breaking backpropagation in Pytorch
body: <p>I am using this class</p>
  <pre><code>class MonetaryLoss(nn.Module):
      def __init__(self,risk,payout):
          super(MonetaryLoss,self).__init__()
          self.risk = risk
          self.payout = payout
      def forward(self,pred,y):
          equity = 100
          pred = t.max(pred,1).indices
          for i in range(len(pred)):
              if (pred[i] == y[i]):
                  equity += equity*self.risk*self.payout
              else:
                  equity -= equity*self.risk
          print(equity)
          return equity
  </code></pre>
  <p>to prepare some examples of custom losses. When I use t.max(pred,1).indices the gradient is lost and this forward() function does not return anything with a gradient. I thought using</p>
  <pre><code>loss = Variable(t.Tensor[0], requires_grad = True)
  </code></pre>
  <p>and then updating the loss variable over training iterations would solve the problem. However, it breaks the backpropagation chain. What is the best way to create this loss and leave the backpropagation chain intact?</p>