Bird
Raised Fist0
TensorFlowml~20 mins

Batch normalization in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Batch Normalization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why use batch normalization in training?

Batch normalization helps improve training by:

  • Avoiding vanishing gradients
  • Reducing internal covariate shift
  • Increasing model capacity
  • Replacing dropout

Which is the main reason batch normalization is used?

AIt reduces internal covariate shift by normalizing layer inputs during training.
BIt increases the number of neurons in each layer automatically.
CIt replaces the need for activation functions like ReLU.
DIt prevents overfitting by randomly dropping neurons.
Attempts:
2 left
💡 Hint

Think about what happens to the input distribution of layers during training.

❓ Predict Output
intermediate
1:30remaining
Output shape after batch normalization layer

Given the following TensorFlow code, what is the shape of output?

TensorFlow
import tensorflow as tf
input_tensor = tf.random.normal([32, 28, 28, 3])
bn_layer = tf.keras.layers.BatchNormalization()
output = bn_layer(input_tensor)
output_shape = output.shape
A(32, 28, 28, 3)
B(28, 28, 3)
C(32, 3)
D(32, 28, 28)
Attempts:
2 left
💡 Hint

Batch normalization does not change the shape of the input tensor.

❓ Hyperparameter
advanced
2:00remaining
Choosing momentum value in batch normalization

In TensorFlow's BatchNormalization layer, the momentum parameter controls the moving average of mean and variance. Which statement about setting momentum is true?

AA higher momentum means the moving averages update quickly, relying more on current batch values.
BMomentum controls the learning rate of the batch normalization layer.
CA higher momentum means the moving averages update slowly, relying more on past values.
DMomentum is only used during training and ignored during inference.
Attempts:
2 left
💡 Hint

Think about how moving averages work with momentum values close to 1.

🔧 Debug
advanced
2:00remaining
Why does batch normalization cause error with batch size 1?

Consider this code snippet:

import tensorflow as tf
input_tensor = tf.random.normal([1, 10])
bn = tf.keras.layers.BatchNormalization()
output = bn(input_tensor)

Running this raises an error. Why?

ABatch normalization requires input values to be positive.
BBatch normalization requires batch size > 1 to compute variance; batch size 1 causes division by zero.
CBatch normalization only works with 4D tensors, not 2D tensors.
DBatch normalization cannot be used on tensors created with tf.random.normal.
Attempts:
2 left
💡 Hint

Think about how variance is calculated with only one sample.

❓ Metrics
expert
2:30remaining
Effect of batch normalization on training metrics

You train two identical neural networks on the same data. One uses batch normalization after each dense layer; the other does not. Which of the following is the most likely difference in training metrics?

ABatch normalization causes the training loss to increase and accuracy to decrease.
BThe network without batch normalization converges faster but has lower validation accuracy.
CBoth networks converge at the same speed but the one without batch normalization has better accuracy.
DThe network with batch normalization converges faster and achieves higher accuracy on training and validation sets.
Attempts:
2 left
💡 Hint

Batch normalization stabilizes and speeds up training.

Practice

(1/5)
1. What is the main purpose of BatchNormalization in a TensorFlow model?
easy
A. To add noise to the inputs for regularization
B. To normalize the inputs of a layer to speed up and stabilize training
C. To increase the learning rate automatically during training
D. To reduce the size of the model by removing neurons

Solution

  1. Step 1: Understand batch normalization role

    Batch normalization normalizes inputs to a layer to keep data distribution stable during training.
  2. Step 2: Identify its effect on training

    This normalization speeds up training and helps avoid issues like vanishing or exploding gradients.
  3. Final Answer:

    To normalize the inputs of a layer to speed up and stabilize training -> Option B
  4. Quick Check:

    BatchNormalization normalizes inputs [OK]
Hint: BatchNormalization normalizes inputs to stabilize training [OK]
Common Mistakes:
  • Thinking it reduces model size
  • Confusing it with learning rate adjustment
  • Assuming it adds noise for regularization
2. Which of the following is the correct way to add a batch normalization layer after a dense layer in TensorFlow?
easy
A. model.add(tf.keras.layers.BatchNormalization(64)) before model.add(tf.keras.layers.Dense())
B. model.add(tf.keras.layers.Dense(64, batch_normalization=True))
C. model.add(tf.keras.layers.BatchNormalization()) after model.add(tf.keras.layers.Dense(64))
D. model.add(tf.keras.layers.Dense(64)) with batch_norm=True parameter

Solution

  1. Step 1: Recall batch normalization usage

    BatchNormalization is a separate layer added after Dense, not a parameter inside Dense.
  2. Step 2: Check syntax correctness

    model.add(tf.keras.layers.BatchNormalization()) after model.add(tf.keras.layers.Dense(64)) correctly adds BatchNormalization after Dense with no extra parameters.
  3. Final Answer:

    model.add(tf.keras.layers.BatchNormalization()) after model.add(tf.keras.layers.Dense(64)) -> Option C
  4. Quick Check:

    BatchNormalization is a separate layer added after Dense [OK]
Hint: BatchNormalization is a separate layer, not a Dense parameter [OK]
Common Mistakes:
  • Trying to pass batch_normalization as Dense parameter
  • Adding BatchNormalization before Dense layer
  • Passing units to BatchNormalization layer
3. What will be the output shape of the following TensorFlow model snippet?
model = tf.keras.Sequential([
  tf.keras.layers.Dense(10, input_shape=(20,)),
  tf.keras.layers.BatchNormalization(),
  tf.keras.layers.Activation('relu')
])
output_shape = model.output_shape
medium
A. (None, 10)
B. (None, 20)
C. (10, None)
D. (20, 10)

Solution

  1. Step 1: Analyze Dense layer output

    Dense(10) with input shape (20,) outputs shape (None, 10), where None is batch size.
  2. Step 2: Check BatchNormalization and Activation effect

    BatchNormalization and Activation do not change shape, so output remains (None, 10).
  3. Final Answer:

    (None, 10) -> Option A
  4. Quick Check:

    Dense output shape = (None, 10) [OK]
Hint: BatchNormalization keeps shape same as previous layer output [OK]
Common Mistakes:
  • Confusing input shape with output shape
  • Assuming BatchNormalization changes output shape
  • Mixing batch size dimension with feature dimension
4. Identify the error in this TensorFlow code snippet using batch normalization:
model = tf.keras.Sequential()
model.add(tf.keras.layers.BatchNormalization(input_shape=(32,)))
model.add(tf.keras.layers.Dense(64))
model.add(tf.keras.layers.Activation('relu'))
medium
A. BatchNormalization should not be the first layer with input_shape
B. Activation should be before BatchNormalization
C. Dense layer must come before BatchNormalization
D. BatchNormalization layer should come after Dense layer, not before

Solution

  1. Step 1: Check layer order logic

    BatchNormalization normalizes outputs of previous layer, so it should come after Dense, not before.
  2. Step 2: Validate input shape usage

    Input shape should be specified in first layer that processes input, usually Dense or Conv layer.
  3. Final Answer:

    BatchNormalization layer should come after Dense layer, not before -> Option D
  4. Quick Check:

    BatchNormalization follows Dense layer [OK]
Hint: BatchNormalization normalizes outputs, so place it after Dense [OK]
Common Mistakes:
  • Placing BatchNormalization as first layer with input_shape
  • Putting Activation before BatchNormalization
  • Assuming BatchNormalization changes input shape
5. You want to add batch normalization to a convolutional neural network in TensorFlow. Which of the following is the best practice?
hard
A. Add BatchNormalization immediately after each convolutional layer and before activation
B. Add BatchNormalization only after the last convolutional layer
C. Add BatchNormalization before convolutional layers
D. Add BatchNormalization only after activation layers

Solution

  1. Step 1: Understand batch normalization placement

    Best practice is to normalize outputs of convolutional layers before applying activation.
  2. Step 2: Evaluate options

    Adding BatchNormalization after each conv layer and before activation stabilizes training and improves performance.
  3. Final Answer:

    Add BatchNormalization immediately after each convolutional layer and before activation -> Option A
  4. Quick Check:

    BatchNormalization follows conv layer, precedes activation [OK]
Hint: Place BatchNormalization after conv layer, before activation [OK]
Common Mistakes:
  • Adding BatchNormalization only once at end
  • Placing BatchNormalization before conv layers
  • Adding BatchNormalization after activation
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