Skip to main content

Overfitting vs Underfitting in Deep Learning: Key Differences

When training a deep learning model, you want it to learn patterns from the training data so it can make accurate predictions on new, unseen data. However, sometimes models learn too little or too much. This leads to underfitting or overfitting. Let’s break them down in simple terms, backed by examples, visuals, and some light math.

1. What Is the Goal of Training a Model?

Imagine you're trying to teach a model to predict house prices based on features like size, location, and number of rooms.

Your goal is to find a function f(x) that maps your input features x (like size, rooms) to a prediction ŷ (the house price), such that the prediction is close to the actual price y.

ŷ = f(x;θ)
MSE = (1/n)(yᵢ - ŷᵢ)²

2. Underfitting

Underfitting happens when your model is too simple to capture the patterns in the data. It doesn’t learn enough from the training data and performs poorly on both training and testing sets.

Example:

Let’s say you’re using a straight line (linear model) to fit this data:


Left chart shows underfitting: a straight line trying to fit a curve

  • Misses the curve in the data
  • Has high bias (too simplistic assumptions)
  • Makes large prediction errors

3. Overfitting

Overfitting happens when your model is too complex and learns not only the underlying pattern but also the noise in the training data. It performs very well on training data but poorly on unseen data.

Right chart shows overfitting: a very wiggly line fitting every point

  • Tries too hard to memorize data points
  • Fails to generalize to new data
  • Has high variance

4. The Sweet Spot: Good Fit

A well-trained model should lie in the middle ground:

Middle chart: the model fits the general trend well without memorizing noise

  • Captures the underlying trend
  • Ignores noise
  • Generalizes well to new data

5. Intuition with Bias-Variance Tradeoff

Definitions:

  • Bias: Error from incorrect assumptions $\text{(too simple model)}$
  • Variance: Error from model sensitivity to small changes in data
Total Error = Bias² + Variance + Irreducible Error
   Situation  Bias   VarianceError Type
   Underfitting  High   Low        High training + test error
   Overfitting  Low   High        Low training error, high test error
   Just Right  Low   Low        Low generalization error

6. Real-World Deep Learning Example

If you're training an image classifier using a convolutional neural network (CNN):

  • Underfitting: Your CNN has only 1 or 2 layers → can’t capture complex features like edges or textures.
  • Overfitting: Your CNN has 100+ layers and memorizes noise or shadows from training images.
MetricUnderfitting         Overfitting
   Train Accuracy  Low          High
   Test Accuracy  Low          Low
   Gap (Train - Test)  Small          Large

7. It’s Central to Generalization – the Whole Point of ML

In ML, we care about generalization — how well the model performs on data it hasn’t seen.

  • Underfitting and overfitting are both failures to generalize, just in opposite directions.
  • Your job is to find the right balance where the model learns enough but not too much.

Understanding this tradeoff helps:

  • Pick the right model complexity
  • Choose good architectures
  • Know when to regularize, simplify, or train longer

8. How to detect and fix

1) Fix Underfitting:

  • Use more complex models
  • Add more features
  • Train longer
  • Reduce regularization

2) Fix Overfitting:

  • Add more data
  • Use data augmentation
  • Apply regularization (L1, L2, Dropout)
  • Use early stopping
  • Simplify model (fewer layers, fewer neurons)

Comments

Popular

Understanding SentencePiece: A Language-Independent Tokenizer for AI Engineers

In the realm of Natural Language Processing (NLP), tokenization plays a pivotal role in preparing text data for machine learning models. Traditional tokenization methods often rely on language-specific rules and pre-tokenized inputs, which can be limiting when dealing with diverse languages and scripts. Enter SentencePiece—a language-independent tokenizer and detokenizer designed to address these challenges and streamline the preprocessing pipeline for neural text processing systems. What is SentencePiece? SentencePiece is an open-source tokenizer and detokenizer developed by Google, tailored for neural-based text processing tasks such as Neural Machine Translation (NMT). Unlike conventional tokenizers that depend on whitespace and language-specific rules, SentencePiece treats the input text as a raw byte sequence, enabling it to process languages without explicit word boundaries, such as Japanese, Chinese, and Korean. This approach allows SentencePiece to train subword models di...

Mastering the Byte Pair Encoding (BPE) Tokenizer for NLP and LLMs

Byte Pair Encoding (BPE) is one of the most important and widely adopted subword tokenization algorithms in modern Natural Language Processing (NLP), especially in training Large Language Models (LLMs) like GPT. This guide provides a deep technical dive into how BPE works, compares it with other tokenizers like WordPiece and SentencePiece, and explains its practical implementation with Python code. This article is optimized for AI engineers building real-world models and systems. 1. What is Byte Pair Encoding? BPE was originally introduced as a data compression algorithm by Gage in 1994. It replaces the most frequent pair of bytes in a sequence with a single, unused byte. In 2015, Sennrich et al. adapted BPE for NLP to address the out-of-vocabulary (OOV) problem in neural machine translation. Instead of working with full words, BPE decomposes them into subword units that can be recombined to represent rare or unseen words. 2. Why Tokenization Matters in LLMs Tokenization is th...

Using Gemini API in LangChain: Step-by-Step Tutorial

What is LangChain and Why Use It? LangChain  is an open-source framework that simplifies the use of  Large Language Models (LLMs)  like OpenAI, Gemini (Google), and others by adding structure, tools, and memory to help build real-world applications such as chatbots, assistants, agents, or AI-enhanced software. Why Use LangChain for LLM Projects? Chainable Components : Easily build pipelines combining prompts, LLMs, tools, and memory. Multi-Model Support : Work with Gemini, OpenAI, Anthropic, Hugging Face, etc. Built-in Templates : Manage prompts more effectively. Supports Multi-Turn Chat : Manage complex interactions with memory and roles. Tool and API Integration : Let the model interact with external APIs or functions. Let's Walk Through the Code: Gemini + LangChain I will break the code into  4 main parts , each showcasing different features of LangChain and Gemini API. Part 1: Basic Gemini API Call Using LangChain import os from dotenv import load_dotenv load_dot...