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

How to Save and Retrieve a Vector Database using LangChain, FAISS, and Gemini Embeddings

How to Save and Retrieve a Vector Database using LangChain, FAISS, and Gemini Embeddings Efficient storage and retrieval of vector databases is foundational for building intelligent retrieval-augmented generation (RAG) systems using large language models (LLMs). In this guide, we’ll walk through a professional-grade Python implementation that utilizes LangChain with FAISS and Google Gemini Embeddings to store document embeddings and retrieve similar information. This setup is highly suitable for advanced machine learning (ML) and deep learning (DL) engineers who work with semantic search and retrieval pipelines. Why Vector Databases Matter in LLM Applications Traditional keyword-based search systems fall short when it comes to understanding semantic meaning. Vector databases store high-dimensional embeddings of text data, allowing for approximate nearest-neighbor (ANN) searches based on semantic similarity. These capabilities are critical in applications like: Question Ans...

Building an MCP Agent with UV, Python & mcp-use

Model Context Protocol (MCP) is an open protocol designed to enable AI agents to interact with external tools and data in a standardized way. MCP is composed of three components: server , client , and host . MCP host The MCP host acts as the interface between the user and the agent   (such as Claude Desktop or IDE) and plays the role of connecting to external tools or data through MCP clients and servers. Previously, Anthropic’s Claude Desktop was introduced as a host, but it required a separate desktop app, license, and API key management, leading to dependency on the Claude ecosystem.   mcp-use is an open-source Python/Node package that connects LangChain LLMs (e.g., GPT-4, Claude, Groq) to MCP servers in just six lines of code, eliminating dependencies and supporting multi-server and multi-model setups. MCP Client The MCP client manages the MCP protocol within the host and is responsible for connecting to MCP servers that provide the necessary functions for the ...

RF-DETR: Overcoming the Limitations of DETR in Object Detection

RF-DETR (Region-Focused DETR), proposed in April 2025, is an advanced object detection architecture designed to overcome fundamental drawbacks of the original DETR (DEtection TRansformer) . In this technical article, we explore RF-DETR's contributions, architecture, and how it compares with both DETR and the improved model D-FINE . We also provide experimental benchmarks and discuss its real-world applicability. RF-DETR Architecture diagram for object detection Limitations of DETR DETR revolutionized object detection by leveraging the Transformer architecture, enabling end-to-end learning without anchor boxes or NMS (Non-Maximum Suppression). However, DETR has notable limitations: Slow convergence, requiring heavy data augmentation and long training schedules Degraded performance on low-resolution objects and complex scenes Lack of locality due to global self-attention mechanisms Key Innovations in RF-DETR RF-DETR intr...