liveBook · Manning

Gradient

As explained in Deep Learning with JAX

The derivative of a function with respect to its inputs, used here in the optimization sense for training neural networks.

A gradient tells you how a function changes with respect to its inputs. In neural-network training, those inputs are usually model parameters, and the gradient points toward the steepest increase in the loss. Subtracting a scaled gradient moves the parameters toward lower loss. section 2.6 section 4.1

Use gradients in JAX as explicit function transformations:

  • Define a loss function that receives parameters and data, then obtain its gradients with grad(loss)(params, x, y). section 2.2 section 2.6.3
  • Update each parameter by subtracting the learning-rate-scaled gradient; an exponentially decaying learning rate makes later steps smaller. section 2.6.3
  • Choose value_and_grad when training needs both the loss value and its gradient. section 2.6.3
  • Use argnums to select differentiated arguments, has_aux for auxiliary results, and repeated grad calls for higher-order derivatives. section 4.4

A minimal parameter update

from jax import grad def update(params, x, y, learning_rate): grads = grad(loss)(params, x, y) return [(w - learning_rate * dw, b - learning_rate * db) for (w, b), (dw, db) in zip(params, grads)] The transformed function returns gradients for each parameter. The update applies them in the opposite direction, while leaving the loss definition separate from the optimization step. section 2.6.3

Visualizing gradient descent steps along the loss landscape

Visualizes repeated steps across a loss landscape, making the opposite-gradient direction and convergence toward a minimum concrete. section 2.6

Listing 2.11 Implementing the gradient update step
from jax import grad                                        #1
  
INIT_LR = 1.0                                               #2
DECAY_RATE = 0.95                                           #3
DECAY_STEPS = 5                                             #4
  
def update(params, x, y, epoch_number):
  grads = grad(loss)(params, x, y)                          #5
  
  lr = INIT_LR * DECAY_RATE ** (epoch_number / DECAY_STEPS) #6
  return [(w - lr * dw, b - lr * db)                        #7
          for (w, b), (dw, db) in zip(params, grads)]       #7

Provides a runnable update-step listing that pairs grad with parameter-wise learning-rate updates. section 2.6.3

How Deep Learning with JAX covers it

How deep it goes
Explains autodiff, updates, options, derivative modes, Jacobians, and Hessians, while deferring detailed framework comparisons elsewhere.
Examples
Demonstrates gradients with neural-network loss updates, a scalar function, a loss landscape, and TensorFlow gradient tapes.
What it assumes
Assumes basic differentiable functions and model parameters, while introducing autodiff without requiring manual derivative formulas.
What you can do after
Apply gradients via JAX grad and value_and_grad to implement parameter updates and monitor training loss.
How it connects
Connects gradients to loss functions, gradient descent, autodiff, JIT compilation, vectorization, Jacobians, Hessians, and optimizers.

Also explained in

Compare how 20 books explain gradient →

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.