Hitesh Sahu
Hitesh SahuHitesh Sahu
  1. Home
  2. ›
  3. posts
  4. ›
  5. …

  6. ›
  7. 1 Neural Network

Loading ⏳
Fetching content, this won’t take long…


💡 Did you know?

🐙 Octopuses have three hearts and blue blood.

🍪 This website uses cookies

No personal data is stored on our servers however third party tools Google Analytics cookies to measure traffic and improve your website experience. Learn more

Loading ⏳
Fetching content, this won’t take long…


💡 Did you know?

🍌 Bananas are berries, but strawberries are not.
AI-DeepLearning

    AI-AgenticAI

    AI-DeepLearning
    • Deep Learning Path 🤖

    • Neural Network Hypothesis and Intuition

    • Forward Propagation in Neural Networks

    • Vectorized Neural Networks Model Representation

    • Examples and Intuitions I — Neural Networks as Logical Gates

    • Examples and Intuitions II — Building XNOR with a Hidden Layer

    • Multiclass Classification with Neural Networks

    • Cost Function for Neural Networks

    • Backpropagation Algorithm

    • Gradient Checking and Random Initialization

    • Training a Neural Network

    • Revision Cheat Sheet

    • AI-DeepLearning Index


    AI-GenAI

    AI-Infrastructure

    AI-Machine-Learning

    AI-Math

    AWS

    Azure

    Hobbies

    kubernetes

    Management

    Programming

    Terraform

    Z_Appendix

    0-root

Cover Image for Neural Network Hypothesis and Intuition
AI-DeepLearning

Neural Network Hypothesis and Intuition

Explore the hypothesis and intuition behind neural networks, including their structure, activation functions, and how they process inputs to produce outputs.

Data Science
Machine Learning
Deep Learning
Neural Networks
Artificial Intelligence
Computational Graphs
← Previous

Deep Learning Path 🤖

Next →

Training a Neural Network

Neural Networks Overview

The Feature Explosion Problem

Why do we need Neural Networks?

Suppose we have x1,x2,…,xnx_1, x_2, \dots, x_nx1​,x2​,…,xn​ as input features and we want to compute a hypothesis hθ(x)h_\theta(x)hθ​(x).

We can

g(θ0+θ1x1+θ2x2+⋯+θnxn)g(\theta_0+ \theta_1 x_1 + \theta_2 x_2 + \dots + \theta_n x_n)g(θ0​+θ1​x1​+θ2​x2​+⋯+θn​xn​)

For quadratic features

g(θ0+θ1x1+θ2x2+⋯+θnxn+θn+1x12+… )g(\theta_0+ \theta_1 x_1 + \theta_2 x_2 + \dots + \theta_n x_n + \theta_{n+1} x_1^2 + \dots)g(θ0​+θ1​x1​+θ2​x2​+⋯+θn​xn​+θn+1​x12​+…)

  • Quadratic terms grow roughly as n2/2n^2/2n2/2

so we will end up with 5000 additional features if we have 100 features.

For cubic features

g(θ0+θ1x1+θ2x2+⋯+θnxn+θn+1x12+⋯+θn+kx13+… )g(\theta_0+ \theta_1 x_1 + \theta_2 x_2 + \dots + \theta_n x_n + \theta_{n+1} x_1^2 + \dots + \theta_{n+k} x_1^3 + \dots)g(θ0​+θ1​x1​+θ2​x2​+⋯+θn​xn​+θn+1​x12​+⋯+θn+k​x13​+…)

  • Cubic terms grows as O(n3)O(n^3)O(n3)

  • So we will end up with 166,000 additional features if we have 100 features.

As the features become more complex, the number of parameters θ\thetaθ grows rapidly.

It becomes

  • computationally expensive to compute the hypothesis with many features.
  • Memory-Heavy to store all the parameters.
  • Prone to overfitting due to the large number of parameters.

In this case, we can use a neural network to compute the hypothesis more efficiently.

Practical Example: Image Recognition

Suppose we have a 100 × 100 pixel image as input.

  • Each pixel is a feature, so we have 10,000 features.
  • For RGB images, we have 3 color channels, so we have 30,000 features.

If we want to compute a hypothesis with quadratic features, we would have on the order of 450 million features, which is computationally infeasible.

Conclusion

Polynomial logistic regression: Works for small 𝑛𝑛n

  • Explodes combinatorially for large 𝑛𝑛n
  • Computationally impossible for large feature sets (like images)
  • We need a non-linear model that can capture complex relationships without explicitly generating all polynomial features.

Neural Networks as a Solution

Neural networks can compute complex hypotheses without explicitly generating all polynomial features.

NN Types and Applications

Comparison of Neural Network Types

Network Type Best For Memory Spatial Awareness
Feedforward Tabular data No No
CNN Images No Yes
RNN Sequences Yes Limited

Evolution of Architectures

Today, many RNN tasks are increasingly replaced by:

  • Transformers
  • Attention mechanisms
flowchart LR

    A[Feedforward Networks]

    A --> B[CNNs]

    A --> C[RNNs]

    C --> D[LSTM / GRU]

    D --> E[Transformers]

1. 🔀 Standard Feedforward Networks

Information moves only in one direction:

Also called:

  • Fully Connected Networks
  • Dense Neural Networks
  • Multi-Layer Perceptrons (MLP)
Input→Hidden Layers→Output\text{Input} \rightarrow \text{Hidden Layers} \rightarrow \text{Output}Input→Hidden Layers→Output

No cycles or memory.

flowchart LR

    A1[Input 1]
    A2[Input 2]
    A3[Input 3]

    A1 --> H1
    A1 --> H2

    A2 --> H1
    A2 --> H2

    A3 --> H1
    A3 --> H2

    H1[Hidden Layer]
    H2[Hidden Layer]

    H1 --> O[Output]
    H2 --> O

Often used for:

  • Housing price prediction
  • Online advertising
  • Credit scoring
  • Customer churn prediction
  • Fraud detection

Mathematical Representation

For one layer:

y=f(Wx+b)y = f(Wx + b)y=f(Wx+b)

Where:

  • xxx = input vector
  • WWW = weights
  • bbb = bias
  • fff = activation function

2. 🏞️ Convolutional Neural Networks (CNNs)

CNNs are specialized neural networks designed primarily used for image data

  • Exploit spatial structure in images

Instead of connecting every neuron to every pixel to CNNs detect patterns using

  • convolution filters
  • feature maps
flowchart TD

    A[Input Image]

    A --> B[Convolution Layer]

    B --> C[Feature Maps]

    C --> D[Pooling Layer]

    D --> E[Fully Connected Layer]

    E --> F[Prediction]

Mathematical Representation of Convolution

A filter slides across the image.

(I∗K)(x,y)=∑m∑nI(m,n)K(x−m,y−n)(I * K)(x,y) = \sum_m \sum_n I(m,n)K(x-m,y-n)(I∗K)(x,y)=m∑​n∑​I(m,n)K(x−m,y−n)

Where:

  • III = image
  • KKK = kernel/filter
  • KKK = kernel/filter
flowchart LR

    A[Image]

    A --> B[Edges]

    B --> C[Textures]

    C --> D[Shapes]

    D --> E[Objects]

Use Case

  • Image classification
  • Face recognition
  • Medical imaging
  • Self-driving cars
  • Object detection

3. 🔢 Recurrent Neural Networks (RNNs)

RNNs process data step-by-step while remembering previous information.

  • Unlike feedforward networks, RNNs have memory.
  • They can capture temporal dependencies in data.

Used for sequence data

Examples:

  • Audio (time series)
  • Language (word-by-word sequence)

RNN Architecture

flowchart LR

    X1[Word 1] --> H1
    H1 --> H2

    X2[Word 2] --> H2
    H2 --> H3

    X3[Word 3] --> H3

    H1[Hidden State]
    H2[Hidden State]
    H3[Hidden State]

Mathematical Representation of RNNs

ht=f(xt,ht−1)h_t = f(x_t, h_{t-1})ht​=f(xt​,ht−1​)

Where:

  • xtx_txt​ = current input
  • ht−1h_{t-1}ht−1​ = previous hidden state
  • hth_tht​ = updated memory state

Use Cases for RNNs

Audio / Time Series

  • Speech recognition
  • Sensor data
  • Financial forecasting

Language Processing

  • Translation
  • Chatbots
  • Text generation
  • Next

4. Custom Neural Networks

Tailored for specific applications Used in complex systems like autonomous driving:

  • CNNs for images
  • Other components for radar
  • Combined into custom architectures

Neurons as Computational Units

At a simple level, neurons are computational units.

They:

  • dendrites(head): Take inputs x1,x2,…,xnx_1, x_2, \dots, x_nx1​,x2​,…,xn​
  • Process them: apply weights and activation function
  • axon(tail): Produce an output hθ(x)h_\theta(x)hθ​(x)

Artificial Neurons Model: Logistic Unit

In artificial neural networks, we model neurons as mathematical Logistic functions.


graph LR

subgraph Input Layer
x0(((x0)))    
x1(((x1)))
x2(((x2)))
x3(((x3)))
end

subgraph Activation Layer
a1{a1}
end

x0-->a1
x1-->a1
x2-->a1
x3-->a1


subgraph Output Layer
y(((hθx)))
end

a1-->y

A simple network looks like:

[x0x1x2x3]→Neuron→hθ(x)\begin{bmatrix} x_0 \\ x_1 \\ x_2 \\ x_3 \end{bmatrix} \rightarrow \text{Neuron} \rightarrow h_\theta(x)​x0​x1​x2​x3​​​→Neuron→hθ​(x)

In our machine learning model:

Inputs are features: x1,x2,…,xnx_1, x_2, \dots, x_nx1​,x2​,…,xn​

x=[x0x1x2⋮xn]x= \begin{bmatrix} x_0 \\ x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix}x=​x0​x1​x2​⋮xn​​​

Parameters θ\thetaθ are called weights

θ=[θ0θ1θ2⋮θn]\theta = \begin{bmatrix} \theta_0 \\ \theta_1 \\ \theta_2 \\ \vdots \\ \theta_n \end{bmatrix}θ=​θ0​θ1​θ2​⋮θn​​​

Bias unit

x0x_0x0​ is the bias unit/ bias Neuron and it is always equal to 1

  • For simplicity we dont draw x0x_0x0​

Output

Outputs of neurons are called activations Weights which is the hypothesis hθ(x)h_\theta(x)hθ​(x)

where hθ(x)=g(θTx)h_\theta(x) = g(\theta^T x)hθ​(x)=g(θTx)

And we can rewrite: z=θTxz = \theta^T xz=θTx

so the hypothesis can be expressed as: hθ(x)=g(z)h_\theta(x) = g(z)hθ​(x)=g(z)

g(z)g(z)g(z) is the activation function that introduces non-linearity into the model.

Activation Function g(⋅)g(\cdot)g(⋅)

g(z)g(z)g(z) is the activation function. for Hypothesis function:

  • Example: ReLU, sigmoid, tanh, etc.

Neural networks using sigmoid activation function for logistic regression:

hΘ(x)=g(z)h_\Theta(x) = g(z)hΘ​(x)=g(z)

Where

g(z)=11+e−zg(z) = \frac{1}{1 + e^{-z}}g(z)=1+e−z1​

Where z=θTxz = \theta^T xz=θTx

ReLU vs Sigmoid Activation Function

Sigmoid

Input:  -5    0    5
Output: 0.01 0.5 0.99

Outputs behave like probabilities.

Sigmoid squashes outputs between:

0 and 1

Its derivative becomes very small for large positive or negative values.

Vanishing gradient

Deep neural networks may contain:

  • dozens
  • hundreds
  • thousands

of layers.

During backpropagation:

  • gradients are multiplied repeatedly across layers.

If gradients are small:

0.1 × 0.1 × 0.1 × 0.1 ...

they rapidly approach zero.

Example

Suppose gradient values are:

0.2 × 0.2 × 0.2 × 0.2 × 0.2

Result:

0.00032

Very tiny gradients mean:

  • almost no weight updates
  • slow or stalled learning

Exploding vs Vanishing Gradients

Problem What Happens
Vanishing gradients Gradients become too small
Exploding gradients Gradients become too large

Rectified linear unit ReLU

ReLU activates only positive values. Negative values become zero.

Input:  -3  -1   0   2   5
Output:  0   0   0   2   5

ReLU helps because:

  • gradients remain stronger
  • training becomes faster
  • deep networks scale better

Enables: This enables:

  • deeper networks
  • faster convergence
  • stable learning

Which was not possible with sigmoid before

Feature ReLU (Rectified Linear Unit) Sigmoid
How it works If the signal is positive, it passes it forward; if negative, it outputs zero Converts output into a probability between 0 and 1
Example (Cat model) Strong whisker detection → output 3.5 (feature exists strongly) Final output → 0.92 → “92% chance this is a cat”
Output Range [0, ∞) [0, 1]
Best suited for Hidden layers; very fast and helps deep models learn efficiently Output layer for yes/no classification problems
Performance Computationally efficient Comparatively slower
Formula f(x) = max(0, x) f(x) = 1 / (1 + e^(-x))
Optimized for Avoiding vanishing gradients Predicting probabilities

Neural Network Structure

An Artificial Neural Network is a computational graph with layers of artificial neurons.

Neural networks are simply multiple logistic regression units stacked together.

Each layer:

  1. Takes activations from previous layer
  2. Multiplies by weight matrix
  3. Applies sigmoid activation
  4. Passes result forward
Input→Hidden→Output\text{Input} \rightarrow \text{Hidden} \rightarrow \text{Output}Input→Hidden→Output [x0x1x2x3]→[a1(2)a2(2)a3(2)]→hθ(x)\begin{bmatrix} x_0 \\ x_1 \\ x_2 \\ x_3 \end{bmatrix} \rightarrow \begin{bmatrix} a^{(2)}_1 \\ a^{(2)}_2 \\ a^{(2)}_3 \end{bmatrix} \rightarrow h_\theta(x)​x0​x1​x2​x3​​​→​a1(2)​a2(2)​a3(2)​​​→hθ​(x)
graph LR
    Input --> Hidden-Layer
    Hidden-Layer --> Output

1. Layer 1: Input layer

Takes features as Input

  • x1,x2,…,xnx_1, x_2, \dots, x_nx1​,x2​,…,xn​
  • x0x_0x0​ = 1 is bias unit that is not drawn

2. Layer 2: Hidden layer:

All Intermediate Layers between Input & Output Layer

  • Computes intermediate activations a1(2),a2(2),…,an(2),a^{(2)}_1, a^{(2)}_2, \dots , a^{(2)}_n,a1(2)​,a2(2)​,…,an(2)​,
  • a0(2)a^{(2)}_0a0(2)​ =1 is bias unit that is not drawn

ai(j)a^{(j)}_iai(j)​ = Activation output of iii th neuron in layer jjj

  • iii = Neuron Index inside that layer
  • jjj = layer number

Example:

  • a1(2)a_1^{(2)}a1(2)​ = first neuron in Second hidden layer
  • a3(2)a_3^{(2)}a3(2)​ = 3rd Neuron in Second hidden layer

Computing Hidden Layer Activations

Activation can be computed as :

a1(2)=g(Θ10(1)x0+Θ11(1)x1+Θ12(1)x2+Θ13(1)x3)a^{(2)}_1 = g(\Theta^{(1)}_{10}x_0 + \Theta^{(1)}_{11}x_1 + \Theta^{(1)}_{12}x_2 + \Theta^{(1)}_{13}x_3)a1(2)​=g(Θ10(1)​x0​+Θ11(1)​x1​+Θ12(1)​x2​+Θ13(1)​x3​) a2(2)=g(Θ20(1)x0+Θ21(1)x1+Θ22(1)x2+Θ23(1)x3)a^{(2)}_2 = g(\Theta^{(1)}_{20}x_0 + \Theta^{(1)}_{21}x_1 + \Theta^{(1)}_{22}x_2 + \Theta^{(1)}_{23}x_3)a2(2)​=g(Θ20(1)​x0​+Θ21(1)​x1​+Θ22(1)​x2​+Θ23(1)​x3​) a3(2)=g(Θ30(1)x0+Θ31(1)x1+Θ32(1)x2+Θ33(1)x3)a^{(2)}_3 = g(\Theta^{(1)}_{30}x_0 + \Theta^{(1)}_{31}x_1 + \Theta^{(1)}_{32}x_2 + \Theta^{(1)}_{33}x_3)a3(2)​=g(Θ30(1)​x0​+Θ31(1)​x1​+Θ32(1)​x2​+Θ33(1)​x3​)

Where g(.) is sigmoid

Weight Matrices Θ(j)\Theta^{(j)}Θ(j)

Weight Matrix of jthjthjth layer

  • weight matrix maps layer jjj to layer j+1j+1j+1
  • Each jjj th layer gets its own matrix of weights Θ(j)\Theta^{(j)}Θ(j)
  • Layers are indexed starting from 1, not 0.

Weight Matrix Dimensions

Outputlayer Neurons x (InputLayer Neurons + 1) Dimensioned Matrix

  • Input side includes bias
  • Output side does NOT include bias

Θ(j)\Theta(j)Θ(j) would be a Matrix of dimension:

s(j+1)X(sj+1)s_{(j+1)} X (s_j+1)s(j+1)​X(sj​+1) Θ(j)∈Rsj+1×(sj+1)\Theta^{(j)} \in \mathbb{R}^{s_{j+1} \times (s_j + 1)}Θ(j)∈Rsj+1​×(sj​+1)

Where:

  • sjs_jsj​ = number of neurons/units in jjjth layer
  • s(j+1)s_{(j+1)}s(j+1)​ units in Output layer j+1j+1j+1

Practical Example:

Each Input Layer is densely connected to each Activation Function of next Layer:

  • Input layer: 3 units + 1 Bias
  • Hidden layer 1: 4 units + 1 Bias
  • Output layer: 1 unit

graph LR
    %% ===== Layer 1 =====
    subgraph "Layer 1: Input Layer"
        x0((x0 = 1))
        x1(((x1)))
        x2(((x2)))
        x3(((x3)))
    end

    %% ===== Layer 2 =====
    subgraph "Layer 2: Hidden Layer"
        a0{a0 = 1}
        a1{a1}
        a2{a2}
        a3{a3}
        a4{a4}
    end

    %% ===== Layer 3 =====
    subgraph "Layer 3: Output Layer"
        y(((hθx)))
    end

  
    %% Input → Hidden
    x0 --> a1
    x0 --> a2
    x0 --> a3
    x0 --> a4

    x1 --> a1
    x1 --> a2
    x1 --> a3
    x1 --> a4
  
    x2 --> a1
    x2 --> a2
    x2 --> a3
    x2 --> a4
  
    x3 --> a1
    x3 --> a2
    x3 --> a3
    x3 --> a4


%% Hidden → Output
    a0 --> y
    a1 --> y
    a2 --> y
    a3 --> y
    a4 --> y

Simplified

Showing Bias unit but only 1 connection for reference


graph LR
    x0(((x0))) --> a1{a1}
    x1(((x1))) --> a1{a1}
    x1 --> a2{a2}
    x1 --> a3{a3}
    x1 --> a4{a4}
        
    x2(((x2))) --> a1
    x2 --> a2
    x2 --> a3
    x2 --> a4
  
    x3(((x3))) --> a1
    x3 --> a2
    x3 --> a3
    x3 --> a4

    a0{a0} --> y(((hθx)))
    a1 --> y
    a2 --> y
    a3 --> y
    a4 --> y

Given

  • Input layer: x0,x1,x2,x3x_0, x_1, x_2, x_3 x0​,x1​,x2​,x3​
  • Hidden layer: a0,a1,a2,a3,a4a_0, a_1, a_2, a_3, a_4a0​,a1​,a2​,a3​,a4​
  • Output layer: hθ(x)h_\theta(x)hθ​(x)

Where:

  • x0=1x_0 = 1x0​=1 → bias unit for the input layer
  • a0=1a_0 = 1a0​=1 → bias unit for the hidden layer

Weight Matrix:

Layer 1 (S1S_1S1​)

Input Layer → Hidden Layer

Θ(1)∈R4×4\Theta^{(1)} \in \mathbb{R}^{4 \times 4}Θ(1)∈R4×4 Θ(1)=4X4Matrix\Theta^{(1)} = 4 X 4 Matrix Θ(1)=4X4Matrix

=> 4 (a_1, a_2, a_3, a_4) × 4 (x0,x1,x2,x3)(x_0, x_1, x_2, x_3)(x0​,x1​,x2​,x3​)

Layer 2 (S2S_2S2​)

Hidden Layer → Output Layer

Θ(2)∈R1×5\Theta^{(2)} \in \mathbb{R}^{1 \times 5}Θ(2)∈R1×5 Θ(2)=1X5Matrix\Theta^{(2)} = 1 X 5 MatrixΘ(2)=1X5Matrix

=> 1 (output neuron) × 5(a0,a1,a2,a3,a4)(a_0, a_1, a_2, a_3, a_4)(a0​,a1​,a2​,a3​,a4​)

Activation of Neurons in Layer 2

First Neuron in layer 2

a1(2)=g(Θ10(1)x0+Θ11(1)x1+Θ12(1)x2+Θ13(1)x3)a^{(2)}_1 = g(\Theta^{(1)}_{10}x_0 + \Theta^{(1)}_{11}x_1 + \Theta^{(1)}_{12}x_2 + \Theta^{(1)}_{13}x_3)a1(2)​=g(Θ10(1)​x0​+Θ11(1)​x1​+Θ12(1)​x2​+Θ13(1)​x3​)

Second Neuron in layer 2

a2(2)=g(Θ20(1)x0+Θ21(1)x1+Θ22(1)x2+Θ23(1)x3)a^{(2)}_2 = g(\Theta^{(1)}_{20}x_0 + \Theta^{(1)}_{21}x_1 + \Theta^{(1)}_{22}x_2 + \Theta^{(1)}_{23}x_3)a2(2)​=g(Θ20(1)​x0​+Θ21(1)​x1​+Θ22(1)​x2​+Θ23(1)​x3​)

Third Neuron in layer 2

a3(2)=g(Θ30(1)x0+Θ31(1)x1+Θ32(1)x2+Θ33(1)x3)a^{(2)}_3 = g(\Theta^{(1)}_{30}x_0 + \Theta^{(1)}_{31}x_1 + \Theta^{(1)}_{32}x_2 + \Theta^{(1)}_{33}x_3)a3(2)​=g(Θ30(1)​x0​+Θ31(1)​x1​+Θ32(1)​x2​+Θ33(1)​x3​)

Where

  • ggg: sigmoid activation function of collective term

3. Output layer

Gives us the final hypothesis hΘ(x)h_\Theta(x)hΘ​(x)

  • Hidden layer outputs become inputs to the next layer
  • Another weight matrix Θ(2)\Theta^{(2)}Θ(2) is applied
  • Then the sigmoid function is applied again

Output Layer Hypothesis

The final hypothesis is the first neuron of 3rd layer

hΘ(x)=a1(3)h_\Theta(x) = a^{(3)}_1hΘ​(x)=a1(3)​

Which is equals to

a1(3)=g(Θ10(2)a0(2)+Θ11(2)a1(2)+Θ12(2)a2(2)+Θ13(2)a3(2))a^{(3)}_1 = g(\Theta^{(2)}_{10}a^{(2)}_0 + \Theta^{(2)}_{11}a^{(2)}_1+ \Theta^{(2)}_{12}a^{(2)}_2+ \Theta^{(2)}_{13}a^{(2)}_3)a1(3)​=g(Θ10(2)​a0(2)​+Θ11(2)​a1(2)​+Θ12(2)​a2(2)​+Θ13(2)​a3(2)​)

Where

  • ggg: Sigmoid of final term
  • Θ(2)\Theta^{(2)}Θ(2) is weight Matrix for final Output Layer

🔢 Normalization Techniques

Normalization techniques are used to stabilize and accelerate the training of neural networks by normalizing the activations of layers. stabilize training

It helps:

  • stabilize training
  • speed up convergence
  • reduce sensitivity to initialization
  • prevent activations from becoming too large or too small

1. Batch Normalization (BatchNorm)

Batch Normalization is a technique that normalizes the activations of a layer across the batch dimension during training.

  • Introduced in 2015 and widely used in CNNs.

BatchNorm

  • BatchNorm → normalize across examples in a batch.

Intuition

"Let's compare each kid to the average height of the whole class."


  Entire Class
  ┌─────────────┐
  │ Alice       │
  │ Bob         │
  │ Chris       │
  │ ...         │
  └─────────────┘
          ↓
  Find class average
          ↓
  Adjust everyone

Illustration of BatchNorm process:

Statistics

For CNN input: (N,C,H,W)(N, C, H, W)(N,C,H,W)

BatchNorm computes mean and variance for each channel across:

(N,H,W)(N, H, W)(N,H,W)

Where:

  • NNN = batch size
  • CCC = number of channels
  • HHH = height
  • WWW = width
flowchart TD

    A["Batch of Images 🖼️🖼️🖼️"]
    A --> B["Compute Mean and Variance ✨"]
    B --> C["Normalize Activations 🔢"]
    C --> D["Scale and Shift 🔣"]
    D --> E["Next Layer"]

Formula

Given activations xxx:

Mean:

μB=1m∑i=1mxi\mu_B = \frac{1}{m}\sum_{i=1}^{m}x_iμB​=m1​i=1∑m​xi​

Variance:

σB2=1m∑i=1m(xi−μB)2\sigma_B^2 = \frac{1}{m}\sum_{i=1}^{m}(x_i-\mu_B)^2σB2​=m1​i=1∑m​(xi​−μB​)2

Normalize:

x^i=xi−μBσB2+ϵ\hat{x}_i = \frac{x_i-\mu_B}{\sqrt{\sigma_B^2+\epsilon}}x^i​=σB2​+ϵ​xi​−μB​​

Scale and shift:

yi=γx^i+βy_i = \gamma \hat{x}_i + \betayi​=γx^i​+β
  • γ\gammaγ and β\betaβ are learnable parameters that allow the model to restore the original distribution if needed.
  • ϵ\epsilonϵ is a small constant to prevent division by zero.
  • BBB denotes the batch of examples.
  • mmm is the number of examples in the batch.
  • xix_ixi​ is the activation of the iii-th example in the batch.

Advantages

  • Work Best when we have large batches
  • Works extremely well for CNNs
  • Faster convergence
  • Regularization effect

Limitations

  • Depends on batch size: Small batches reduce effectiveness
  • Requires synchronization in distributed training

Typical Use Cases

  • ResNet
  • VGG - EfficientNet
  • Most traditional CNNs

2. Layer Normalization (LayerNorm)

Layer Normalization normalizes across the features of a single sample, making it independent of batch size.

LayerNorm

  • LayerNorm → normalize across features

Popularized by Transformers.

  • Transformers / LLMs → LayerNorm (or RMSNorm)
  • RNNs / Sequence Models → LayerNorm

Intuition

"Let's compare your subjects with each other."

    
    Your Report Card
    
    Math       90
    Science    70
    English    80
    Art        60
          ↓
    Find YOUR average
          ↓
    Balance YOUR scores
 

Illustration of LayerNorm process:

flowchart TD

    A[Single Sample 🧩]
    A --> B[Compute Mean and Variance across Features ✨]
    B --> C[Normalize Activations 🔢]
    C --> D[Scale and Shift 🔣]
    D --> E[Next Layer]

Formula

For a hidden vector of size ddd:

μ=1d∑i=1dxi\mu = \frac{1}{d}\sum_{i=1}^{d}x_iμ=d1​i=1∑d​xi​ σ2=1d∑i=1d(xi−μ)2\sigma^2 = \frac{1}{d}\sum_{i=1}^{d}(x_i-\mu)^2σ2=d1​i=1∑d​(xi​−μ)2

Normalize:

x^i=xi−μσ2+ϵ\hat{x}_i = \frac{x_i-\mu}{\sqrt{\sigma^2+\epsilon}}x^i​=σ2+ϵ​xi​−μ​

Scale and shift:

yi=γx^i+βy_i = \gamma \hat{x}_i + \betayi​=γx^i​+β

Statistics Computed Across

For one sample we have ddd features:

[x1,x2,x3,…,xd][x_1, x_2, x_3, \ldots, x_d][x1​,x2​,x3​,…,xd​]

Mean and variance are computed across all features: (d)(d)(d) for that single sample.

No dependency on other samples in the batch.

Advantages

  • Works with batch size 1
  • Stable for sequence models
  • Same behavior during training and inference
  • Ideal for Transformers

Limitations

  • Usually weaker than BatchNorm for CNNs

Typical Use Cases

  • GPT
  • BERT
  • Llama
  • RNNs
  • Transformers

3. Group Normalization (GroupNorm)

Group Normalization divides channels into groups and normalizes within each group.

GroupNorm → normalize across groups of channels

GroupNorm

  • Designed to remove dependence on batch size.
  • Small-batch CNNs → GroupNorm

Intuition

"Let's compare your subjects in groups."


    Whole Class
    
    ┌─ Red Team ─┐
    │ Alice      │
    │ Bob        │
    │ Chris      │
    └────────────┘
    
    ┌─ Blue Team ─┐
    │ Emma        │
    │ John        │
    │ Lisa        │
    └────────────┘
    
    Each team
    finds its own average


Illustration of GroupNorm process:

flowchart TD

    A["Channels 📊"]
    A --> B["Divide into Groups 🧩"]
    B --> C["Compute Mean and Variance within Each Group ✨"] 
    C --> D["Normalize Activations 🔢"]
    D --> E["Scale and Shift 🔣"]
    E --> F[Next Layer]

Formula

Suppose CCC channels are divided into GGG groups.:

C=64C = 64C=64 G=4G = 4G=4

Each group contains:

CG=16 channels.\frac{C}{G} = 16 \ channels.GC​=16 channels.

Mean and variance are computed within each group:

μg=1m∑i=1mxi\mu_g = \frac{1}{m}\sum_{i=1}^{m}x_iμg​=m1​i=1∑m​xi​ σg2=1m∑i=1m(xi−μg)2\sigma_g^2 = \frac{1}{m}\sum_{i=1}^{m}(x_i-\mu_g)^2σg2​=m1​i=1∑m​(xi​−μg​)2

Normalize:

x^i=xi−μgσg2+ϵ\hat{x}_i = \frac{x_i-\mu_g}{\sqrt{\sigma_g^2+\epsilon}}x^i​=σg2​+ϵ​xi​−μg​​

Statistics Computed Across

For:

(N,C,H,W)(N,C,H,W)(N,C,H,W)

GroupNorm computes statistics across:

(CG,H,W)\left(\frac{C}{G}, H, W\right)(GC​,H,W)

within each sample.

Advantages

  • Independent of batch size
  • Works well with small batches
  • Excellent for detection and segmentation

Limitations

  • Slightly slower than BatchNorm
  • Less common than LayerNorm in Transformers

Typical Use Cases

  • Mask R-CNN
  • Object Detection
  • Segmentation Models
  • Memory-constrained CNN training

BatchNorm vs LayerNorm vs GroupNorm Comparison

Feature BatchNorm LayerNorm GroupNorm
Uses Batch Statistics ✅ ❌ ❌
Works with Batch Size = 1 ❌ ✅ ✅
Best for CNNs ✅ ❌ ✅
Best for Transformers ❌ ✅ ❌
Same Train/Inference Behavior ❌ ✅ ✅
Multi-GPU Friendly ⚠️ ✅ ✅

Hitesh Sahu
Written by Hitesh Sahu, a passionate developer and blogger.

Fri Feb 27 2026

Share This on

← Previous

Deep Learning Path 🤖

Next →

Training a Neural Network

AI-DeepLearning/1-Neural-Network
Let's work together
+49 176-2019-2523
hiteshkrsahu@gmail.com
WhatsApp
Skype
Munich 🥨, Germany 🇩🇪, EU
Playstore
Hitesh Sahu's apps on Google Play Store
Need Help?
Let's Connect
Navigation
  Home/About
  Skills
  Work/Projects
  Lab/Experiments
  Contribution
  Awards
  Art/Sketches
  Thoughts
  Contact
Links
  Sitemap
  Legal Notice
  Privacy Policy

Made with

NextJS logo

NextJS by

hitesh Sahu

| © 2026 All rights reserved.