liveBook · Manning

Jax.value_and_grad

As explained in Deep Learning with Python, Third Edition

A JAX function that returns both a function value and its gradient in one call.

jax.value_and_grad creates a function that returns a loss value and its gradient together, avoiding a second loss computation. It mirrors jax.grad but returns a tuple: the value first, then gradients. section 3.5.3 section 3.5.1

Differentiate a squared value

Define `compute_loss(input_var)` to return `jnp.square(input_var)`, then wrap it with `grad_fn = jax.value_and_grad(compute_loss)`. Calling `output, gradient = grad_fn(input_var)` produces both the computed value and its gradient with respect to the argument. section 3.5.3

Use the same wrapper as the loss function grows:

  • Pass model state as the first argument when differentiating several variables; tuples, lists, dictionaries, and nested combinations are supported as JAX trees. section 3.5.3
  • Set `has_aux=True` when the loss function returns `(loss, output)`; the call then returns the loss, gradients, and auxiliary output. section 3.5.3
  • Keep training functions stateless by passing parameters in and returning updated parameters alongside the loss. section 3.5.4

Linear-classifier training step

Package prediction and mean squared error in `compute_loss(state, inputs, targets)`, where `state` contains `W` and `b`. Wrap it with `jax.value_and_grad`, unpack the gradients, subtract each gradient multiplied by `learning_rate`, and return `loss, W, b`. Repeating this stateless step trains the classifier. section 3.5.4

learning_rate = 0.1

@jax.jit                                                                #1
def training_step(inputs, targets, W, b):
    loss, grads = grad_fn((W, b), inputs, targets)                    #2
    grad_wrt_W, grad_wrt_b = grads
    W = W - grad_wrt_W * learning_rate                                #3
    b = b - grad_wrt_b * learning_rate                                 #3
    return loss, W, b                                                 #4

Shows the complete gradient-based update pattern, including JIT compilation, state unpacking, parameter updates, and returned values. section 3.5.4

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains single arguments, nested state trees, auxiliary outputs, and stateless updates, but does not derive automatic differentiation internals.
Examples
Uses a squared scalar loss, a multi-variable state example, auxiliary outputs, and a linear classifier trained by gradient descent.
What it assumes
Assumes basic JAX arrays and functions while teaching gradient calls, state trees, auxiliary returns, and training-step structure.
What you can do after
Build it from scratch by wrapping a loss function, extracting gradients, and applying stateless parameter updates.
How it connects
Connects jax.grad, loss functions, JAX trees, stateless functions, JIT compilation, and custom training loops.

Also explained in

Compare how 3 books explain jax.value_and_grad →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.