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
















