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





