Stochastic gradient descent (SGD) updates model parameters using a gradient estimated from a small random batch rather than the entire dataset. This makes each step computationally practical for millions or billions of examples, while introducing noise that can help explore the loss landscape and escape shallow local minima. section 3.1.2
Use SGD as part of an iterative training loop:
- Process a small batch, commonly 8, 16, or 32 examples, and estimate its gradient. section 3.1.2
- Choose the learning rate carefully: large values can overshoot and destabilize training, while very small values make progress painfully slow. section 3.1.2
- Compute outputs and loss, clear previous gradients, backpropagate, and update parameters with the optimizer. section 3.1.2
LLM fine-tuning batch
A batch can contain pairs such as Input: “Summarize this article:” and Output: the expected response. SGD uses the resulting loss signal to adjust the model incrementally. section 3.1.2
Listing 3.1 Basic steps of the training process
for inputs, targets in dataloader: #A
outputs = model(inputs) #B
loss = loss_fn(outputs, targets) #C
optimizer.zero_grad() #D
loss.backward() #E
optimizer.step() #F
if step % 10 == 0: #G
print(f"Step {step}, Loss: {loss.item():.4f}")Shows the practical loop from inputs and targets through loss calculation, backpropagation, and the parameter update. section 3.1.2
















