liveBook · Manning

Gradient descent

As explained in Deep Learning with JAX

An optimization step that updates model parameters using gradients, typically in the opposite direction of the loss gradient.

A gradient update adjusts model parameters to reduce loss. Compute the loss and its gradients, then move weights in the opposite direction. In regression, a linear model y = wx + b uses mean squared error; repeated updates form the training loop. section 4.2

A practical update follows this pattern:

  • Evaluate the loss and obtain gradients with value_and_grad(loss). section 7.3.2
  • For data-parallel training, sum corresponding gradients across devices with lax.psum. section 7.3.2
  • Apply w - learning_rate * gradient and repeat across batches and epochs. section 4.2 section 7.3.2
  • Keep parameter copies synchronized: each device computes locally, receives aggregated gradients, and performs the same update. section 7.3.2

Updating a replicated classifier

An update function receives replicated parameters and per-device image batches. It computes categorical cross-entropy and gradients, aggregates each weight and bias gradient with lax.psum, applies a decaying learning rate, and returns updated parameters with per-device loss values. A complete loop reshapes a 256-example batch into eight groups of 32 before calling update. section 7.3.2

Listing 7.27 Loss and update functions
from functools import partial
 
INIT_LR = 1.0
DECAY_RATE = 0.95
DECAY_STEPS = 5
NUM_EPOCHS  = 20
 
def loss(params, images, targets):                           #1
  """Categorical cross entropy loss function."""
  logits = batched_predict(params, images)
  log_preds = logits - logsumexp(logits)
  return -jnp.mean(targets*log_preds)
 
@partial(jax.pmap, 
  axis_name='devices', 
  in_axes=(None, 0, 0, None), 
  out_axes=(None,0))                                         #2
def update(params, x, y, epoch_number):
  loss_value, grads = value_and_grad(loss)(params, x, y)
  grads = [(jax.lax.psum(dw, 'devices'),                     #3
    jax.lax.psum(db, 'devices'))                             #3
    for dw, db in grads]                                     #3
  lr = INIT_LR * DECAY_RATE ** (epoch_number / DECAY_STEPS)
  return [(w - lr * dw, b - lr * db)
          for (w, b), (dw, db) in zip(params, grads)], loss_value
#1 The same loss function as before
#2 Uses pmap to parallelize function and map over the data only; parameters are replicated and not mapped over
#3 Accumulates gradients on each of the devices

Inspect the JAX loss and update listing, including value_and_grad, pmap, lax.psum, learning-rate decay, and parameter arithmetic. section 7.3.2

How Deep Learning with JAX covers it

How deep it goes
Explains the loss-gradient relationship and update loop, then implements gradient aggregation and learning-rate decay, without deriving autodiff internals.
Examples
Uses synthetic time-series regression and replicated image batches with eight devices and 32 examples per device.
What it assumes
Assumes model, loss, training-data, and parameter basics while introducing the gradient-update loop through regression and classification examples.
What you can do after
Builds a JAX training update that computes gradients, aggregates them across devices, and applies synchronized parameter changes.
How it connects
Connects model parameters, loss functions, gradients, learning rates, training loops, pmap, and data-parallel training.

Also explained in

Compare how 17 books explain gradient descent →

Related concepts

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