Weight initialization affects how well and how fast a model learns. The key metrics to watch are training loss and validation loss. Good initialization helps the model start learning without getting stuck or exploding gradients. If loss decreases smoothly, initialization is likely good.
Weight initialization strategies in TensorFlow - Model Metrics & Evaluation
Weight initialization itself does not produce a confusion matrix. Instead, we observe training curves showing loss or accuracy over time. For example:
Epoch | Training Loss | Validation Loss
---------------------------------------
1 | 1.2 | 1.3
2 | 0.8 | 0.9
3 | 0.5 | 0.6
...
Smoothly decreasing loss means weights started well. If loss stays high or NaN, initialization may be poor.
Simple random initialization can cause gradients to vanish or explode, slowing learning. Using He initialization or Glorot (Xavier) initialization balances variance of activations and gradients, helping stable training.
For example, He initialization works well with ReLU activations, while Glorot suits sigmoid or tanh. Choosing the wrong one can make training slow or unstable.
Good: Training loss decreases steadily from the first epoch, validation loss follows closely, and accuracy improves smoothly.
Bad: Training loss stays flat or increases, validation loss is erratic, or loss becomes NaN. This suggests poor initialization causing unstable gradients.
- Vanishing gradients: Weights too small cause gradients to shrink, stopping learning.
- Exploding gradients: Weights too large cause gradients to grow uncontrollably.
- Ignoring activation function: Using wrong initialization for activation slows or breaks training.
- Overfitting signs: Good training loss but bad validation loss is not an initialization issue but model complexity.
Your model shows training loss stuck at 2.0 and validation loss is NaN from the start. Is your weight initialization good?
Answer: No. This suggests poor initialization causing unstable gradients or numerical issues. Try He or Glorot initialization matching your activation.
Practice
Solution
Step 1: Understand activation and initialization relation
ReLU activation benefits from initial weights that maintain variance through layers to avoid dying neurons.Step 2: Match initializer to activation
HeNormal initializer is designed for ReLU, scaling weights to keep variance stable.Final Answer:
HeNormal initializer -> Option DQuick Check:
ReLU uses HeNormal [OK]
- Choosing GlorotUniform for ReLU layers
- Using zeros initializer causing no learning
- RandomNormal without scaling causes slow training
Solution
Step 1: Recall TensorFlow initializer module
Initializers are accessed via tf.keras.initializers with exact class names.Step 2: Check correct class name and casing
The correct class name is GlorotUniform with capital G and U, called as a class with parentheses.Final Answer:
tf.keras.initializers.GlorotUniform() -> Option CQuick Check:
Correct class name and module path [OK]
- Using lowercase function names instead of class
- Missing parentheses after class name
- Using wrong module path tf.initializers
layer = tf.keras.layers.Dense(10, kernel_initializer=tf.keras.initializers.HeNormal()) print(layer.kernel_initializer)
Solution
Step 1: Identify initializer and layer output
The layer uses HeNormal initializer explicitly for kernel weights and has 10 output units.Step 2: Understand output shape of Dense layer
Dense layers output shape is (batch_size, units), so shape is (None, 10) before input is given.Final Answer:
HeNormal initializer object, output shape (None, 10) -> Option BQuick Check:
HeNormal used and output units = 10 [OK]
- Confusing output shape with kernel shape
- Assuming default initializer instead of HeNormal
- Mixing initializer types
layer = tf.keras.layers.Dense(5, kernel_initializer='he_normal') model = tf.keras.Sequential([layer]) model.build(input_shape=(None, 3)) print(layer.kernel_initializer())What is the cause of the error?
Solution
Step 1: Understand kernel_initializer attribute type
kernel_initializer is an initializer object, not a callable function without arguments.Step 2: Identify error cause
Calling kernel_initializer() without arguments causes error because it expects shape and dtype parameters.Final Answer:
Calling kernel_initializer as a function without arguments -> Option AQuick Check:
Initializer needs shape args, can't call empty () [OK]
- Confusing string initializer with object
- Ignoring required arguments for initializer call
- Assuming bias_initializer causes this error
Solution
Step 1: Match initializers to activation functions
HeNormal suits ReLU to maintain variance; GlorotUniform suits sigmoid/tanh to avoid saturation.Step 2: Understand impact on training
Proper matching prevents slow or stuck training and improves accuracy by keeping gradients healthy.Final Answer:
Use HeNormal for ReLU layers and GlorotUniform for sigmoid layers -> Option AQuick Check:
Match initializer to activation for best training [OK]
- Using same initializer for all activations
- Using zeros initializer causing no learning
- Using RandomNormal without scaling
Ready to prove it?
Pass the test and this pattern moves unknown
