Auto-Conditioned Fast Gradient Method (AC-FGM) optimizer for PyTorch.
Project description
acfgm-pytorch
acfgm-pytorch is a PyTorch optimizer implementation of the Auto-Conditioned Fast Gradient Method (AC-FGM) from A simple uniformly optimal method without line search for convex optimization (Version v2).
AC-FGM is an accelerated first-order method designed for convex optimization without the need for estimating the global Lipschitz constant. Instead of asking the user to tune a fixed learning rate, the method estimates local curvature from previous gradients and updates its step size automatically. This package wraps that implementation in torch.optim.Optimizer interface so it can be used with tensors, custom objectives, and standard torch.nn.Module parameters.
Features
- Implements the Corollary 2 setup for AC-FGM update described in the paper, with an optional simple line search step through
linesearch=Truefor better complexity bound. - Uses a standard PyTorch closure like
torch.optim.LBFGS: each optimizer step reevaluates the objective, computes gradients, and returns the loss. - Includes projection bounds through
lims, which keeps iterates inside a box constraint such as[-10, 10]. - Works on any device supported by the optimized tensors, including CPU, CUDA and MPS tensors.
Installation
pip install acfgm-pytorch
Optimizer Parameters
ACFGM(params, beta=0.1, eps=1e-8, lims=None, linesearch=False)
params: iterable of tensors or parameter groups to optimize.beta: AC-FGM averaging parameter. The implementation accepts values in(0, 1); the paper discusses more specific theoretical ranges for particular guarantees.eps: small positive value used to avoid division by zero in curvature and norm calculations.lims: two-element projection interval. If omitted, parameters are projected to[-1, 1]; pass a wider interval when your problem requires it.linesearch: choose whether linesearch is used (only in the first iteration) to ensure $\eta_1 \in [\frac{\beta}{4 (1-\beta) L_1}, \frac{1}{3L_1} ]$. The default isFalse.
Quickstart
Optimizing Different Parameter Shapes
ACFGM can optimize scalar, vector, and higher-dimensional tensor parameters in
the same optimizer instance. This is useful for experiments where the decision
variables are tensors rather than a neural network.
This example places three independent quadratic objectives into one loss:
a scalar target near 2.0, a vector target near 3.0, and a rank-3 tensor
target near 4.0.
import torch
from acfgm import ACFGM
device = "cpu" # or "cuda" "mps"
scalar_param = torch.tensor(0.5, device=device, requires_grad=True)
vector_param = torch.rand(3, device=device, requires_grad=True)
tensor_param = torch.rand(2, 3, 4, device=device, requires_grad=True)
optimizer = ACFGM(
[scalar_param, vector_param, tensor_param],
beta=0.26,
lims=[-10, 10],
)
for i in range(25):
def closure():
optimizer.zero_grad()
loss = (
(scalar_param - 2.0).pow(2)
+ (vector_param - 3.0).pow(2).sum()
+ (tensor_param - 4.0).pow(2).sum()
)
loss.backward(retain_graph=True)
return loss
loss = optimizer.step(closure)
if i % 5 == 0:
print(f"itr {i}: loss {round(loss.detach().cpu().item(), 5)}")
print(
scalar_param.detach(),
vector_param.detach(),
tensor_param.detach().mean(),
)
Training a Neural Network
Although AC-FGM is designed for smooth convex optimization, the optimizer can also be applied to standard PyTorch modules. The example below fits a simple model and checks that the loss decreases.
import torch
from torch import nn
from acfgm import ACFGM
device = "cpu" # or "cuda" "mps"
torch.manual_seed(0)
train_x = torch.linspace(-1.0, 1.0, 16, device=device).unsqueeze(1)
train_y = 2.0 * train_x - 1.0
model = nn.Sequential(nn.Linear(1, 16), nn.ReLU(), nn.Linear(16, 1)).to(device)
optimizer = ACFGM(model.parameters(), beta=0.26, lims=[-10, 10])
loss_fn = nn.MSELoss()
for i in range(25):
def closure():
optimizer.zero_grad()
loss = loss_fn(model(train_x), train_y)
loss.backward()
return loss
loss = optimizer.step(closure)
if i % 5 == 0:
print(f"itr {i}: loss {round(loss.detach().cpu().item(), 5)}")
Reference
@misc{li2024simpleuniformlyoptimalmethod,
title = {A simple uniformly optimal method without line search for convex optimization},
author = {Tianjiao Li and Guanghui Lan},
year = {2024},
eprint = {2310.10082},
archivePrefix = {arXiv},
primaryClass = {math.OC},
url = {https://arxiv.org/abs/2310.10082v2}
}
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 acfgm_pytorch-0.1.0.tar.gz.
File metadata
- Download URL: acfgm_pytorch-0.1.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17b4cb795711279d39895864b52908d2bf132ce6ae0f431a43e1b73b451b5f62
|
|
| MD5 |
c0553d03aa5f1bb049230a1fa373302f
|
|
| BLAKE2b-256 |
3b7f9c6a00b07bac99df611696a462c67d7ed285b500b117b3583af587bd89c0
|
Provenance
The following attestation bundles were made for acfgm_pytorch-0.1.0.tar.gz:
Publisher:
workflow.yml on JGIoA/ACFGM-pytorch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
acfgm_pytorch-0.1.0.tar.gz -
Subject digest:
17b4cb795711279d39895864b52908d2bf132ce6ae0f431a43e1b73b451b5f62 - Sigstore transparency entry: 1463045955
- Sigstore integration time:
-
Permalink:
JGIoA/ACFGM-pytorch@38ac2081a06625ed376d81985360e872643909d7 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/JGIoA
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@38ac2081a06625ed376d81985360e872643909d7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file acfgm_pytorch-0.1.0-py3-none-any.whl.
File metadata
- Download URL: acfgm_pytorch-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
53a4abbf92f72298e637cf9ddb4f614ebb2c1bb6b2b5a8ba5de3741c2672bacb
|
|
| MD5 |
735f2c038baa595a35ca18d018e91a10
|
|
| BLAKE2b-256 |
b61913bc9292ee647ba94d06b435b35c7fe6878f582c7b86ae85fa8e1f17303b
|
Provenance
The following attestation bundles were made for acfgm_pytorch-0.1.0-py3-none-any.whl:
Publisher:
workflow.yml on JGIoA/ACFGM-pytorch
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
acfgm_pytorch-0.1.0-py3-none-any.whl -
Subject digest:
53a4abbf92f72298e637cf9ddb4f614ebb2c1bb6b2b5a8ba5de3741c2672bacb - Sigstore transparency entry: 1463045970
- Sigstore integration time:
-
Permalink:
JGIoA/ACFGM-pytorch@38ac2081a06625ed376d81985360e872643909d7 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/JGIoA
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@38ac2081a06625ed376d81985360e872643909d7 -
Trigger Event:
push
-
Statement type: