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__)"

Examples

Run bundled examples from liteopt-py/example/:

cd liteopt-py
uv run python example/run.py all

Or from repository root:

uv run --project liteopt-py python liteopt-py/example/run.py all

Run a single example:

uv run python example/run.py gd
uv run python example/run.py gn
uv run python example/run.py lm

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)

# Collect per-iteration history (list[dict]) with an option:
x_star, f_star, ok, history = liteopt.gd(
    f, grad, x0=[0.0], step_size=0.1, history=True
)
print("history rows:", len(history))

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):
    # If you return a Python list, it must be row-major 1D (m*n elements).
    # `[[1.0, 0.0], [0.0, 1.0]]` raises TypeError.
    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)

# Optional trace history:
x_star, cost, iters, r_norm, dx_norm, ok, history = liteopt.gn(
    residual, jacobian, x0=[0.0, 0.0], history=True
)

jacobian must be either:

  • row-major 1D list (list[float], length = m * n)
  • 2D numpy.ndarray (shape = (m, n))

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 trace history:
x_star, cost, iters, r_norm, dx_norm, ok, history = liteopt.lm(
    residual, jacobian, x0=[0.0, 0.0], history=True
)

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.8.tar.gz (30.5 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.8-cp313-abi3-macosx_11_0_arm64.whl (274.6 kB view details)

Uploaded CPython 3.13+macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for liteopt-0.1.8.tar.gz
Algorithm Hash digest
SHA256 7a0ac80506b14f8b24650622cfc8fa6bdab251473cd35aa7b4a046fecd0b7c10
MD5 de62356830073b4612bc3c0fb372ad0b
BLAKE2b-256 ca285cff275eec1a99cf5d794fd2793ef61724fad48e43973ddb32b4a0074bf1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for liteopt-0.1.8-cp313-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 296026da1a9e12c75cd343b167c4928d6ac6dfeb7ddadd71819a282dfcd5cd80
MD5 0ef0e838f93ac300c5fde51acabf9dd2
BLAKE2b-256 7671155a6757f328b9aadcd911b89042d4f7bd9c5ea873eba5a044bf2696c9f1

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