Tensorflow 2.0 doesn't compute the gradient
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
When TensorFlow returns None gradients, the cause is usually graph disconnection, non-trainable values, or operations outside GradientTape. This is one of the most common debugging issues in custom training loops. A systematic check of watched tensors, differentiable operations, and variable types usually resolves it quickly.
Minimal Working Gradient Example
Start from a known-good baseline.
If this works but your real model fails, the issue is in model wiring rather than TensorFlow installation.
Common Cause 1: Not Using tf.Variable
Gradients are computed for watched tensors, usually trainable variables. If you use Python numbers or constant tensors where trainable variables are expected, gradients can be missing.
Bad pattern:
Correct pattern:
Common Cause 2: Operations Outside Tape Scope
Only operations executed inside the active tape context are recorded.
If part of the forward pass runs before entering GradientTape, gradient flow breaks.
Common Cause 3: Non-Differentiable Ops
Some operations are not differentiable or behave poorly for gradient-based learning, such as hard indexing patterns, casts to integer, or string operations in the loss path.
If you must use such operations, keep them out of the trainable path or redesign the objective.
Common Cause 4: Numpy Break in the Middle
Using NumPy operations inside the forward pass can detach tensors from TensorFlow autodiff.
Problematic pattern:
Keep calculations in TensorFlow ops:
Watching Non-Variable Tensors Manually
If you need gradients with respect to non-variable tensors, watch them explicitly.
This is useful for input-gradient methods and saliency techniques.
Multi-Variable Model Debug Pattern
In model training, inspect gradient list before applying optimizer updates.
This quickly identifies disconnected layers.
tf.function and Shape Issues
tf.function can hide debugging signals by tracing graphs. During debugging, run eagerly first, verify gradients, then add tf.function for performance.
Also check shape and dtype consistency. Integer tensors in loss paths can silently block useful gradients.
Practical Debug Checklist
Use this order:
- verify trainable values are
tf.Variable - ensure forward pass is inside tape scope
- remove NumPy operations from differentiable path
- check for non-differentiable ops
- print missing gradient variable names
This sequence resolves most cases quickly.
Common Pitfalls
- Defining trainable parameters as constants instead of variables.
- Computing part of the forward pass outside
GradientTapescope. - Mixing NumPy operations into TensorFlow gradient path.
- Expecting gradients through non-differentiable integer or indexing logic.
- Applying optimizer without checking whether any gradients are
None.
Summary
- Missing gradients in TensorFlow usually come from graph disconnection, not random failure.
- Keep trainable computations inside
GradientTapeand in TensorFlow ops. - Use
tf.Variablefor trainable parameters andtape.watchfor custom cases. - Print gradient diagnostics before optimizer steps.
- Debug eagerly first, then reintroduce tracing and performance optimizations.
Related reading
- Tensorflow 2.0 How to change the output signature while using tf.saved_model
- Tensorflow 2.14.0 with CUDA not registering CUDA?
- Tensorflow 2.2.0 error Predictions must be 0 Condition x y did not hold element-wise while using Bidirectional LSTM layer
- Tensorflow 2.4.1 - Couldn't invoke ptxas.exe
- TensorFlow 2.0 How to get trainable variables from tf.keras.layers layers, like Conv2D or Dense
- TensorFlow 2.0 how to group graph using tf.keras? tf.name_scope/tf.variable_scope not used anymore?
- Tensorflow 2.0 how to transform from MapDataset after reading from TFRecord to some structure that can be input to model.fit
- Tensorflow 2.0 Keras is training 4x slower than 2.0 Estimator
.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.