Concepts / Batch Processing in Neural Network Training

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.

  • Programming

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.

Pick a settinge.g. a learning rateTrain the modelwith that settingScore on validationsetheld-out tuning dataBetter than best sofar?Keep as best settingMore settings to try?Final test evaluationrun once, at the endyesnoyesno
What happens in each tuning round: train with a setting, score it on validation data, keep the best setting so far, and repeat until no candidates remain.

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.

Consulted manytimesConsulted onceGuides tuningdecisionsFinal unbiased judgeReused after eachchangeUsed only at the end
How the data flow differs: the validation set is reused during tuning, while the test set is touched only once at the end.

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.

Rule

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.

Batch 13 correct of 4Batch 24 correct of 4Batch 31 correct of 2Running total8 correct of 10add 3 then 4add 1divide by 10
How the per-batch correct counts add up to one accuracy value over the full dataset, and how the running total changes from one batch to the next.
Worked example

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?

  1. 1
    Count the chunks

    10 examples in chunks of 4 gives chunks of sizes 4, 4, and 2, so there are three iterations.

  2. 2
    Start the running total at zero

    Before the loop begins, the total number of correct predictions is 0.

  3. 3
    Add the first chunk

    After chunk 1 the running total is 0 + 3 = 3.

  4. 4
    Add 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.

  5. 5
    Add the third chunk

    After chunk 3 the running total is 7 + 1 = 8.

  6. 6
    Divide 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.

Result

The accuracy over the full 10-item dataset is 0.8, or 80 percent.

python
Notes on the code
  • correct_total starts 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_size happens once, after the loop

Mistakes That Bias Evaluation

  • Common mistake

    Tuning hyperparameters against the test set

    What it looks like

    Trying a learning rate, scoring on the test set, trying another, and keeping whichever scores highest.

    Why it fails

    The 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 instead

    Use 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 fails

    If 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 instead

    Keep 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 fails

    The 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 instead

    Assign 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 fails

    The 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 instead

    Divide the accumulated correct count by the size of the whole dataset, once, after the loop.

Checking Your Understanding

EASY

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?

  • 2

  • 3

  • 4

  • 10

Reveal answer
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.

Related concepts

More concepts starting with B