Visualizing Gradients with PyTorch
Updated 29 April 2026
Key takeaways
- We built a gradient visualization tool to make the mathematics behind machine learning optimization easier to picture.
- The plot pairs a surface showing function values with arrows in the input plane that point toward the steepest ascent.
- The same mental picture extends to higher dimensions and provides a foundation for understanding gradient descent.
Gradients appear throughout calculus and machine learning. To understand them better myself, I wanted to build a visualization tool that helps me develop the correct mental picture of what the gradient of a function is. I came across GistNoesis/VisualizeGradient, so I went on from there to write my own iteration. This mental model generalizes to higher dimensions and is the foundation for understanding optimization algorithms like gradient descent.
The colored surface shows function values. Black arrows show gradient vectors in the input plane (x-y space), pointing toward the direction of steepest ascent.
The full project is on my GitHub. I’m also looking forward to doing something similar on the Central Limit Theorem as well as doing a short tutorial on plotting options volatility surfaces with python, a project I have been waiting to finish for some time now.