TensorFlow
tf.gradients
machine learning
deep learning
Python

How tf.gradients work in TensorFlow

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

tf.gradients is a TensorFlow 1 style API for symbolic automatic differentiation. It does not immediately compute numeric gradients. Instead, it adds new gradient operations to the graph that represent the derivatives of one set of tensors with respect to another.

The Core Idea

If you ask for the gradient of y with respect to x, TensorFlow walks backward through the graph from y to x using the chain rule and builds the necessary gradient subgraph.

In TensorFlow 1 style code, that looks like this:

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5x = tf.compat.v1.placeholder(tf.float32, name="x")
6y = x * x + 3 * x
7
8grad = tf.gradients(y, x)[0]
9
10with tf.compat.v1.Session() as sess:
11    print(sess.run(grad, feed_dict={x: 2.0}))   # 7.0

grad is itself a symbolic tensor until the session runs it.

It Returns Symbolic Results, Not Immediate Numbers

This is the most important thing to understand. tf.gradients(...) does not behave like a normal Python math function. It creates graph nodes that describe how to compute the derivative later.

That is why it was a natural fit for TensorFlow 1 graph mode, where most of the program existed as a symbolic graph before execution.

In TensorFlow 2, the usual replacement is tf.GradientTape, which fits eager execution much better.

Multiple Inputs and Outputs

You can differentiate one output with respect to several inputs, or several outputs with respect to one input:

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5x = tf.compat.v1.placeholder(tf.float32)
6y = tf.compat.v1.placeholder(tf.float32)
7z = x * y + y * y
8
9grads = tf.gradients(z, [x, y])
10
11with tf.compat.v1.Session() as sess:
12    gx, gy = sess.run(grads, feed_dict={x: 2.0, y: 3.0})
13    print(gx, gy)   # 3.0 8.0

Here TensorFlow computes:

  • 'dz/dx = y'
  • 'dz/dy = x + 2y'

and evaluates them numerically when the session runs.

What Happens When No Gradient Path Exists

If there is no differentiable path from ys back to xs, TensorFlow may return None for that input. That is often a useful debugging clue rather than a random failure.

It means one of these is usually true:

  • the graph path is disconnected
  • the operation is not differentiable
  • the gradient was stopped intentionally with tf.stop_gradient

So None often says more about graph structure than about syntax.

The grad_ys Parameter

grad_ys lets you supply the upstream gradient explicitly. This matters when ys is not a scalar and you want to control how the chain rule starts.

In plain training code, you often ignore this because TensorFlow can use a default upstream gradient of ones for scalar losses. But for custom differentiation or weighted gradient flows, grad_ys becomes important.

Why tf.gradients Feels Different From GradientTape

tf.gradients belongs to graph mode. tf.GradientTape belongs to eager mode. They solve the same underlying problem, but with different programming models.

So when reading legacy TensorFlow code, expect:

  • placeholders
  • sessions
  • symbolic tensors
  • 'tf.gradients'

When writing new TensorFlow code, expect eager execution and GradientTape instead.

Common Pitfalls

  • Expecting tf.gradients to produce immediate numbers instead of symbolic tensors.
  • Forgetting that it is primarily a TensorFlow 1 graph-mode API.
  • Misinterpreting None gradients when the real issue is a disconnected or nondifferentiable graph path.
  • Using it in code that is otherwise written for eager TensorFlow 2 execution.
  • Confusing gradient construction with actual numeric evaluation in a session.

Summary

  • 'tf.gradients builds symbolic gradient operations in a TensorFlow 1 style graph.'
  • It computes derivatives by traversing the graph backward with the chain rule.
  • The returned values are symbolic until the graph is executed.
  • 'None gradients usually indicate no valid differentiable path.'
  • In modern TensorFlow 2 code, tf.GradientTape is usually the more natural API.

Course illustration
Course illustration

All Rights Reserved.