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
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 Distributions
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 modern_fm-0.2.1.tar.gz.
File metadata
- Download URL: modern_fm-0.2.1.tar.gz
- Upload date:
- Size: 38.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3da8278bd363e7f58183ecf058ff18c077a8e89c0459fa1eb5864d035116b424
|
|
| MD5 |
4aa49304e2e67f4f8d34fee7c4327214
|
|
| BLAKE2b-256 |
88394485c3cad5a6ab6e66146046f4c290760b60d1c0bba6a3fe70ddd648da33
|
Provenance
The following attestation bundles were made for modern_fm-0.2.1.tar.gz:
Publisher:
release.yml on Matapanino/modern_fm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
modern_fm-0.2.1.tar.gz -
Subject digest:
3da8278bd363e7f58183ecf058ff18c077a8e89c0459fa1eb5864d035116b424 - Sigstore transparency entry: 1961110903
- Sigstore integration time:
-
Permalink:
Matapanino/modern_fm@de787f079849be4b98ff50fd903cd05e7ebd7b68 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/Matapanino
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@de787f079849be4b98ff50fd903cd05e7ebd7b68 -
Trigger Event:
push
-
Statement type:
File details
Details for the file modern_fm-0.2.1-cp310-abi3-win_amd64.whl.
File metadata
- Download URL: modern_fm-0.2.1-cp310-abi3-win_amd64.whl
- Upload date:
- Size: 277.6 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a4e7341906082032c11e28d453cc558ab283b794875bd0b0e5928271f7a3d564
|
|
| MD5 |
80d8018c651970086476ef7afe5f9447
|
|
| BLAKE2b-256 |
38e88e5f502bed318e65457cf98669b3869b02b83277358f6d8030f389942556
|
Provenance
The following attestation bundles were made for modern_fm-0.2.1-cp310-abi3-win_amd64.whl:
Publisher:
release.yml on Matapanino/modern_fm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
modern_fm-0.2.1-cp310-abi3-win_amd64.whl -
Subject digest:
a4e7341906082032c11e28d453cc558ab283b794875bd0b0e5928271f7a3d564 - Sigstore transparency entry: 1961111397
- Sigstore integration time:
-
Permalink:
Matapanino/modern_fm@de787f079849be4b98ff50fd903cd05e7ebd7b68 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/Matapanino
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@de787f079849be4b98ff50fd903cd05e7ebd7b68 -
Trigger Event:
push
-
Statement type:
File details
Details for the file modern_fm-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: modern_fm-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 390.5 kB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7059e389565a830702e53967d9d543048ec4288e48d76bbc1305e95b5bd2a1dc
|
|
| MD5 |
80368ad792f643f86aa8b9bfabc3464d
|
|
| BLAKE2b-256 |
5c3ad4175d6bbc0f5b28b020f117c080ca1fb36ae7ba1c5ac82de6b9d180d760
|
Provenance
The following attestation bundles were made for modern_fm-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
release.yml on Matapanino/modern_fm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
modern_fm-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
7059e389565a830702e53967d9d543048ec4288e48d76bbc1305e95b5bd2a1dc - Sigstore transparency entry: 1961111123
- Sigstore integration time:
-
Permalink:
Matapanino/modern_fm@de787f079849be4b98ff50fd903cd05e7ebd7b68 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/Matapanino
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@de787f079849be4b98ff50fd903cd05e7ebd7b68 -
Trigger Event:
push
-
Statement type:
File details
Details for the file modern_fm-0.2.1-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.
File metadata
- Download URL: modern_fm-0.2.1-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
- Upload date:
- Size: 672.6 kB
- Tags: CPython 3.10+, macOS 10.12+ universal2 (ARM64, x86-64), macOS 10.12+ x86-64, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
854774111f5cf3d30b7abd9082222aa82b1dae09f956abc49dd8bfc37fc54791
|
|
| MD5 |
41343897f119dcff21eeb7e5118b2212
|
|
| BLAKE2b-256 |
8c903738dc05f379f6e6b0b8eab83b759e7c65beb51a6b585461f43560cd26e4
|
Provenance
The following attestation bundles were made for modern_fm-0.2.1-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:
Publisher:
release.yml on Matapanino/modern_fm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
modern_fm-0.2.1-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl -
Subject digest:
854774111f5cf3d30b7abd9082222aa82b1dae09f956abc49dd8bfc37fc54791 - Sigstore transparency entry: 1961111269
- Sigstore integration time:
-
Permalink:
Matapanino/modern_fm@de787f079849be4b98ff50fd903cd05e7ebd7b68 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/Matapanino
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@de787f079849be4b98ff50fd903cd05e7ebd7b68 -
Trigger Event:
push
-
Statement type: