Loss at first epoch is very high but back to normal from second epoch and so on

BarCodeReader: <p>I just counter a strange problem recently.</p>
<p>I have a very normal ConvNet and doing some image restoration tasks.</p>
<p>The overall training is fine, my model can yield good results, but the behavior of the first epoch is kinda strange and I just need to confirm if there is something wrong:</p>
<blockquote>
<p>the loss at the end of first epoch is like 6,000,000+, but instantly decrease to less than 10 on second epoch and then everything seems normal…after 100 epochs the loss is around 5.</p>
</blockquote>
<p>Any explaination for this phenomenon, or does anyone counter this before?</p>
<p>Thanks in advance.</p>

ptrblck: <p>Depending on the use case, i.e. which loss function you are using, number of classes etc., you could calculate the “expected” loss for random outputs.<br>
If the initially high loss value does not match the use case, e.g. your initialization might be bad for the use case and thus your model would perform worse than predicting random outputs (e.g. it could be very confident in wrong labels).</p>

BarCodeReader: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>,</p>
<p>Thanks for the reply.</p>
<p>The task I am working on is similar to Super-resolution, and the loss we use is MSE.<br>
so basically we mesure the difference between each pixel and use MSE to calculate the loss.</p>
<p>for 1 epoch, there are 20000 iterations, and loss in 1st epoch will decrease from 10M+ to 6M+,<br>
but instantly drop to around 10 at the begining of 2nd epoch.</p>
<p>for random input the loss is very high, just like what I observed during training.<br>
Thus I still dont understand why second epoch the loss will drop that much…though the model gives good test result in the end anyway…</p>

Shivani_Singh: <p>Hello <a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<p>Actually I’m facing another issue while training the model. I’m getting loss(MSE loss) in the range of micro in the very first epoch. My dataset has input image tensors containing values in the range of 10^-4 to 10^-8 but all positive. My predicted image tensor contains values in the similar range but some values are negative also. And I tried to visualise the predicted image, it just looks like a shade of grey.</p>

ptrblck: <p>How does the visualization of the target look? I would guess that the small (abs) values might be causing the gray-ish output if e.g. <code>matplotlib</code> maps the input range of <code>[0, 1]</code> to all available colors.<br>
Could you compare the target and output visualization and if they are approx. equal check if you could adapt the colormap of <code>imshow</code>?</p>

Shivani_Singh: <p>Yes, I tried to visualise predicted(output) and target images. My predicted image doesn’t show any information, I’m attaching the image here. My input and target images are fine but predicted image didn’t learn anything.<br>
After that I tried to train the model by multiplying all my input with 10^4, then when I visualised the predicted images it was close to the target images. But the loss still doesn’t seems to be fine enough.<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/f/6faddab1bbf7c765473c66caf5d7a113ffd36a95.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/6faddab1bbf7c765473c66caf5d7a113ffd36a95" title="IMG_20211006_111304"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/f/6faddab1bbf7c765473c66caf5d7a113ffd36a95_2_578x500.jpeg" alt="IMG_20211006_111304" data-base62-sha1="fVXtMdogt1sSrG29c1iX237Gy1f" width="578" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/f/6faddab1bbf7c765473c66caf5d7a113ffd36a95_2_578x500.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/6/f/6faddab1bbf7c765473c66caf5d7a113ffd36a95.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/6/f/6faddab1bbf7c765473c66caf5d7a113ffd36a95.jpeg 2x" data-dominant-color="B7B7B7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">IMG_20211006_111304</span><span class="informations">736×636 60 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

Shivani_Singh: <p>This was my loss vs epoch graph for training and testing.<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/5/a501d1e56226451116625e654bf90146e521a622.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/a501d1e56226451116625e654bf90146e521a622" title="IMG_20211006_111713"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/5/a501d1e56226451116625e654bf90146e521a622_2_690x453.jpeg" alt="IMG_20211006_111713" data-base62-sha1="nxIGgFqf2llk8N3oYOXvQbf3FTQ" width="690" height="453" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/5/a501d1e56226451116625e654bf90146e521a622_2_690x453.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/a/5/a501d1e56226451116625e654bf90146e521a622_2_1035x679.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/a/5/a501d1e56226451116625e654bf90146e521a622.jpeg 2x" data-dominant-color="FAF9FB"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">IMG_20211006_111713</span><span class="informations">1079×709 92.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>Scaling the inputs (and targets) during training might be a proper workaround for small absolute values (we’ve been discussing this idea in other threads as well, so you might want to search for it).</p>

Shivani_Singh: <p>Can you provide me the link for that thread?</p>

ptrblck: <p><a href="https://discuss.pytorch.org/t/validation-and-training-loss-almost-constant/130871/2">This</a> is one discussion I could find. There I describe a use case where the original target was in <code>[0, 96]</code> and where scaling the target range to <code>[0, 1]</code> during training (and unscaling it to the original range during prediction) worked better than trying to directly learn the target range.<br>
I’m sure a proper weight and bias initialization would also work, but this was a quick way to let the model learn.</p>

Shivani_Singh: <p>Okay, thanks. Will check.</p>