Bird
Raised Fist0

Batch Normalization vs Layer Normalization - Why and When

THEORYadvancedInterview freq: very_high
Choose your preparation mode6 modes available
🤖
Batch Normalization vs Layer Normalization - Why and When
Understand when to use Batch Normalization versus Layer Normalization in neural networks.
theoryadvancedFreq: very_highDeep Learning
Prerequisites:BackpropagationTransformer Architecture
💡
Why It Exists

Batch Normalization was introduced to address the problem of internal covariate shift, where the distribution of each layer's inputs changes during training, slowing down convergence. Layer Normalization was developed to provide a similar stabilization effect in settings where Batch Normalization is less effective, such as in recurrent neural networks or when batch sizes are very small.

📖
What It Is

Batch Normalization normalizes the inputs across the batch dimension, while Layer Normalization normalizes across the features of each individual data point. Imagine Batch Normalization as adjusting the brightness of a group photo, while Layer Normalization adjusts the brightness of each individual portrait.

∑
Math Intuition
Normalization transforms inputs to have zero mean and unit variance, which stabilizes the learning process by reducing the sensitivity to the scale of the inputs. This means that the network can learn more robustly, as it doesn't need to adapt to varying input scales.
⚙️
Core Mechanism

Both techniques normalize inputs to have zero mean and unit variance, but differ in the dimensions over which they compute these statistics.

📋
Step by Step
  1. Batch Normalization: Compute mean and variance across the batch for each feature.
  2. Batch Normalization: Normalize each feature using the batch statistics.
  3. Batch Normalization: Scale and shift the normalized values using learnable parameters.
  4. Layer Normalization: Compute mean and variance across all features for each data point.
  5. Layer Normalization: Normalize, scale, and shift using learnable parameters for each data point.
🎯
Intuition
Think of Batch Normalization as ensuring that each feature behaves consistently across different data points, while Layer Normalization ensures that each data point is internally consistent across its features.
🐍
Python Example
Python
import numpy as np

def batch_norm(X, gamma, beta, eps=1e-5):
    mean = np.mean(X, axis=0)
    variance = np.var(X, axis=0)
    X_normalized = (X - mean) / np.sqrt(variance + eps)
    return gamma * X_normalized + beta

X = np.array([[1, 2], [3, 4], [5, 6]])
gamma = np.array([1, 1])
beta = np.array([0, 0])
print(batch_norm(X, gamma, beta))  # Output: normalized array
✅
When It Works
  • Batch Normalization is effective in deep feedforward networks with large batch sizes.
  • Layer Normalization is preferred in recurrent neural networks where batch statistics are less meaningful.
  • Layer Normalization is useful when batch sizes are small, such as in online learning or when memory is constrained.
⚠️
When It Breaks
  • Batch Normalization fails with very small batch sizes, as the statistics become unreliable.
  • Layer Normalization can be less effective in convolutional networks where spatial information is crucial.
  • Batch Normalization may introduce noise in recurrent networks, disrupting temporal dependencies.
⇄
vs Layer Normalization
Use This When
Use Batch Normalization when training deep networks with large batch sizes.
Use Alternative When
Use Layer Normalization in recurrent networks or with small batch sizes.
🎯
Interview Angles
  • Interviewer asks about the impact of batch size on normalization techniques → testing understanding of statistical reliability in Batch Normalization.
  • Interviewer presents a scenario with a recurrent neural network and asks which normalization to use → assessing knowledge of Layer Normalization's advantages in RNNs.
  • Interviewer asks why a model with Batch Normalization might still overfit → evaluating understanding of normalization's role in regularization.
⚠️
Common Mistakes
📌
Summary
  • Batch Normalization stabilizes learning by normalizing across the batch dimension, effective in large batch settings.
  • Layer Normalization normalizes across features for each data point, beneficial in RNNs and small batch scenarios.
  • Choosing the right normalization depends on the architecture and batch size, impacting training stability and performance.

Practice

(1/5)
1. Which dimensionality reduction technique is most suitable for visualizing high-dimensional data while preserving local structures?
easy
A. t-SNE
B. PCA
C. UMAP
D. LDA

Solution

  1. Step 1: Understand the purpose of each technique

    PCA is used for linear dimensionality reduction, UMAP is similar to t-SNE but faster, and LDA is for supervised dimensionality reduction.
  2. Step 2: Identify the technique that preserves local structures

    t-SNE is specifically designed to preserve local structures in high-dimensional data.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    t-SNE for local structures [OK]
Hint: t-SNE for local structures [OK]
Common Mistakes:
2. Given the following Python code snippet, what will be the output?
import numpy as np
business_goals = np.array(['increase sales', 'reduce churn', 'improve satisfaction'])
ml_problems = np.array(['classification', 'regression', 'clustering'])
result = np.char.add(business_goals, ml_problems)
print(result)
easy
A. ['increase salesclassification', 'reduce churnregression', 'improve satisfactionclustering']
B. ['increase sales', 'reduce churn', 'improve satisfaction']
C. ['classification', 'regression', 'clustering']
D. Throws an error due to incompatible types

Solution

  1. Step 1: Understand the operation

    The np.char.add function concatenates corresponding elements of two arrays element-wise.
  2. Step 2: Eliminate wrong options

    ['increase sales', 'reduce churn', 'improve satisfaction'] and C are incorrect because they ignore the concatenation operation. Throws an error due to incompatible types is incorrect because np.char.add handles string arrays correctly.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    np.char.add concatenates strings element-wise. [OK]
Hint: np.char.add concatenates strings element-wise. [OK]
Common Mistakes:
3. When choosing between K-Means and Gaussian Mixture Models (GMM) for clustering, which scenario favors K-Means?
medium
A. Data with overlapping clusters
B. Clusters with different covariance structures
C. Clusters with varying densities
D. Clusters with similar variance and spherical shape

Solution

  1. Step 1: Compare K-Means and GMM

    K-Means assumes spherical clusters with similar variance, while GMM can handle different shapes and densities.
  2. Step 2: Identify the scenario

    Clusters with similar variance and spherical shape describes clusters with similar variance and spherical shape, which is ideal for K-Means.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    K-Means loves spherical, similar variance clusters [OK]
Hint: K-Means loves spherical, similar variance clusters [OK]
Common Mistakes:
4. You have a model that is overfitting. Which approach is more suitable to address this issue?
medium
A. Add dropout layers to the model.
B. Reduce the size of the training dataset.
C. Use a smaller learning rate.
D. Increase the model complexity by adding more layers.

Solution

  1. Step 1: Identify methods to combat overfitting

    Dropout is a regularization technique that helps prevent overfitting by randomly dropping units during training.
  2. Step 2: Eliminate wrong options

    Increase the model complexity by adding more layers. increases overfitting risk, Reduce the size of the training dataset. reduces data, worsening generalization, and Use a smaller learning rate. affects convergence speed, not overfitting directly.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Dropout reduces overfitting [OK]
Hint: Dropout reduces overfitting [OK]
Common Mistakes:
5. You are tasked with evaluating a model for a rare disease detection system. Which metric would you prioritize and why?
hard
A. ROC-AUC, because it provides a balanced view of performance.
B. Accuracy, because it is straightforward to interpret.
C. Log Loss, because it penalizes incorrect predictions.
D. PR-AUC, because it focuses on the minority class performance.

Solution

  1. Step 1: Consider the context

    In rare disease detection, the minority class (disease cases) is critical.
  2. Step 2: Evaluate metric suitability

    PR-AUC is more informative for minority class performance. ROC-AUC (A) and Accuracy (C) can be misleading. Log Loss (D) is useful but doesn't focus on minority class.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    PR-AUC for minority class focus. [OK]
Hint: PR-AUC for minority class focus. [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard