Skip to main content

Fast, sklearn-compatible Factorization Machines and Field-aware Factorization Machines

Project description

modern_fm

Fast, sklearn-compatible Factorization Machines (FM) and Field-aware Factorization Machines (FFM) for Python.

Status: v0.2 (Beta). A Rust CPU backend (parity-tested against pure-NumPy reference implementations) drives sklearn-style estimators — FMClassifier (binary + multiclass softmax), FMRegressor, and FFMClassifier (binary + multiclass softmax) — with the SGD / AdaGrad / Adam / FTRL-Proximal optimizers, mini-batch gradient averaging (batch_size), multi-core training via rayon (n_jobs), plus sample_weight/class_weight, label_smoothing, early stopping, a CategoricalEncoder, and save_model/load_model. FTRL's L1 (l1_linear/l1_factors) yields exact-zero weights. See docs/roadmap.md for remaining niche gaps (FTRL + early stopping, multiclass + early-stopping for FFM).

Installation

pip install modern-fm        # once published; prebuilt wheels, no Rust needed

Until the first PyPI release, install from source (requires a Rust toolchain; see Development below).

Usage

from modern_fm import FMClassifier, FFMClassifier

model = FMClassifier(
    n_factors=16,
    optimizer="adagrad",
    learning_rate=0.05,
    max_iter=100,
    batch_size=256,        # mini-batch gradient averaging (1 = per-row SGD)
    n_jobs=-1,             # train batches across all CPU cores
    l2_linear=1e-5,
    l2_factors=1e-5,
    random_state=42,
)
model.fit(X_train, y_train)
proba = model.predict_proba(X_test)

# FTRL-Proximal with L1 for sparse linear weights (classic CTR setup)
sparse = FMClassifier(optimizer="ftrl", l1_linear=1.0, batch_size=256, random_state=42)
sparse.fit(X_train, y_train)

ffm = FFMClassifier(n_factors=8, n_jobs=-1, random_state=42)
ffm.fit(X_train, y_train, field_ids=field_ids)

FMRegressor, multiclass FMClassifier (just pass a target with >2 classes), early stopping (early_stopping=True or eval_set=(X_val, y_val)), and the CategoricalEncoder are demonstrated in examples/basic_usage.py. benchmarks/bench_synthetic.py reports fit time and predict throughput against the NumPy reference floor.

Development

Requires Python >= 3.10 and a recent Rust toolchain (1.74+; rustup update).

python3 -m venv .venv
.venv/bin/pip install -e ".[dev]"   # builds the Rust extension via maturin
.venv/bin/pytest -q
.venv/bin/ruff check .

pip install -e . compiles rust/ and installs the extension as modern_fm._rust (maturin mixed layout, config in pyproject.toml). After editing Rust code, re-run pip install -e . to rebuild. Rust-only checks:

cd rust
PYO3_PYTHON=$PWD/../.venv/bin/python3 cargo test
PYO3_PYTHON=$PWD/../.venv/bin/python3 cargo clippy

Without the extension built, the package still works: modern_fm._backend falls back to the pure-NumPy reference implementations, and the parity tests in tests/test_rust_parity.py are skipped.

Design documents live in docs/ — start with docs/requirements.md and docs/math_spec.md. The roadmap is in docs/roadmap.md.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

modern_fm-0.2.0-cp310-abi3-win_amd64.whl (277.7 kB view details)

Uploaded CPython 3.10+Windows x86-64

modern_fm-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (390.5 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

modern_fm-0.2.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (673.0 kB view details)

Uploaded CPython 3.10+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file modern_fm-0.2.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: modern_fm-0.2.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 277.7 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for modern_fm-0.2.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f6e4f9af53af5574c92e620be66c898507b566657a9055ceddaceb1277934c14
MD5 c9c4c19e8a6f68a3fd9838ec03ca47ce
BLAKE2b-256 54488250277860bc84d7a8bc267bdce33f8612e3b3c93400167d02daaeeac1a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for modern_fm-0.2.0-cp310-abi3-win_amd64.whl:

Publisher: release.yml on Matapanino/modern_fm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file modern_fm-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for modern_fm-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7634205841bb0c93db16104c054391e019ea8c66a6a35ea081fc4648dba5476a
MD5 036a263dbe0835dc1eec15949cead213
BLAKE2b-256 892364c990c87a81002a70d28b1c1ee2ce0c650ecc2d36348445ac928b33887a

See more details on using hashes here.

Provenance

The following attestation bundles were made for modern_fm-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on Matapanino/modern_fm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file modern_fm-0.2.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for modern_fm-0.2.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 fee4082db6e2b580100c034d481dd8ae81047d99e6bae9c69ead031e04b3f0e1
MD5 d41772f8427e2fa8ea1ba72a1a71461c
BLAKE2b-256 14a2d775f7312a30e9d92bb669c67164a4f1a82281b7a9f21920d368ea39a215

See more details on using hashes here.

Provenance

The following attestation bundles were made for modern_fm-0.2.0-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: release.yml on Matapanino/modern_fm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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