Skip to main content

Lightweight gradient descent optimizer implemented in Rust

Project description

liteopt

A lightweight optimization library written in Rust with Python bindings.

Installation

Install from PyPI:

uv venv
source .venv/bin/activate
uv pip install liteopt

Install from source (development):

Requirements:

  • Rust toolchain (cargo)
  • Python 3.8+
  • uv
cd liteopt-py
uv sync --extra dev
uv run --extra dev maturin develop --manifest-path Cargo.toml
uv run python -c "import liteopt; print(liteopt.__file__)"

Quick Start

Gradient Descent:

import liteopt

f = lambda x: (x[0] - 3.0) ** 2
grad = lambda x: [2.0 * (x[0] - 3.0)]

x_star, f_star, ok = liteopt.gd(f, grad, x0=[0.0], step_size=0.1)
print(ok, x_star, f_star)

Custom line search callback (GD):

def half_step(ctx):
    return {"accepted": True, "alpha": 0.5 * ctx["alpha0"]}

x_star, f_star, ok = liteopt.gd(f, grad, x0=[0.0], line_search=half_step)

Gauss-Newton (least squares):

import liteopt

target = [1.0, -2.0]

def residual(x):
    return [x[0] - target[0], x[1] - target[1]]

def jacobian(_x):
    return [[1.0, 0.0], [0.0, 1.0]]

x_star, cost, iters, r_norm, dx_norm, ok = liteopt.gn(residual, jacobian, x0=[0.0, 0.0])
print(ok, x_star, cost)

Gauss-Newton simple loop (fixed damping + strict-decrease line search):

x_star, cost, iters, r_norm, dx_norm, ok = liteopt.gn(
    residual,
    jacobian,
    x0=[0.0, 0.0],
    lambda_=1e-8,
    damping_update="fixed",
    linear_system="normal_jtj",
    line_search_method="strict_decrease",
    line_search=True,
    ls_beta=0.5,
    ls_min_step=1e-8,
    ls_max_steps=12,
)
print(ok, x_star, cost)

Custom line search callback (GN):

def half_step(ctx):
    return {"accepted": True, "alpha": 0.5 * ctx["alpha0"]}

x_star, cost, *_ = liteopt.gn(residual, jacobian, x0=[0.0, 0.0], line_search=half_step)

Levenberg-Marquardt (least squares):

x_star, cost, iters, r_norm, dx_norm, ok = liteopt.lm(residual, jacobian, x0=[0.0, 0.0])
print(ok, x_star, cost)

Optional manifold callbacks:

gd(...), gn(...), and lm(...) accept manifold=... with these methods:

  • retract(x, direction, alpha) -> list[float]
  • tangent_norm(v) -> float
  • scale(v, alpha) -> list[float]
  • add(x, v) -> list[float]
  • difference(x, y) -> list[float]

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

liteopt-0.1.7.tar.gz (26.6 kB view details)

Uploaded Source

Built Distribution

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

liteopt-0.1.7-cp313-abi3-macosx_11_0_arm64.whl (266.0 kB view details)

Uploaded CPython 3.13+macOS 11.0+ ARM64

File details

Details for the file liteopt-0.1.7.tar.gz.

File metadata

  • Download URL: liteopt-0.1.7.tar.gz
  • Upload date:
  • Size: 26.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for liteopt-0.1.7.tar.gz
Algorithm Hash digest
SHA256 43a8275a66f8a16088b1042da131333a08f76258ed6b4ae233af974a0a4d734c
MD5 cf757073a556db52a3a87b418fe6fb40
BLAKE2b-256 110d96c52d7a19ccca03f4a53dbb355d9f36f3836eec428f8e8a4e876c8921c4

See more details on using hashes here.

File details

Details for the file liteopt-0.1.7-cp313-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for liteopt-0.1.7-cp313-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8e18c12c1a45ef9ecdfcc9e2a0b362a172cd96c5a15a427d0149fea16527bdec
MD5 22b951772e679d2aeb468782b6c8e1fe
BLAKE2b-256 390e412e1ef640656e42c4738039bfd811dc2f4646b5959a0843e62a15625eaf

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