tf.gradients sums over ys, does it?
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
Yes. tf.gradients computes gradients of the sum of ys with respect to xs unless you override that behavior with grad_ys. This is a key detail because many people expect a full Jacobian-like result, but tf.gradients is designed for backpropagation, where accumulated contributions are exactly what you want.
What tf.gradients Returns
In TensorFlow 1 style graph mode, tf.gradients(ys, xs) returns, for each x in xs, the total gradient contribution coming from all values in ys.
That means:
- if
ysis a list, the contributions are added together - if
ysis a tensor, TensorFlow seeds the backward pass with ones by default
So the default behavior is effectively "differentiate the sum of ys."
Scalar Example
At x = 2:
- derivative of
y1 = x^2is4 - derivative of
y2 = 3xis3
So the result is 7, which is the sum of both contributions.
Non-Scalar ys
If ys is a vector, the same idea applies:
By default, TensorFlow behaves as if the output vector were weighted by ones. So at x = 3, the result is:
- derivative of
x, which is1 - plus derivative of
x^2, which is6
Total: 7.
That is why tf.gradients is not the same thing as asking for every partial derivative separately.
Using grad_ys to Control the Weights
You can change the default all-ones weighting with grad_ys:
Now the backward pass computes:
- '
10 * d(x)/dx' - plus
0.5 * d(x^2)/dx
At x = 4, that becomes 10 * 1 + 0.5 * 8 = 14.
So grad_ys is the mechanism that lets you weight or seed each output contribution explicitly.
Why the API Works This Way
Backpropagation usually needs the gradient of a scalar objective such as a loss with respect to model parameters. Summing output contributions fits that training use case exactly, which is why tf.gradients is designed around accumulated gradients rather than a full Jacobian matrix.
If you need the full output-by-input derivative structure, use a Jacobian-oriented approach instead of expecting tf.gradients to return it automatically.
TensorFlow 2 Note
tf.gradients is a TensorFlow 1 style graph-mode API. In modern TensorFlow 2 code, the standard tool is tf.GradientTape.
For example:
Here the summation is explicit because you reduce the output yourself before asking for a gradient.
Common Pitfalls
The biggest mistake is expecting tf.gradients to return a full Jacobian. It returns accumulated gradients, not a structured matrix of every partial derivative.
Another mistake is forgetting that vector ys values are effectively weighted by ones when grad_ys is omitted.
People also try to use tf.gradients in eager TensorFlow 2 code and run into API-model mismatches. In current TensorFlow, prefer tf.GradientTape unless you are intentionally working with graph-mode compatibility APIs.
Finally, if you need per-output derivatives, compute them separately or use Jacobian-specific APIs rather than relying on the summed behavior of tf.gradients.
Summary
- Yes,
tf.gradientssums gradient contributions overys. - For non-scalar
ys, the default seed is effectively a tensor of ones. - '
grad_yslets you weight each output contribution explicitly.' - '
tf.gradientsis for accumulated backprop-style gradients, not full Jacobians.' - In TensorFlow 2, prefer
tf.GradientTapeand make the scalar target explicit.
Related reading
- tf.keras.layers.MultiHeadAttention's argument key_dim sometimes not matches to paper's example
- tf.keras.optimizers.Adam and other optimizers with minimization
- TFLearn - Evaluate a model
- tf.newaxis operation in TensorFlow
- tfjs_binding.node not found in tensorflow installed folder
- TF.Keras model.predict is slower than straight Numpy?
- TfidfVectorizer in scikit-learn ValueError np.nan is an invalid document
- tflearn / tensorflow does not learn xor
.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.