Automatic differentiation mechanically computes derivatives by tracing the elementary operations in a function. It replaces hand-derived, closed-form gradients when models contain many parameters or nonlinear functions. In PyTorch, specifying the model function lets Autograd construct and use the required gradient computation. section 3.5.2 section 3.5.1
Set requires_grad = True on variables whose gradients should be tracked. PyTorch then maintains a computation graph as expressions are created. Calling backward() on a loss traverses that graph and computes gradients for the tracked parameters. section 3.5.2 section 8.5
A typical gradient-based training loop is:
- Run the model’s forward pass to produce predictions. section 8.5
- Evaluate a loss function such as mean squared error. section 8.5
- Call loss.backward() to compute parameter gradients automatically. section 8.5
- Use an optimizer’s step operation to update weights and biases. section 8.5
From linear regression to neural-network training
For a linear model y_pred = b + w*x, Autograd computes gradients without explicitly writing the formulas for w and b. The same mechanism extends to a two-layer PyTorch network: compute predictions, evaluate MSELoss, clear previous gradients, call mse_loss.backward(), and run optimizer.step(). Repeating this loop reduces the loss and brings predictions closer to the observed targets.
Use the computation-graph figure to connect tracked operations with the gradients produced by Autograd. section 3.5.2





