Batch Processing in Neural Network Training
Hyperparameters are settings chosen before training, and a validation dataset is the held-out data used to find the best of them.
Choices You Make Before Training Starts
When you train a neural network, you make choices that the training process does not make for you. The learning rate, the number of training epochs, and the size of each batch are settings you pick before training starts. These settings are called hyperparameters, and finding good values for them is a central part of building a working model. This article answers a practical question: which data should you use to judge those choices, and why the answer is not always the dataset you expect.
Hyperparameters are settings chosen before training, and a validation dataset is the held-out data used to find the best of them. Keep that pairing in mind, because the rest of the article builds on it.
The Tempting Shortcut of Tuning on the Test Set
A very common first instinct goes like this. You already have a test set that the model never sees during training, so you should use it to decide which hyperparameter settings work best. You try a learning rate, check the score on the test set, try another learning rate, check again, and keep whichever one scores highest. This sounds like a reasonable process. Many learners reach this conclusion because they treat every held-out split as one generic kind of data that can be consulted as often as needed.
The instinct has a grain of truth. A validation dataset is in fact similar to a test set, so the two are easy to confuse. The mistake lies in assuming that because they look alike, they can be used in the same way. The difference is in how often each one is allowed to influence your decisions.
Validation Data in the Tuning Loop
A validation dataset is meant to be used many times while tuning. You adjust a model setting, train with it, and check the results on the validation data after each change. Because the validation set absorbs this repeated tuning, it is the dataset that guides your decisions. Each round of the loop keeps the best setting found so far and then tries the next candidate.
The loop in the diagram is the reason the validation set exists. Each pass reads the validation score and uses it to choose the next move. The test set is kept out of that loop entirely, so that at the end it still represents data the tuning process has never been shaped by.
How the Test Set Differs
A test set should be used precisely once, at the end, so that it gives an unbiased view of the finished model. Repeatedly consulting it biases the evaluation, and the final reported performance stops being an honest estimate. A validation set, by contrast, is consulted over and over during tuning. Both are held-out data and both look alike, but their roles are different.
Counting Correct Predictions Chunk by Chunk
Evaluating a model on a validation or test set requires computing its accuracy, which means counting how many predictions match the correct labels. With large datasets this often cannot be done in one step, because memory limits usually mean only a small portion of the dataset can pass through the model at a time. The accuracy computation therefore works with chunks and combines the results.
The approach iterates over a data loader. In each iteration the model receives one chunk, its predictions for that chunk are compared with the true labels, and the number of correct predictions is added to a running total. Each chunk has the same size as the batches used during training, so the same procedure works for datasets of any size.
Here N is the size of the whole dataset. The running total is divided by N only once, after the loop finishes. The final accuracy depends only on that running total and the overall dataset size, which is why the whole dataset never needs to sit in memory at once.
Accuracy over a 10-item dataset in batches of 4
A validation dataset has 10 labeled examples. The data loader yields chunks of size 4. The model gets 3 correct in the first chunk, 4 correct in the second, and 1 correct in the third. What is the accuracy over the full dataset?
- 1Count the chunks
10 examples in chunks of 4 gives chunks of sizes 4, 4, and 2, so there are three iterations.
- 2Start the running total at zero
Before the loop begins, the total number of correct predictions is 0.
- 3Add the first chunk
After chunk 1 the running total is 0 + 3 = 3.
- 4Add the second chunk
After chunk 2 the running total is 3 + 4 = 7. The model's predictions for this chunk never need to be kept once they are counted.
- 5Add the third chunk
After chunk 3 the running total is 7 + 1 = 8.
- 6Divide by the whole dataset size
accuracy = 8 / 10 = 0.8. The division uses 10, the size of the whole dataset, not the chunk size of 4 and not the number of chunks.
The accuracy over the full 10-item dataset is 0.8, or 80 percent.
correct_totalstarts at zero and is the running total described above- each pass of the loop handles one chunk, the same size as a training batch
- the division by
dataset_sizehappens once, after the loop
Mistakes That Bias Evaluation
Common mistake Tuning hyperparameters against the test set
What it looks likeTrying a learning rate, scoring on the test set, trying another, and keeping whichever scores highest.
Why it failsThe test set has now shaped the choice of setting. Its score no longer measures how the finished model will do on unseen data, so the reported performance is optimistic.
Do this insteadUse the validation set for every tuning decision and touch the test set once, at the end.
Common mistake Letting validation examples leak into training
Why it failsIf examples used for scoring also appear in training, the model is being scored partly on data it has already learned from, which inflates the result.
Do this insteadKeep the training, validation, and test sets separate so no example appears in more than one of them.
Common mistake Treating every held-out split as one generic kind of data
Why it failsThe validation set and the test set are similar in form, but they are meant to be used differently. Using them interchangeably is the root cause of the biased evaluations above.
Do this insteadAssign each split a role: validation for repeated tuning, test for one final evaluation.
Common mistake Dividing the running total by the number of chunks
Why it failsThe accuracy is a fraction of the whole dataset. Dividing by the number of chunks or by the chunk size gives a number that is not the fraction correct.
Do this insteadDivide the accumulated correct count by the size of the whole dataset, once, after the loop.
Checking Your Understanding
A validation set has 10 examples and the data loader uses chunks of size 4. How many chunks does one pass over the validation set require, and what is the size of the last chunk? Then explain which dataset the final division should use, and why.
Hints
- Divide 10 by 4 and count the leftover examples.
- The accuracy denominator is the size of the whole dataset, not the chunk size.
What do you think happens?
How many chunks does a 10-example dataset need when the chunk size is 4?
Reveal answer
3
The chunks hold 4, 4, and 2 examples. The last chunk is smaller because 10 is not a multiple of 4.
Key Takeaways
- Hyperparameters are settings chosen before training, and a validation dataset is the held-out data used to find the best of them.
- A validation set is consulted many times during tuning, while a test set should be used only once, at the end, to give an unbiased estimate.
- Accuracy over a large dataset is computed chunk by chunk: each chunk's correct count is added to a running total, and the total is divided by the size of the whole dataset.
- Common evaluation mistakes, such as tuning on the test set or leaking validation examples into training, make reported accuracy look better than it really is.