liveBook · Manning

Gradient

As explained in Deep Learning with R, Third Edition

A gradient is the tensor-valued derivative of a function whose inputs are tensors, with each coefficient indicating how the output changes with respect to one input coefficient.

A gradient tells you how a function’s output changes when its tensor input changes. In neural-network training, the function maps model weights to a loss value. Its gradient points toward increasing loss, so moving the weights a little in the opposite direction can reduce that loss. This is more efficient than testing changes to every weight separately. section 2.4.2 section 2.4

A gradient for model weights

Start with an input vector x, a weight matrix W, and a target y_true. Compute y_pred from x and W, then measure its mismatch with y_true using a loss function. With x and y_true fixed, the loss is a function of W. The gradient has the same shape as W: each entry indicates the direction and magnitude of the loss change associated with changing that weight. section 2.4.2

one_training_step <- function(model, images_batch, labels_batch) {
  with(tf$GradientTape() %as% tape, {
    predictions <- model$call(images_batch)
    per_sample_losses <-
      op_sparse_categorical_crossentropy(labels_batch, predictions)
    average_loss <- op_mean(per_sample_losses)
  })
  gradients <- tape$gradient(average_loss, model$weights)
  update_weights(gradients, model$weights)
  average_loss
}

In this R training step, tf$GradientTape records the prediction and loss operations. tape$gradient() then obtains the average loss gradient for the model weights, which update_weights() uses to change them. section 2.5.2

For each mini-batch, gradient-based training follows a short cycle:

  • Run a forward pass and calculate the loss against the batch’s targets. section 2.4.3
  • Compute the loss gradient with respect to the weights in a backward pass. section 2.4.3
  • Update the weights in the opposite direction, scaled by a learning rate. A rate that is too small makes progress slow; one that is too large can make updates erratic. section 2.4.3

Gradients are useful beyond weight training. PyTorch can obtain a gradient with respect to an input image and use it for gradient ascent. Gradients can also be pooled and weighted to produce a class activation heatmap. section 10.2.2 section 10.3.4

How Deep Learning with R, Third Edition covers it

How deep it goes
Defines tensor gradients through a weight-matrix example and demonstrates automatic differentiation in a training step, without deriving backpropagation from scratch.
Examples
Uses a weight matrix and target loss, a TensorFlow training step, PyTorch image gradient ascent, and a class activation heatmap.
What it assumes
Introduces tensor gradients from scalar derivatives and smooth tensor operations without assuming prior knowledge of gradients.
What you can do after
Apply it via TensorFlow tf$GradientTape to compute loss gradients and update model weights.
How it connects
Connects gradients to differentiable tensor operations, loss functions, backpropagation, stochastic gradient descent, and learning rates.

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 R, Third Edition. The section links point to the parts of the book it draws on.