Use of grads_ys parameter in tf.gradients - TensorFlow
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The grad_ys parameter in tf.gradients(ys, xs, grad_ys) is one of the most overlooked yet powerful features in TensorFlow's automatic differentiation system. It lets you control the initial gradient that flows backward through the computation graph. Understanding what it does and why you would use it is essential for implementing custom training loops, multi-task learning, and advanced gradient manipulation techniques.
What tf.gradients Does
tf.gradients(ys, xs) computes the symbolic partial derivatives of the tensors in ys with respect to the tensors in xs. Internally, TensorFlow walks the computation graph backward from ys to xs, applying the chain rule at each operation node.
The function returns a list of tensors, one for each element in xs, where each tensor is the sum of all partial derivatives of ys with respect to that xs element.
What grad_ys Represents
The grad_ys parameter is the initial gradient, sometimes called the upstream gradient or the seed gradient. In the chain rule, when computing dL/dx where L is a downstream loss, you are really computing (dL/dy) * (dy/dx). The grad_ys parameter is that dL/dy term.
When you omit grad_ys (or pass None), TensorFlow defaults it to a tensor of ones with the same shape as ys. This means tf.gradients(y, x) computes dy/dx as if y were the final loss scalar, with an implicit upstream gradient of 1.0.
Practical Use Cases
Weighted Gradients for Multi-Task Learning
When you have multiple loss functions, grad_ys lets you weight their contributions to the final gradient without modifying the loss computation itself:
Gradient Scaling
You can scale all gradients by a constant factor, which is useful for gradient accumulation across micro-batches:
Stopping Gradient Flow Selectively
By setting certain elements of grad_ys to zero, you can block gradient flow from specific outputs:
TF2 Equivalent with GradientTape
In TensorFlow 2.x, tf.gradients is replaced by tf.GradientTape for eager execution. The equivalent of grad_ys is the output_gradients parameter in the gradient() method:
For multiple outputs with different weights:
Relationship to the Chain Rule
The mathematical foundation is straightforward. Given a computation chain L -> y -> x, the chain rule states:
dL/dx = (dL/dy) * (dy/dx)
In tf.gradients(y, x, grad_ys):
yandxdefine the (dy/dx) portiongrad_ysprovides the (dL/dy) portion- The return value is the full (dL/dx)
When grad_ys is None, TensorFlow substitutes dL/dy = 1, which is correct when y is the final loss and there is no further upstream computation.
Common Pitfalls
- Forgetting that
grad_ysdefaults to ones, not zeros, which means omitting it computes the full gradient rather than no gradient. - Passing a
grad_ystensor with a shape that does not matchys, which causes a shape mismatch error. - Confusing
grad_yswith gradient clipping;grad_ysscales the initial seed, not the final computed gradient. - Using
tf.gradientsinside eager mode in TF2 without wrapping it intf.function, since it operates on symbolic graphs. - Not realizing that when
ysis a list,grad_ysmust also be a list of the same length.
Summary
grad_ysis the upstream (initial) gradient seed passed into the backward pass oftf.gradients.- When omitted, it defaults to a tensor of ones, meaning "compute dy/dx directly."
- Use it for weighted gradients in multi-task learning, gradient scaling, and selective gradient blocking.
- In TensorFlow 2.x, the equivalent is the
output_gradientsparameter inGradientTape.gradient(). - Always ensure
grad_yshas the same shape and length asysto avoid runtime errors.

