We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← JAX Autodiff step 22 of 25
Jacobian with jacrev
Compute a Jacobian with reverse-mode differentiation
A Jacobian contains the derivatives of every output with respect to
every input. jax.jacrev constructs it using reverse-mode
differentiation. For a vector input of length d and vector output of
length k, the result has shape (k, d): outputs in rows, inputs in
columns.
import jax
import jax.numpy as jnp
def f(x):
return jnp.array([x[0], x[0] + x[1]])
J = jax.jacrev(f)(jnp.array([1.0, 2.0]))
# [[1.0, 0.0],
# [1.0, 1.0]]
The first output depends only on x[0], giving the row [1, 0].
The second adds both inputs, giving [1, 1]. Pass the function itself
to jax.jacrev, then evaluate the returned function at x.
Choose a mode without changing the derivative
Reverse-mode builds Jacobian rows from output weights. Forward-mode
jax.jacfwd builds columns from input directions. For a full Jacobian,
reverse-mode often suits functions with many inputs and few outputs.
Actual time and memory use also depend on the function and compiler;
the number of inputs or outputs alone is not a complete cost model.
For the smooth functions here, both methods compute the same
mathematical Jacobian, allowing for floating-point differences.
Composing them, as in jax.jacrev(jax.jacfwd(f)), differentiates the
Jacobian again. For this exercise's three-output function, that would
produce second derivatives with shape (3, d, d), not the requested
first derivatives with shape (3, d).
Problem
Implement jacobian_mixed(x) using jax.jacrev. The input is a
one-dimensional real floating-point JAX array with shape (d,).
Define a function returning the three-element JAX array
[sum(x), sum(2*x), sum(x**2)].
Return its Jacobian with shape (3, d). The first row contains ones,
the second contains twos, and the third contains 2*x.
For x=[-2.0, 0.5], return:
[[ 1.0, 1.0],
[ 2.0, 2.0],
[-4.0, 1.0]]
Keep the function name jacobian_mixed used by the starter code. The
required calculation is a single reverse-mode Jacobian, not a
composition of differentiation methods. Practice using jacrev;
numerical tests cannot distinguish it from the equivalent formula.
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Compute a Jacobian with reverse-mode differentiation
A Jacobian contains the derivatives of every output with respect to
every input. jax.jacrev constructs it using reverse-mode
differentiation. For a vector input of length d and vector output of
length k, the result has shape (k, d): outputs in rows, inputs in
columns.
import jax
import jax.numpy as jnp
def f(x):
return jnp.array([x[0], x[0] + x[1]])
J = jax.jacrev(f)(jnp.array([1.0, 2.0]))
# [[1.0, 0.0],
# [1.0, 1.0]]
The first output depends only on x[0], giving the row [1, 0].
The second adds both inputs, giving [1, 1]. Pass the function itself
to jax.jacrev, then evaluate the returned function at x.
Choose a mode without changing the derivative
Reverse-mode builds Jacobian rows from output weights. Forward-mode
jax.jacfwd builds columns from input directions. For a full Jacobian,
reverse-mode often suits functions with many inputs and few outputs.
Actual time and memory use also depend on the function and compiler;
the number of inputs or outputs alone is not a complete cost model.
For the smooth functions here, both methods compute the same
mathematical Jacobian, allowing for floating-point differences.
Composing them, as in jax.jacrev(jax.jacfwd(f)), differentiates the
Jacobian again. For this exercise's three-output function, that would
produce second derivatives with shape (3, d, d), not the requested
first derivatives with shape (3, d).
Compute derivatives by following the operations
Automatic differentiation computes derivatives by applying the chain rule to a program's operations. It does not estimate them by evaluating nearby inputs, as finite differences do, or rearrange the whole expression using symbolic algebra.
For f(g(x)), the chain rule combines the derivative of g with the derivative
of f at g(x). Repeating this process lets a framework differentiate a long
sequence of operations.
Forward mode follows an input change
For a function with n inputs and m outputs, the Jacobian J is an (m, n)
matrix of derivatives. Entry (i, j) describes how output i changes with input
j.
Forward-mode differentiation computes Jv: the output's rate of change when the
input moves in direction v. For example, if f(x) = sum(x_i^2), then at
x = [1, 2] the gradient is [2, 4]. In direction v = [3, 0], the rate of
change is 2 * 3 + 4 * 0 = 6.
This is a directional derivative. It gives one Jacobian column only when v
selects one input coordinate, such as [1, 0].
Reverse mode works backward from an output
Reverse mode computes J^T u, where u weights the outputs. For a scalar loss,
choosing u = 1 gives the gradient with respect to all inputs. This makes reverse
mode useful for training: a model can have many parameters but just one loss.
Forward mode can be useful when you need sensitivity to only a few input directions. Neither mode needs to construct the full Jacobian for these products. That can save substantial memory. Higher-order differentiation can similarly compute a Hessian-vector product without storing the full matrix of second derivatives.
Changing gradient flow changes what is differentiated
A stop-gradient operation keeps a value in the forward calculation but treats it as constant during differentiation. A custom derivative supplies a derivative rule explicitly. It should match the intended function unless the exercise asks for a surrogate, such as an approximation used during training.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
JAX reference solution
Sign in to track your attempts toward unlocking the reference solution.