Tensorflow 2.0 doesn't compute the gradient
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

