Training a model requires knowing how a change in each parameter would change the loss. PyTorch’s automatic differentiation engine, autograd, calculates those derivatives without requiring a hand-derived formula for the model. Tensors retain a history of the operations and parent tensors that produced them; autograd uses that history to work back from the loss to the parameters. section 1.5 section 5.5.1
Gradient for a calibration model
Suppose a thermometer-calibration model predicts `w * t_u + b`, with loss `((t_p - t_c)**2).mean()`. Create `params = torch.tensor([1.0, 0.0], requires_grad=True)`, then compute `loss = loss_fn(model(t_u, *params), t_c)` and call `loss.backward()`. Before that call, `params.grad` is `None`; afterward, it holds the loss derivative for each parameter. For the sample data, it is `tensor([4517.2969, 82.6000])`. section 5.5.1
To use those gradients in a training loop:
- Clear any existing parameter gradients with `params.grad.zero_()` when `params.grad` is not `None`. section 5.5.1
- Compute predictions and loss, then call `loss.backward()` to populate `params.grad`. section 5.5.1
- Update parameters using the learning rate and gradient inside `with torch.no_grad():`, then repeat. section 5.5.1
# In[9]:
def training_loop(n_epochs, learning_rate, params, t_u, t_c):
for epoch in range(1, n_epochs + 1):
if params.grad is not None: #1
params.grad.zero_()
t_p = model(t_u, *params)
loss = loss_fn(t_p, t_c)
loss.backward()
with torch.no_grad(): #2
params -= learning_rate * params.grad
if epoch % 500 == 0:
print('Epoch %d, Loss %f' % (epoch, float(loss)))
return params
The complete calibration loop puts gradient clearing, the forward calculation, `backward()`, and the parameter update together. section 5.5.1





