liveBook · Manning

Backpropagation

As explained in Deep Learning with R, Third Edition

Backpropagation is an algorithm for computing gradients of a neural network's loss with respect to its parameters by applying the chain rule backward through the network.

A network makes a prediction, and a loss function measures how far that prediction is from its target. Backpropagation computes how the loss changes with respect to the network’s weights. An optimizer uses those gradients to adjust the weights toward a lower loss. section 1.6 section 2.4.4

One PyTorch training step

Consider a linear model, `torch$matmul(inputs, W) + b`, trained with mean squared error. Mark `W` and `b` with `requires_grad = TRUE`. After computing predictions and loss, call `loss$backward()`: `W$grad` and `b$grad` then hold the gradients needed for an update. section 3.4.2

learning_rate <- 0.1

training_step <- function(inputs, targets, W, b) {
  predictions <- model(inputs, W, b)  #1
  loss <- mean_squared_error(targets, predictions)  #1
  loss$backward() #2
  grad_loss_wrt_W <- W$grad  #3
  grad_loss_wrt_b <- b$grad  #3
  with(torch$no_grad(), { #3
    W$sub_(grad_loss_wrt_W * learning_rate)  #4
    b$sub_(grad_loss_wrt_b * learning_rate)  #4
  }) #4
  W$grad <- b$grad <- NULL  #5
  loss
}

The full training step separates backpropagation from the weight update. It updates parameters inside `torch$no_grad()` and clears their gradients afterward; otherwise, gradients accumulate across steps. section 3.4.2

Why can one backward call handle a network with many layers? Each operation, such as addition, matrix multiplication, or ReLU, has a derivative. The chain rule combines these derivatives to find how the final loss changes with each parameter. A computation graph represents the linked operations; backpropagation works backward from the loss through that graph. section 2.4.4

Backpropagation computes gradients; it does not choose the update size. In mini-batch stochastic gradient descent, each batch gets a forward pass, a loss calculation, a backward pass, and a parameter update in the opposite direction from the gradient. The learning rate controls the size of that update. Higher-level training calls can perform these steps without an explicit `backward()` call: a Keras digit classifier, for example, computes loss gradients for each batch during `fit()`. section 2.4.3 section 2.5

How Deep Learning with R, Third Edition covers it

How deep it goes
Works through the chain rule and computation graphs, then demonstrates a PyTorch training step without deriving every layer’s gradient manually.
Examples
Uses a two-layer network, a handwritten-digit classifier, and a PyTorch linear classifier’s training step.
What it assumes
Builds from differentiable tensor operations and introduces the chain rule before applying it to network loss.
What you can do after
Apply it via PyTorch `loss$backward()` to obtain gradients, update parameters, and clear gradients before the next step.
How it connects
Links loss functions, the chain rule, computation graphs, automatic differentiation, optimizers, and mini-batch stochastic gradient descent.

Also explained in

Compare how 17 books explain backpropagation →

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.