The epoch oscillation on the training set is obvious

Gorgen: <p>hello everyone. Is there any problem with the code on the training set, the validation set, and the test set? In particular, is there any problem with loss and precision calculation. The loss is printed according to the epoch, and the epoch oscillation on the training set is obvious. thank you all.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/b/ab7a7eb46d47a9f34eb8b721e5cad34cc286d943.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/ab7a7eb46d47a9f34eb8b721e5cad34cc286d943" title="LQY~QJUJ5E)AYT)V1HR_PV"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/b/ab7a7eb46d47a9f34eb8b721e5cad34cc286d943_2_497x500.jpeg" alt="LQY~QJUJ5E)AYT)V1HR_PV" data-base62-sha1="osY5GhXsO9J8EAkn0Cd1TSinR2b" width="497" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/b/ab7a7eb46d47a9f34eb8b721e5cad34cc286d943_2_497x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/a/b/ab7a7eb46d47a9f34eb8b721e5cad34cc286d943_2_745x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/a/b/ab7a7eb46d47a9f34eb8b721e5cad34cc286d943.jpeg 2x" data-dominant-color="F7F7F6"><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">LQY~QJUJ5E)AYT)V1HR_PV</span><span class="informations">872×877 90.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><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cdda64cc0001bdb60ad682f2a8d770e9f6dc4b70.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/cdda64cc0001bdb60ad682f2a8d770e9f6dc4b70" title="SW9JM~6911EIHG{XZC_88Y"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cdda64cc0001bdb60ad682f2a8d770e9f6dc4b70.jpeg" alt="SW9JM~6911EIHG{XZC_88Y" data-base62-sha1="tn3MQyQceUA83SdBtqKYpnhL9yo" width="614" height="500" data-dominant-color="F3F3F3"><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">SW9JM~6911EIHG{XZC_88Y</span><span class="informations">640×521 55.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><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/7/77d0a533856af92c4e7ef4741051bd7234a56fcd.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/77d0a533856af92c4e7ef4741051bd7234a56fcd" title="N2PPO4%NVPWYL_%HXVDJML"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/7/77d0a533856af92c4e7ef4741051bd7234a56fcd_2_560x500.jpeg" alt="N2PPO4%NVPWYL_%HXVDJML" data-base62-sha1="h5VQ35LPXZ2O6IQFa6pefphFWLr" width="560" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/7/77d0a533856af92c4e7ef4741051bd7234a56fcd_2_560x500.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/7/7/77d0a533856af92c4e7ef4741051bd7234a56fcd.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/7/7/77d0a533856af92c4e7ef4741051bd7234a56fcd.jpeg 2x" data-dominant-color="2B2929"><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">N2PPO4%NVPWYL_%HXVDJML</span><span class="informations">774×691 60.6 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>

harpone: <p>It looks like you’re printing only the loss of the last minibatch in epoch… if you want a more accurate representation of the loss, I suggest tracking e.g. all the losses by collecting them into a list, and then taking the average (of maybe the last 100 losses) at the end of epoch and printing that.</p>
<p>However, the loss does seem to fluctuate pretty badly… maybe your minibatch size is very small? Or maybe you’re actually just taking the first example from a minibatch in <code>data[0]</code>?</p>
<p>Going through the training loop line by line with a debugger always helps!</p>

Gorgen: <p><span class="mention">@harphone</span>. Thank  you for your advice. To be honest,  i am new to DL and pytorch. I don’t know how to print the average loss and accuray for an epoch. Could you please share more detailed code or example, thank you very much</p>

harpone: <p>Well it’s kinda hard to modify your code because you shared a screenshot instead of code <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=12" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"></p>
<p>but something like</p>
<pre><code class="lang-auto">running_loss_list = list()
for data in train_loader:
    ...
    running_loss_list.append(loss.item())
running_loss = np.mean(running_loss_list[-100:])
running_loss_list = list()
</code></pre>

Gorgen: <p><a class="mention" href="/u/harpone">@harpone</a> I am grateful for your detailed help.<br>
Thanks, best wishes</p>