Skip to main content

What is the Vanishing Gradient Problem in Deep Learning?

Vanishing Gradient is a common problem in training deep neural networks, especially in very deep architectures. It makes it difficult for the model to learn from data during training. 

What is Vanishing Gradient?

In deep learning, training happens through a method called backpropagation, where the model adjusts its weights using gradients (a kind of slope) of the loss function with respect to each weight. These gradients tell the model how much to change each weight to improve performance.

However, in deep neural networks (many layers), the gradients can get very small as they are propagated backward through the layers. This is called vanishing gradient.

As a result:

  • Early layers (closer to the input) receive almost no updates.
  • The network stops learning or learns very slowly.


When Does Vanishing Gradient Happen?

  1. Very Deep Networks: The more layers, the more chance gradients will shrink as they go back.
  2. Activation Functions:
    • Sigmoid or tanh squish inputs into small ranges (e.g., between 0 and 1 for sigmoid).
    • Their derivatives are also small.
    • Multiplying many small numbers together makes them even smaller.

Example:

Let’s say you have 10 layers, and the gradient at each layer is around 0.5. After 10 layers:

Final gradient = 0.5^10 = 0.000976

This tiny number means almost no learning for the early layers.


How to Solve Vanishing Gradient?

1. Use ReLU Activation (or variants)

  • ReLU (Rectified Linear Unit): f(x) = max(0, x)
  • Its derivative is either 1 (for positive inputs) or 0 (for negative).
  • No shrinking happens like with sigmoid.

Example in PyTorch:

Before:

x = torch.sigmoid(linear(x))


After:

x = torch.relu(linear(x))

Other ReLU variants: LeakyReLU, ELU, GELU(often used in transformers).


2. Use Batch Normalization

  • This technique normalizes the inputs to each layer.
  • Helps keep gradients in a stable range.
  • Often improves both speed and performance.

Example in PyTorch:

nn.Sequential( nn.Linear(128, 64), nn.BatchNorm1d(64), nn.ReLU() )

3. Use Proper Weight Initialization

Some methods set initial weights in a way that keeps gradient flow stable.

  • Xavier (Glorot) Initialization: Good for tanh
  • He Initialization: Good for ReLU

Example in PyTorch:

torch.nn.init.kaiming_normal_(layer.weight, nonlinearity='relu')

4. Use Residual Connections (Skip Connections)

Used in ResNet and similar architectures.

  • Skip connections let gradients flow directly across layers.
  • Solves vanishing (and exploding) gradient issues.

Concept: Instead of computing:

x = F(x)


do:

x = x + F(x)


Example in PyTorch:

class ResidualBlock(nn.Module):
    def __init__(self, dim):
        super().__init__()
        self.linear = nn.Linear(dim, dim)
        
    def forward(self, x):
        return x + F.relu(self.linear(x))

5. Use Shorter Networks or Pretrained Models

If you don't need a very deep network:

  • Use fewer layers.
  • Or use a pretrained model (like ResNet or BERT) that has already solved this issue.

Reference

  • Y. Bengio et al., "Learning long-term dependencies with gradient descent is difficult", IEEE Transactions on Neural Networks (1994)
  • K. He et al., "Deep Residual Learning for Image Recognition", CVPR (2016)
  • S. Ioffe and C. Szegedy, "Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift", ICML (2015)
  • PyTorch Official Document: https://pytorch.org/docs/stable/nn.html

Comments

Popular

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

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

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