Automatic Differentiation with jax.grad
Introduction
Welcome back to the third lesson of "JAX Fundamentals: NumPy Power-Up"! We're making fantastic progress on our journey to mastering JAX's most powerful capabilities. In our previous lessons, we explored JAX arrays and their immutable nature, then discovered how pure functions form the cornerstone of JAX's design. These concepts weren't just theoretical exercises — they were laying the groundwork for the truly transformative feature we'll explore today.
Today, we're diving into one of JAX's most celebrated features: automatic differentiation. This is where JAX begins to show its true power beyond just being a NumPy replacement. Automatic differentiation is the mathematical foundation that enables machine learning, optimization algorithms, and scientific computing applications to compute gradients effortlessly and accurately.
As you may recall from our previous lesson, pure functions are essential because they enable JAX's transformations to work reliably. Today, we'll see this principle in action as we use jax.grad to automatically compute derivatives of our pure functions. By the end of this lesson, you'll understand how to use jax.grad to compute gradients of scalar-output functions, evaluate these gradients at specific points, and even handle functions with multiple variables.
What is Automatic Differentiation?
Before we start computing gradients with code, let's understand what automatic differentiation actually is and why it's so revolutionary for numerical computing. In calculus, we learned to compute derivatives by hand using rules like the power rule, product rule, and chain rule. For simple functions like , finding the derivative is straightforward. But imagine trying to compute derivatives by hand for the complex functions found in modern machine learning models — functions with millions of parameters and hundreds of layers of computations!
Automatic differentiation (often abbreviated as autodiff) is a computational technique that allows computers to compute exact derivatives of functions defined by computer programs. Unlike numerical differentiation (which approximates derivatives using finite differences) or symbolic differentiation (which manipulates mathematical expressions), automatic differentiation computes exact derivatives by applying the chain rule systematically to the elementary operations in a program.
The key insight is that any complex function computed by a program can be broken down into a sequence of elementary operations like addition, multiplication, exponentials, and trigonometric functions. Each of these elementary operations has a known derivative, and automatic differentiation applies the chain rule to combine these derivatives automatically.
What makes this particularly powerful in JAX is that it works seamlessly with the pure functions we learned about previously. Since pure functions have no side effects and behave deterministically, JAX can safely analyze their computational structure and compute gradients without worrying about unpredictable behavior.
