Model not converging moving from Keras to Pytorch, faulty ReLU

canyalniz: <p>I recently moved my regression model from Keras to Pytorch and I have been getting much worse results in Pytorch, to say the least. At first the model wasn’t even converging, it was just getting worse with each epoch(the std of the error kept growing both on the training set and the validation set). This was happening even though I had taken the utmost care to have the <em>weight/bias initializations</em>, <em>optimizer and loss function parameters</em>, <em>learning rate</em> and <em>batch size</em> exactly the same as I had them in Keras.</p>
<p>But then, as I was trying out different architectures to see what the problem was, I accidentally forgot to add the ReLU activation to one of them and my mistake somehow made the model converge! After realizing this I tried removing the ReLU activations from the other architectures as well and they all started converging.</p>
<p>Has anyone else been experiencing the same problem? Is there something wrong with ReLU in Pytorch?</p>
<p>Also, as a side note, I haven’t been able to find the source code for torch.relu(), if anyone knows where to find it and could share a link it would be very helpful. Thank you!</p>

colesbury: <p>There’s nothing wrong with the ReLU implementation. It’s widely used and pretty simple:</p>
<p>(ReLU calls threshold)</p>
<p>CPU: <a href="https://github.com/pytorch/pytorch/blob/3b1c3996e1c82ca8f43af9efa196b33e36efee37/aten/src/ATen/native/cpu/Activation.cpp#L33" rel="nofollow noopener">https://github.com/pytorch/pytorch/blob/3b1c3996e1c82ca8f43af9efa196b33e36efee37/aten/src/ATen/native/cpu/Activation.cpp#L33</a><br>
CUDA: <a href="https://github.com/pytorch/pytorch/blob/3b1c3996e1c82ca8f43af9efa196b33e36efee37/aten/src/ATen/native/cuda/Activation.cu#L290" rel="nofollow noopener">https://github.com/pytorch/pytorch/blob/3b1c3996e1c82ca8f43af9efa196b33e36efee37/aten/src/ATen/native/cuda/Activation.cu#L290</a></p>
<p>The choice of derivative for torch.relu at 0 may vary between frameworks. The subgradient includes the interval [0,1]. PyTorch uses 0 for the derivative. I think TensorFlow also uses 0, but other frameworks might use 1. (Values between 0 and 1 are also subgradients, but would make for an awkward choice.)</p>
<p>                    <a href="https://miro.medium.com/max/1400/1*DfMRHwxY1gyyDmrIAd-gjQ.png" target="_blank" class="onebox" rel="nofollow noopener">
            <img src="https://miro.medium.com/max/1400/1*DfMRHwxY1gyyDmrIAd-gjQ.png" width="690" height="314">
          </a>

</p>
<p>(Image from <a href="https://medium.com/@danqing/a-practical-guide-to-relu-b83ca804f1f7" rel="nofollow noopener">https://medium.com/@danqing/a-practical-guide-to-relu-b83ca804f1f7</a>)</p>

colesbury: <p>Also removing non linearities (like ReLU) typically makes models easier to optimize, but less powerful. If you only have linear functions (or more precisely affine) like nn.Linear, nn.Conv2d, and + than you can only learn a linear (affine) function of your inputs.</p>
<p>My guess is that there’s some difference in the interpretation of optimizer parameters (like momentum) plus maybe some other small differences. I remember that there’s a different interpretation of momentum in PyTorch vs. Caffe2, but I don’t remember how it differs from Caffe2.</p>
<p>For SGD, I think dampening=True more closely matches the Caffe2 behavior:<br>
<a href="https://pytorch.org/docs/stable/optim.html#torch.optim.SGD" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/docs/stable/optim.html#torch.optim.SGD</a></p>

canyalniz: <p><a class="mention" href="/u/colesbury">@colesbury</a> Thank you for such a quick response.  Although the model is still yielding problematic results in Pytorch using ReLU, I was at least able to get it to converge using selu instead. I guess hoping the model would behave the same in different frameworks was an unrealistic expectation in the first place.</p>