CHAPTER 09
Intermediate
Training and Evaluating Models
Updated: May 16, 2026
6 min read
# CHAPTER 9
Training and Evaluating Models
1. Introduction
You have designed the perfect neural network and compiled it. But if you call.fit() without understanding how data flows into the model, you will likely build a model that memorizes the data rather than learning from it. Deep Learning training is a delicate balancing act involving Epochs, Batch Sizes, and Validation Data. In this chapter, we will master the training loop and learn how to diagnose sick models using training histories.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define Epochs, Batch Size, and Iterations.
- Implement Validation Splits to monitor real-time performance.
- Diagnose Overfitting and Underfitting from loss graphs.
- Plot training histories using Matplotlib.
- Stop training automatically using Early Stopping.
3. Epochs and Batch Size
Imagine a student reading a textbook to prepare for a final exam.-
Epoch: Reading the *entire* textbook from cover to cover one time.
epochs=10means the model reads the entire dataset 10 times.
-
Batch Size: A neural network cannot read the entire textbook at once (your GPU doesn't have enough RAM). It reads a few pages at a time.
batchsize=32means the model looks at 32 images, calculates the error, updates its Weights, and then grabs the next 32 images.
*If you have 3,200 images and a batchsize of 32, it will take 100 "steps" (iterations) to complete 1 Epoch.*
4. The Validation Split
If a student memorizes the textbook, they will score 100% on a practice quiz, but fail the real exam. To prevent this, we hide a portion of the data during training. In Keras, you can pass avalidation_split=0.2. This tells TensorFlow: "Take 20% of the training data and hide it. Do NOT use it to update Weights. At the end of every Epoch, test the model on this hidden 20% to see if it is actually learning, or just memorizing."
5. Mini Project: Train Image Classifier (with Validation)
Let's train a model and properly monitor its validation metrics.
python
6. Diagnosing Overfitting and Underfitting
When you savemodel.fit() to a variable named history, Keras saves the accuracy and loss for every single epoch. You can plot this to diagnose your model.
python
How to Read the Graph:
- Underfitting: The Training Loss and Validation Loss are both extremely high and not dropping. The model is too simple to learn the data. (Solution: Add more layers or neurons).
-
Overfitting: The Training Loss drops perfectly to
0, but the Validation Loss suddenly starts going UP. The model stopped learning general patterns and started memorizing the specific training images. It is failing on unseen data!
- Perfect Fit: Both lines drop down smoothly and level out together.
7. Early Stopping
If your model starts overfitting at Epoch 6, but you told it to run forepochs=50, it will waste time ruining itself. We can use a Callback to stop training the exact moment Validation Loss stops improving.
python
*Now, you can set epochs=1000 safely. TensorFlow will automatically stop it at the perfect moment!*
8. Common Mistakes
-
Setting Batch Size too high: If you set
batchsize=10000, the model takes massive steps and learns very slowly, and it might crash your computer's RAM. Stick to powers of 2 (32, 64, 128).
-
Evaluating on the Validation set: The Validation set is used *during* training to tune hyperparameters (like early stopping). You must keep a completely separate
Xtestset that the model never sees until the very finalmodel.evaluate().
9. Best Practices
-
Always use Early Stopping: In professional environments, data scientists rarely guess the exact number of epochs. They set a massive number (like 500) and rely entirely on the
EarlyStoppingcallback to save the best model.
10. Exercises
- 1. If you have 10,000 images and a batch size of 100, how many iterations (steps) will it take to complete 1 Epoch?
-
2.
Write the code to import
EarlyStoppingand configure it to monitorvalaccuracywith a patience of 5.
11. MCQ Quiz with Answers
Question 1
What happens during Overfitting?
Question 2
Which parameter in model.fit() dictates how many images the model processes before updating its Weights?
12. Interview Questions
- Q: Explain the exact relationship between the Training Set, the Validation Set, and the Test Set in a deep learning workflow.
- Q: How does reducing the batch size generally affect the training time and the stability of the loss curve?
13. FAQs
Q: Is 100% accuracy a good thing? A: Usually, no. If you see exactly1.0000 training accuracy, your model has almost certainly overfitted and memorized the data. Real-world data is noisy; a healthy model usually caps out somewhere below 100%.
14. Summary
Training a model is about controlling its learning curve. By carefully managing batch sizes to protect our RAM, reserving validation data to ensure honesty, and utilizing Early Stopping callbacks to prevent overfitting, we ensure that our models are robust, generalized, and ready for real-world predictions.15. Next Chapter Recommendation
So far, we have only stacked layers in a straight line using theSequential API. What if we want to build a model with two different inputs (e.g., an image AND text) that merge together? In Chapter 10: Working with TensorFlow Keras API, we will unlock the true power of the Keras Functional API.