Newton and Quasi-Newton optimization with PyTorch
pip install pytorch-minimizePyTorch Minimize
For the most up-to-date information on pytorch-minimize, see the docs site: pytorch-minimize.readthedocs.io
Pytorch-minimize represents a collection of utilities for minimizing multivariate functions in PyTorch.
It is inspired heavily by SciPy's optimize module and MATLAB's Optimization Toolbox.
Unlike SciPy and MATLAB, which use numerical approximations of function derivatives, pytorch-minimize uses real first- and second-order derivatives, computed seamlessly behind the scenes with autograd.
Both CPU and CUDA are supported.
Author: Reuben Feinman
At a glance:
import torch
from torchmin import minimize
def rosen(x):
return torch.sum(100*(x[..., 1:] - x[..., :-1]**2)**2
+ (1 - x[..., :-1])**2)
# initial point
x0 = torch.tensor([1., 8.])
# Select from the following methods:
# ['bfgs', 'l-bfgs', 'cg', 'newton-cg', 'newton-exact',
# 'trust-ncg', 'trust-krylov', 'trust-exact', 'dogleg']
# BFGS
result = minimize(rosen, x0, method='bfgs')
# Newton Conjugate Gradient
result = minimize(rosen, x0, method='newton-cg')
# Newton Exact
result = minimize(rosen, x0, method='newton-exact')
Solvers: BFGS, L-BFGS, Conjugate Gradient (CG), Newton Conjugate Gradient (NCG), Newton Exact, Dogleg, Trust-Region Exact, Trust-Region NCG, Trust-Region GLTR (Krylov)
Examples: See the Rosenbrock minimization notebook for a demonstration of function minimization with a handful of different algorithms.
Install with pip:
pip install pytorch-minimize
Install from source (bleeding edge):
pip install git+https://github.com/rfeinman/pytorch-minimize.git
Motivation
Although PyTorch offers many routines for stochastic optimization, utilities for deterministic optimization are scarce; only L-BFGS is included in the optim package, and it's modified for mini-batch training.
MATLAB and SciPy are industry standards for deterministic optimization. These libraries have a comprehensive set of routines; however, automatic differentiation is not supported.* Therefore, the user must provide explicit 1st- and 2nd-order gradients (if they are known) or use finite-difference approximations.