Skip to main content

Educational tool for visualising PyTorch-compatible optimisers on any differentiable 1D or 2D function.

Project description

OptiViz

Walk along loss landscapes and ride along as your favourite optimisers battle it out for the global minimum with OptiViz 1.0, the latest update to OptiViz. Gradient descent with momentum minimising an egg-carton raised by a quadratic.

Optiviz is a python package that enables effortless visualisation of any PyTorch optimiser on any differentiable function in one or two variables. OptiViz might find educational use in an introductory nonlinear optimisation or deep learning class. Vanilla gradient descent minimising a convex quadratic form.

Installation

To install OptiViz, please use:

pip install optiviz

Usage

All functionality of OptiViz is exposed through the optiviz.optimise and optiviz.optimise_interactive functions.

import torch
from optiviz import optimise, optimise_interactive

Any optimisation problem has an objective function. OptiViz works with differentiable, real-valued objective functions in one or two variables.

f : \mathbb{R} \rightarrow \mathbb{R}
g : \mathbb{R}^2 \rightarrow \mathbb{R}

In code, every input and output to the objective function must be a torch.Tensor of shape (1,)

def f(x: torch.Tensor) -> torch.Tensor:
    """
    Example of an objective function in one variable.
    """
    return x ** 2
def g(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
    """
    Example of an objective function in two variables.
    """
    return x ** 2 + y ** 2 + x.sin() * y.sin()

The optiviz.optimise function (please see docstring for complete usage) is used to visualise the optimisation sequence of a 1D or 2D objective function using a PyTorch optimiser.

arg_g_min = optimise(
        g, # objective function
        (12.5, 12.5), # initial values of the parameters being adjusted
        plot_centre=(0, 0), # plot options
        plot_boundary=25,
        iters=100,
        optimiser=torch.optim.Adam, # PyTorch-compatible optimiser
        lr=5e-1 # any keyword arguments for the optimiser
    )

[NEW] The optiviz.optimise_interactive function (please see docstring for complete usage) provides interactive visualisation of 2D objective functions with multiple optimisers, navigation along the landscape, optimiser tracking, music and more.

optimise_interactive(
        g, # objective function
        (12.5, 12.5), # initial values of the parameters being adjusted
        plot_centre=(0, 0), # plot options
        plot_boundary=25,
        iters=100,
        optimisers = [("Vanilla GD", lambda params: torch.optim.SGD(params, lr=0.1))], # PyTorch-compatible optimisers
        iter_delay: int = 100,
        gimbal_radius: float = 4.0, # advanced visualisation options
        gimbal_hover: float = 8.0,
    )

Example programs

OptiViz 0.x/1.x:

import torch
from optiviz import optimise

f = lambda x,y: ×**2+y**2

optimise(f, init_vector=(12.5, 12.5), optimiser=torch.optim.SGD, lr=le-2)

Optiviz 1.x:

import torch
from optiviz import optimise_interactive

def egg_carton(x, y):
    return 0.05*(x**2+y**2)+2.5*(torch.sin(0.5*x)**2+torch.sin(0.5*y)**2)

optimise_interactive(fn=egg_carton, optimisers=[("SGD with momentum", lambda params: torch.optim.SGD(params, lr=0.1, momentum=0.99)), ("Adam", lambda params: torch.optim.Adam(params, lr=0.5))])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

optiviz-1.0.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

optiviz-1.0.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file optiviz-1.0.0.tar.gz.

File metadata

  • Download URL: optiviz-1.0.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for optiviz-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a822005e11cd275fe7d76491fe4834907796d71b8b1372fc73b243711fb976ff
MD5 3b339239595e16bad9f8f3d40ea54a1d
BLAKE2b-256 e96f0a367d3dd17d940f59f6163f0c168f0e67854993c3d76597596968cf2be1

See more details on using hashes here.

File details

Details for the file optiviz-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: optiviz-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for optiviz-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2afc6e77359904d7b292d4fdaeb345f7740790f6c824d0cb1bf8a7ea87df216
MD5 3adfcc2f376171c3e0958b2437ad92ec
BLAKE2b-256 708dd9bc2a33a25ff06ddf8464c6b55b40f4cbd88ea180133338096877f1ac95

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page