liveBook · Manning

Automatic differentiation

As explained in Deep Learning with R, Third Edition

Automatic differentiation computes exact derivatives of programs composed of differentiable operations by systematically applying the chain rule.

Suppose a classifier predicts labels for a batch of images. To improve it, a training step needs the gradient of its loss with respect to its weights. Automatic differentiation computes those gradients from the tensor operations used to produce the loss, so you do not have to write each derivative by hand. section 2.5.2

The underlying idea is the chain rule: for a sequence of differentiable operations, combine the derivative of each operation to find how an earlier value affects a later one. A computation graph records those operations and their connections. Backpropagation uses the graph to work backward from the loss toward the weights. section 2.4.4

An image-classifier training step

Run the model and calculate the batch’s average loss inside a TensorFlow `tf$GradientTape` scope. Then call `tape$gradient(average_loss, model$weights)` to obtain gradients and pass them to the weight-update step. This replaces a handwritten gradient function—even when the model uses operations such as softmax and cross-entropy, whose derivatives are cumbersome to implement manually. section 2.5.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
}

The complete R training-step listing shows the tape around prediction and loss calculation, followed by gradient retrieval and weight updates. section 2.5.2

Automatic differentiation computes the gradients; updating the weights is a separate job, commonly handled by an optimizer. Gradient computation is a core capability of TensorFlow, JAX, and PyTorch, alongside tensor computation and distributed computation. section 2.5.2 section 3.1

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains the chain-rule and computation-graph basis, then implements gradient retrieval without deriving softmax or cross-entropy gradients.
Examples
Uses an R image-classifier training step that records prediction and loss operations with `tf$GradientTape`.
What it assumes
Introduces the chain rule and computation graphs before using a tape; examples assume familiarity with basic R function and tensor-operation syntax.
What you can do after
Apply it via TensorFlow’s `tf$GradientTape` to obtain gradients of average loss with respect to model weights.
How it connects
Connects the chain rule, computation graphs, backpropagation, loss functions, and optimizers.

Also explained in

Compare how 6 books explain automatic differentiation →

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.