Plotting results with Matplotlib
Matplotlib is the standard Python library for drawing charts; in ML you use it mostly to see loss curves, data and predictions.
Why it matters
Training prints thousands of numbers, and you can't spot a trend by reading them. Is the loss still falling? Did it jump to a huge value at step 400? Is the model overfitting? A picture answers those questions in a second. Many training bugs are found by looking at a plot, not by reading the code.
The idea
A plot turns numbers into a picture, so you can see patterns and bugs. In ML, the most important plot is the loss curve.
A car dashboard. You don't read engine data line by line. You glance at the gauges.
A dashboard shows only the present. A loss curve shows the whole history of training, and that history is where the clues are.
How it works
- Keep a list during training: losses.append(loss) once per step, or once per epoch.
- plt.plot(losses) draws a line: the position in the list goes on the x-axis and the value on the y-axis.
- Label it: plt.xlabel("step"), plt.ylabel("loss") and a title. An unlabelled plot is easy to misread a week later.
- For training and validation curves, call plt.plot twice with label="train" and label="validation", then plt.legend() to add the key.
- If the loss spans several powers of 10, call plt.yscale("log") so the late progress is visible.
- plt.show() opens the window; plt.savefig("loss.png", dpi=150) saves it for your README or report.
- For several charts side by side, fig, axes = plt.subplots(1, 2) gives you two drawing areas: axes[0].plot(...) and axes[1].plot(...).
Key points
- The loss curve is the main health check for any training run. Plot it every time.
- Healthy: falls fast, then flattens. Spiky or rising: the learning rate is too high, or there is a bug. Flat from the start: nothing is being learned.
- Training loss falling while validation loss rises means overfitting. Keep the model from the lowest validation point.
- Use a log scale for anything that spans powers of 10: losses, learning rates, dataset sizes.
- plt.scatter for data points, plt.hist for how values are spread, plt.imshow for images and matrices.
- Always label the axes, and add a legend whenever there is more than one line.
Common mistakes
- Plotting every step of a noisy loss and seeing only noise. Smooth it with a moving average, or plot once per epoch.
- Comparing two runs drawn with different y-axis ranges. Fix the range with plt.ylim.
- A normal y-axis that hides late improvement. Try a log scale.
- Passing a PyTorch tensor straight to Matplotlib. Convert it first with loss.item() or tensor.detach().cpu().numpy().
Not true: A normal, evenly spaced y-axis is the clearest way to see late improvements in a loss.
In fact: On a normal axis, a loss that falls from 1,000 to 0.01 looks flat at the end. A log scale makes the late progress visible.
Not true: Two runs can be compared fairly even when their plots use different y-axis ranges.
In fact: Different ranges make the same curve look steeper or flatter. Fix the range with plt.ylim, or draw both runs on the same axes.
Part of Unit 0: Your toolkit in the free ML Foundations course.