Automatic Differentiation, Gradients, and Optimization Theory
Differentiation as a computational primitive
Why gradients matter
Training neural networks is the problem of minimizing an empirical risk objective by following gradients and curvature information. TensorFlow's automatic differentiation enables exact derivatives of the implemented computation, subject to floating-point arithmetic.
Gradient vocabulary
In deep learning, reverse-mode automatic differentiation is especially efficient when the model has:
Reverse-mode autodiff is efficient when computing the gradient of a scalar loss with respect to many parameters.
Correct answer: Few outputs and many parameters
Chain rule in deep networks
Backpropagation is repeated application of the chain rule. If , then . In deep nets, TensorFlow composes these local derivatives across layers and control flow.
Using GradientTape correctly
-
1
Enter a tape context and ensure the watched quantities are TensorFlow variables or explicitly watched tensors.
-
2
Compute the forward pass and scalar loss.
-
3
Call tape.gradient(loss, variables) to obtain gradients.
-
4
Inspect gradient norms to detect vanishing, exploding, or disconnected gradients.
-
5
Apply gradients using an optimizer or custom update rule.
Common pitfall
If a tensor is created outside the tape and is not a variable or explicitly watched, its gradient may be None.
What scalar quantity do we typically differentiate in supervised learning?
Optimization is usually performed on a scalar objective so gradients are well-defined and tractable.
Correct answer: The loss function
First-order vs second-order methods
First-order methods
- Use gradient information only
- Scale well to large models
- Often sufficient for deep learning
Second-order methods
- Use curvature information such as the Hessian
- Can improve convergence
- Are usually more expensive at scale
Why is the Hessian rarely formed explicitly for very large neural networks?
The Hessian scales poorly with parameter count, so large-scale training relies mainly on first-order or approximate second-order methods.
Correct answer: Because it is too expensive in memory and computation