Challenge - 5 Problems
BatchNorm Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of BatchNorm1d on a simple tensor
What is the output tensor after applying nn.BatchNorm1d to the input tensor below during training mode?
PyTorch
import torch import torch.nn as nn batch_norm = nn.BatchNorm1d(num_features=3) input_tensor = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=torch.float32) output = batch_norm(input_tensor) print(output)
Attempts:
2 left
💡 Hint
BatchNorm normalizes each feature across the batch to have mean 0 and variance 1 during training.
✗ Incorrect
BatchNorm1d normalizes each feature dimension independently. For the input, each feature's mean and variance are computed across the batch, then each value is normalized by subtracting the mean and dividing by the standard deviation. The output values are approximately ±1.2247 due to standard deviation sqrt(4.5).
🧠 Conceptual
intermediate1:30remaining
Purpose of running mean and variance in BatchNorm
What is the main purpose of the running mean and running variance in nn.BatchNorm layers during training and evaluation?
Attempts:
2 left
💡 Hint
Think about how BatchNorm behaves differently during training and evaluation.
✗ Incorrect
During training, BatchNorm uses batch statistics to normalize inputs. It also updates running mean and variance as moving averages. During evaluation, it uses these running statistics to normalize inputs instead of batch statistics.
❓ Hyperparameter
advanced1:30remaining
Effect of momentum parameter in nn.BatchNorm
In nn.BatchNorm, what effect does increasing the momentum parameter have on the running mean and variance?
Attempts:
2 left
💡 Hint
Momentum controls how much weight is given to new batch statistics versus old running statistics.
✗ Incorrect
Higher momentum means the running mean and variance incorporate more of the current batch's statistics, updating faster. Lower momentum means slower updates, relying more on past values.
🔧 Debug
advanced2:00remaining
Identifying error in BatchNorm usage
What error will occur when running the following code snippet and why?
import torch
import torch.nn as nn
batch_norm = nn.BatchNorm2d(num_features=3)
input_tensor = torch.randn(10, 5, 32, 32)
output = batch_norm(input_tensor)
PyTorch
import torch import torch.nn as nn batch_norm = nn.BatchNorm2d(num_features=3) input_tensor = torch.randn(10, 5, 32, 32) output = batch_norm(input_tensor)
Attempts:
2 left
💡 Hint
Check the channel dimension of the input tensor and the num_features parameter.
✗ Incorrect
nn.BatchNorm2d expects the input tensor to have shape (batch_size, num_features, height, width). Here, num_features=3 but input has 5 channels, causing a runtime error.
❓ Model Choice
expert2:30remaining
Choosing BatchNorm variant for sequence data
You are building a neural network to process variable-length sequences of word embeddings with shape (batch_size, sequence_length, embedding_dim). Which BatchNorm variant is most appropriate to normalize the embeddings across the batch and sequence length?
Attempts:
2 left
💡 Hint
Consider how BatchNorm1d expects input shape and what dimension to normalize.
✗ Incorrect
nn.BatchNorm1d expects input of shape (batch_size, num_features) or (batch_size, num_features, length). Reshaping to (batch_size*sequence_length, embedding_dim) allows normalization over embedding features. BatchNorm2d and 3d expect 4D or 5D tensors, not suitable here. LayerNorm is an alternative but not a BatchNorm variant.