A visualization tool for neural network optimizers
Project description
Optima Vis
Optima Vis is a Python package for visualizing optimization trajectories of PyTorch models in 2D parameter space.
It provides tools to plot loss surfaces and generate static or animated visualizations of optimizer behavior, either as GIFs or MP4 videos.
Features
- LossContourGenerator – Generates 2D contour plots of the loss surface for models with exactly two trainable parameters.
- OptimizerPlotGenerator – Creates static plots of optimization trajectories on top of the loss contours.
- OptimizerAnimationGenerator – Generates animated visualizations (GIF/MP4) of the optimizer's path through parameter space.
Installation
You can install via PyPI (once published) or directly from source:
# From PyPI
pip install optima-vis
# From source
git clone https://github.com/yourusername/optima-vis.git
cd optima-vis
pip install .
Dependencies:
- Python 3.13+
- PyTorch 2.8+
- Matplotlib 3.10+
Usage
from torch import nn, tensor
from torch import randn_like
from optima_vis import (
OptimizerPlotGenerator,
OptimizerAnimationGenerator,
LossContourGenerator,
VideoProperties,
GifProperties
)
Generate a loss contour
loss_fcn = nn.MSELoss()
X_interval = [[-10, 10], [-10, 10]]
X_1 = X_interval[0][0] + (X_interval[0][1] - X_interval[0][0]) * rand(100, 1)
X_2 = X_interval[1][0] + (X_interval[1][1] - X_interval[1][0]) * rand(100, 1)
X_samples = cat([X_1, X_2], 1)
def y_true_fcn(X: Tensor) -> Tensor:
return (X @ Tensor([-5, 6.5]).unsqueeze(1))
y_true = y_true_fcn(X_samples)
y_measured = y_true + 2.0 * randn_like(y_true) # Measurements with noise
model = Linear(in_features=2, out_features=1, bias=False)
# Create contour generator
contour_gen = LossContourGenerator(loss_fcn, X_samples)
fig, ax = contour_gen(model, y_true)
fig.show()
Plot optimization trajectory
optimizer_plot_gen = OptimizerPlotGenerator(loss_fcn)
fig, ax = optimizer_plot_gen(model, X_samples, y_true, loss_data)
fig.show()
loss_datais a list of tuples: (list[Tensor], Tensor) representing parameters and corresponding loss at each iteration.
Generate animated visualization
animation_gen = OptimizerAnimationGenerator()
# Export as GIF
export_props = GifProperties(name="training_animation", length=5)
animation_gen(model, X_samples, y_true, loss_fcn, loss_data, export_props)
# Export as MP4 video
export_props = VideoProperties(name="training_video", length=5, format="mp4")
animation_gen(model, X_samples, y_true, loss_fcn, loss_data, export_props)
- Displays the optimizer's path on the loss surface.
- Supports both GIF and MP4 export formats.
- Uses matplotlib.animation.FuncAnimation internally.
Notes
- Only supports models with exactly two trainable parameters.
- For animations, the frame rate is automatically calculated based on the total number of iterations and the desired video length.
- Arrows indicate the direction of parameter updates in the trajectory.
- More examples can be found under
testsfolder.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file optima_vis-1.4.2.tar.gz.
File metadata
- Download URL: optima_vis-1.4.2.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ed5d512b41e89a91cd4a5a39816a66e34070d5a95c322ed647b055d8318412b
|
|
| MD5 |
4d4478456fe0d81ee0a27d07238b95f8
|
|
| BLAKE2b-256 |
0b8c42c32b171164cc77fff6a0c6d1f9ae551a651b0645a974f6df39747d5924
|
File details
Details for the file optima_vis-1.4.2-py3-none-any.whl.
File metadata
- Download URL: optima_vis-1.4.2-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
968d47ea3131a7a98c5a620b1b114c08fa8f541d716d6adaaf084667209d663d
|
|
| MD5 |
f3bd473c1ef93dec643c0dac30d27b99
|
|
| BLAKE2b-256 |
cc756f58a4ae2b0173b74cc9f29495552c45e77f85de7711a86182a7b82207f4
|