TensorFlow
Theano
neural networks
loss function
automatic differentiation

TensorFlow or Theano how do they know the loss function derivative based on the neural network graph?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

TensorFlow and Theano do not "guess" the derivative of the loss function. They compute it through automatic differentiation by traversing the computation graph and applying known derivative rules for each primitive operation.

The key idea is that a neural network is built from many simple operations such as matrix multiplication, addition, activation functions, and reductions. If the framework knows the derivative rule for each operation, it can combine them with the chain rule to obtain gradients for the full network.

The Graph Is A Recipe Of Elementary Operations

A neural network loss is not treated as one giant mysterious formula. It is represented as a graph of smaller operations. For example, a dense layer and a mean squared loss might involve:

  • matrix multiplication
  • bias addition
  • activation
  • subtraction
  • squaring
  • averaging

Each of those primitive operations has a local gradient rule. The framework records how the loss depends on each intermediate value, then works backward through the graph.

This Is Automatic Differentiation, Not Symbolic Algebra

Automatic differentiation is different from symbolic differentiation and different from numerical finite differences.

  • symbolic differentiation manipulates formulas algebraically
  • numerical differentiation estimates slopes by tiny perturbations
  • automatic differentiation applies exact local derivative rules through program execution

For deep learning, reverse-mode automatic differentiation is the important one because you usually have many parameters and one scalar loss.

TensorFlow Example

In modern TensorFlow, GradientTape makes the process visible:

python
1import tensorflow as tf
2
3x = tf.Variable(3.0)
4
5with tf.GradientTape() as tape:
6    y = x * x + 2 * x
7
8grad = tape.gradient(y, x)
9print(grad.numpy())

TensorFlow did not symbolically manipulate x * x + 2 * x as text. It recorded the operations and then applied the derivative rules for multiplication and addition in reverse order.

Theano Worked The Same Way In Principle

Theano exposed a more explicitly symbolic graph-building style, but the core idea was still automatic differentiation over graph operations.

python
1import theano
2import theano.tensor as T
3
4x = T.scalar('x')
5y = x * x + 2 * x
6dy_dx = T.grad(y, x)

Here dy_dx is a symbolic graph representing the derivative computation. The graph form is symbolic, but the method is still automatic differentiation driven by per-operation gradient rules and the chain rule.

Local Gradient Rules Are The Secret

The frameworks know how to differentiate standard operations because each operation comes with a backward rule. For instance:

  • 'z = x + y contributes a gradient of 1 to both inputs'
  • 'z = x * y contributes y to x and x to y'
  • 'z = relu(x) contributes 1 where x > 0 and 0 otherwise'

By chaining those local derivatives along the graph edges, the framework can compute the derivative of the final loss with respect to every trainable parameter. That is backpropagation in practice.

Custom Gradients Make The Mechanism Obvious

Frameworks let you define custom gradient rules, which makes their internal model clearer. In TensorFlow:

python
1import tensorflow as tf
2
3@tf.custom_gradient
4def cube(x):
5    y = x ** 3
6
7    def grad(dy):
8        return dy * 3 * x * x
9
10    return y, grad

This shows that the framework is not discovering derivatives by magic. It either uses a built-in local gradient rule or a custom one you provide.

Why This Works For Large Neural Networks

Neural networks are enormous compositions of repeated simple operations. Automatic differentiation scales because it never needs to invent a closed-form derivative for the whole model at once. It only needs the graph and the local derivative definitions. Reverse-mode autodiff then reuses intermediate results efficiently while propagating gradients from the scalar loss back to the parameters.

That is why these frameworks can differentiate very deep models without you manually deriving the gradient of every parameter by hand.

Common Pitfalls

One common mistake is thinking the framework differentiates formulas the way a symbolic math package does. It does not. Another is assuming backpropagation is something separate from automatic differentiation when, in these frameworks, backpropagation is essentially reverse-mode autodiff applied to the neural network graph. Developers also sometimes forget that some operations are not differentiable or do not have registered gradients, which is why custom gradient support exists. Finally, numerical gradient checking is useful for validation, but it is not how training frameworks compute gradients in practice.

Summary

  • TensorFlow and Theano compute loss derivatives with automatic differentiation.
  • The graph is a composition of elementary operations, each with a known local gradient rule.
  • Reverse-mode autodiff applies the chain rule backward from the scalar loss.
  • The symbolic feel of older graph APIs does not mean the frameworks rely on symbolic algebra as their core method.
  • Custom gradients work because the frameworks are fundamentally built around explicit backward rules per operation.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design