Update of the weights after Gradient Descent in TensorFlow
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In TensorFlow, weights are updated after gradient descent when an optimizer applies the computed gradients to trainable variables. The important idea is that TensorFlow does not change weights just because you computed a loss; the update happens only when gradients are explicitly applied.
The Core Update Rule
The basic gradient descent rule is:
w = w - learning_rate * gradient
TensorFlow follows that same idea, but usually through an optimizer object rather than a handwritten assignment.
The training loop has three conceptual steps:
- run the model and compute the loss
- compute gradients of the loss with respect to the weights
- apply those gradients to the weights
A Minimal Manual Example
Here is a small TensorFlow example showing the full process.
This example uses assign_sub to perform the actual update. That is the TensorFlow equivalent of writing w = w - ..., but it updates the tf.Variable in place.
The Usual Approach: Use An Optimizer
In real model code, you usually let an optimizer handle the variable update logic.
The important line is optimizer.apply_gradients(...). That is where the weight update happens.
What Happens In model.fit
When you use model.fit, Keras is doing the same logic for you internally.
Conceptually, the framework:
- runs the forward pass
- computes the loss
- computes gradients with
GradientTape - applies gradients through the optimizer
So if you ever wonder "when are the weights updated," the answer is: after each training step when the optimizer applies gradients to the trainable variables.
A Small Keras Model Example
After apply_gradients, the layer weights have changed.
Why Computing Gradients Is Not Enough
A very common misunderstanding is thinking that tape.gradient(...) already updates the weights. It does not. That call only computes the gradients.
The update happens only when you:
- call
assign_submanually - or call
optimizer.apply_gradients(...) - or use a higher-level training API that does it internally
That distinction is the heart of the question.
Different Optimizers Mean Different Update Details
Although the basic intuition is gradient descent, optimizers such as Adam, RMSprop, and momentum SGD use more than just the raw gradient. They keep internal state and modify the update rule.
But the structure is still the same:
- compute gradients
- apply optimizer-specific update to weights
So even when the math becomes more sophisticated, the update point in TensorFlow is still the optimizer application step.
Common Pitfalls
The most common mistake is computing the loss and gradients but never calling apply_gradients or a manual variable update. In that case, training appears to run but the weights never change.
Another mistake is forgetting that only tf.Variable objects or model trainable variables can be updated this way. Plain tensors are not mutable weights.
Developers also sometimes inspect weights before the optimizer step and conclude that TensorFlow did nothing. The order of operations matters.
Finally, remember that model.predict does not update weights. Only training steps do.
Summary
- In TensorFlow, weights are updated when gradients are applied to trainable variables.
- '
GradientTapecomputes gradients but does not update weights by itself.' - Manual updates use methods like
assign_sub. - Optimizer-based updates use
optimizer.apply_gradients(...). - '
model.fitperforms the same pattern internally during each training step.'

