Confused about train/test in neural networks

jpj: <p>I am running the neural network for the train data for 10 iterations, with a learning rate of 0.01 and the accuracy increases about 20% after those 10 iterations.</p>
<p>I’ve used train/test in regression previously, and it’s mainly testing the data on a set of parameters that I computed during the testing/validation phase.</p>
<p>But for neural networks, the only hyperparameters are the learning rate and epochs/iterations.</p>
<p>How do I run the test data? Do I run it for the same number of epochs as the training or only one, and that becomes my testing accuracy?</p>

RaLo4: <p>It is common to split your data into training set and test set (where test set is much smaller than training set).<br>
This way you can train on the training set and let the network learn from it while you test on the test set from which the network is <strong>not</strong> allowed to learn.</p>
<p>For how often you should test…<br>
Well it’s really up to you. You could test once the whole training is finished or test once after every epoch to see how accuracy is changing over time.<br>
If you test set is not too large I would recommend testing after every epoch.</p>
<p>You can test your network by running your whole (usually the whole if it is not too large) test set through it once.<br>
The very very important difference to training is, that <strong>you do not use back propagation</strong> on the output you get from the test set! So no <code>Tensor.backward()</code> on the output of your test data.<br>
In fact, it is best practice to wrap the testing code into <code>with torch.no_grad():</code></p>
<pre><code class="lang-auto">with torch.no_grad():
    #testing code
</code></pre>
<p>this disables gradient calculation inside it. Which makes sure you cannot call <code>Tensor.backward()</code> meaning you cannot learn from your test data and it can also reduce memory consumption.</p>

jpj: <p>Thank you for your reply, it’s a lot more clearer now. So just to confirm, I just run the test data for only one iteration of the forward pass with the same learning rate or do I also have to first extract the weights from the last iteration of the training phase?</p>

RaLo4: <p>I don’t quite understand what you mean here</p>
<aside class="quote no-group" data-username="jpj" data-post="3" data-topic="112697">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/ed655f/48.png" class="avatar"> jpj:</div>
<blockquote>
<p>do I also have to first extract the weights from the last iteration of the training phase?</p>
</blockquote>
</aside>
<p>and I think you mean epoch and not iteration over here</p>
<aside class="quote no-group" data-username="jpj" data-post="3" data-topic="112697">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/ed655f/48.png" class="avatar"> jpj:</div>
<blockquote>
<p>run the test data for only one iteration</p>
</blockquote>
</aside>
<p>but I hope an example helps:</p>
<p>Let’s say I have 10,000 images. I split it into 8,000 images for my training set and 2,000 images for my test set (just an example). I then train my network for one epoch on the training set (with a batch size of 100 that would be 80 iterations). Here I use back-propagation to calculate the loss and give the loss to the optimizer (with the specified learning rate) to update the weight so the network learns.</p>
<p>After that I test the network by sending the test set through the network once, so one epoch (which would we 20 iterations with a batch size of 100) but here I do not use back-propagation! I do also not use the optimizer and I don’t even need a learning rate since I am not making any changes to the weights.<br>
I just get the output and check it against the labels to calculate my accuracy.</p>