Skip to content

← JAX Autodiff step 2 of 25

Medium Primitives

jacfwd vs jacrev

Jump to the task

Put each output's derivatives in a row

A Jacobian collects the first derivatives of a function with several outputs. If the input is a vector of length d and the output is a vector of length m, its shape is (m, d). Entry (i, j) tells you how output i changes when input j changes.

import jax
import jax.numpy as jnp

def f(x):
    return jnp.array([x[0] + x[1], x[0] * x[1]])

x = jnp.array([2.0, 3.0])
J_fwd = jax.jacfwd(f)(x)
J_rev = jax.jacrev(f)(x)
# Both give [[1.0, 1.0],
#            [3.0, 2.0]]

The first output is a sum, so both derivatives in its row are 1. For the product, changing x[0] has derivative x[1]=3, and changing x[1] has derivative x[0]=2.

Two ways to build the matrix

Forward-mode jax.jacfwd builds the Jacobian from input directions: one coordinate direction selects one column. Reverse-mode jax.jacrev builds it from output weights: selecting one output gives one row. JAX batches these derivative calculations, so this is not a promise of one separate Python call or device operation per row or column.

For a full Jacobian, forward-mode is often a good choice when there are few inputs and many outputs. Reverse-mode often suits many inputs and few outputs. These are guidelines, not timing guarantees; the function, compiler and hardware also affect cost. Both compute the same mathematical Jacobian for the smooth function here, though floating-point results need not be bitwise identical.

For array inputs and outputs, output axes come before input axes. A scalar output contributes no axes: for vector input x, both methods return shape (d,), just like jax.grad. There is no extra length-one output axis. The exercise below has three outputs, so it needs three rows.

Problem

Implement jacobian_fwd(x) using jax.jacfwd. The input is a one-dimensional real floating-point JAX array with shape (d,). Define a function that returns the three-element JAX array [sum(x), sum(x**2), sum(x**3)].

Return its Jacobian as a JAX array with shape (3, d). In order, the rows contain ones, 2*x, and 3*x**2. These formulas let you check your result while practicing automatic differentiation.

For x=[-2.0, 0.5], return:

[[ 1.0, 1.0 ],
 [-4.0, 1.0 ],
 [12.0, 0.75]]

Keep the output derivatives in rows, rather than transposing the result. Numerical checks cannot distinguish use of jacfwd from the same formula written by hand, but the exercise is intended as jacfwd practice.