Tensorflow weight initialization
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Weight initialization affects how easily a neural network starts learning. In TensorFlow and Keras, you usually do not need to invent an initialization scheme from scratch, but you do need to choose one that matches the layer type and activation function.
Why Initialization Matters
If weights start too small, activations and gradients can shrink toward zero. If they start too large, activations can explode and training becomes unstable. Good initializers try to keep signal magnitude in a reasonable range as data moves forward and gradients move backward.
That is why initialization is tied to layer shape and activation behavior, not just to random numbers.
The Common Built-In Initializers
TensorFlow exposes initializers through tf.keras.initializers. The ones you will see most often are:
- '
GlorotUniformorGlorotNormal, also called Xavier initialization' - '
HeNormalorHeUniform, often used with ReLU-like activations' - '
RandomNormalandRandomUniformfor manual control' - '
ZerosandOnes, usually for biases or special cases rather than kernels'
A simple rule of thumb is:
- use Glorot for tanh-like or general dense networks
- use He initialization for ReLU-family activations
- use zeros for biases unless you have a specific reason not to
A Basic Keras Example
Here is a small model that sets initializers explicitly.
The hidden ReLU layers use He initialization because it is designed to preserve variance more effectively for ReLU-like activations. The output layer uses Glorot initialization, which is a sensible general-purpose choice.
Initializers for Standalone Variables
You can also initialize raw TensorFlow variables directly.
This is useful when writing custom layers or lower-level TensorFlow code outside the standard Dense or Conv layer constructors.
Matching Initializer to Activation
The initializer choice is not arbitrary. It reflects how the activation behaves.
- ReLU drops negative values, so He initialization often works better
- tanh and sigmoid are more symmetric, so Glorot is a common baseline
- very deep or unusual architectures may need custom schemes or empirical tuning
That does not mean other combinations never work. It means the default starting point should be informed by the activation rather than chosen randomly.
What Not to Do
Initializing every weight to zero is a classic mistake. If all neurons in a layer start with the same weights, they receive the same gradients and learn the same features. Randomized initializers break that symmetry.
Biases are different. Zero bias initialization is usually fine because symmetry problems mainly come from identical kernels, not from identical bias values.
Common Pitfalls
The most common mistake is using one initializer everywhere without considering the activation function.
Another mistake is initializing kernels to zeros, which prevents neurons in the same layer from learning distinct patterns.
A third pitfall is treating initialization as a cure-all. Bad learning rates, poor normalization, or unstable architectures cannot always be fixed by changing the initializer.
Summary
- Weight initialization strongly affects optimization stability and learning speed.
- Use TensorFlow's built-in initializers instead of inventing ad hoc random values.
- He initializers are a strong default for ReLU-family layers.
- Glorot initializers are a common general-purpose baseline.
- Keep bias initialization simple and avoid zero-initializing the kernels of trainable layers.
Related reading
- Tensorflow What are the output_node_names for freeze_graph.py in the model_with_buckets model?
- Tensorflow what does index denote in CUDA_1D_KERNEL_LOOPindex, nthreads op user
- Tensorflow What does tf.nn.separable_conv2d do?
- Tensorflow What exact formula is applied in tf.nn.sparse_softmax_cross_entropy_with_logits?
- Tensorflow What is the relationship between .ckpt file and .ckpt.meta and .ckpt.index , and .pb file
- tensorflow what's the difference between tf.nn.dropout and tf.layers.dropout
- Tensorflow When are variable assignments done in sess.run with a list?
- Tensorflow When should I use or not use feed_dict?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.