Decomposition & Manifold
NMF
Non-Negative Matrix Factorization via Multiplicative Updates with optional L1 and L2 regularization.
NMF (Non-Negative Matrix Factorization) finds two non-negative matrices W (sample loadings) and H (component dictionary) whose product W @ H approximates a non-negative input matrix X. OchreML implements Multiplicative Updates with optional elastic-net style L1/L2 penalties (l1_ratio, alpha_W, alpha_H) and NNDSVD or random initialization. (^▽^)
Backend Support
- Backend primitives:
E1–E2(dense matrix multiplicationA @ BandA^T @ B),N2(elementwise multiplicative updates and clamping). - Supported devices:
"auto","cpu","cuda:0","cuda:all","wgpu","tpu:0". Falls back cleanly to multi-threaded Rayon CPU (f64) when an accelerator is unavailable.
Import & Signature
from ochreml.cross_decomposition import NMF
# Also available from ochreml.decomposition or top-level ochreml:
from ochreml.decomposition import NMF
from ochreml import NMFNMF(
n_components: Optional[int] = None,
init: Optional[str] = None,
max_iter: int = 200,
tol: float = 1e-4,
l1_ratio: float = 0.0,
alpha_W: float = 0.0,
alpha_H: Optional[float] = None,
random_state: Optional[int] = None,
device: Optional[str] = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
n_components | int or None | None | Number of components. If None, all input features are kept. |
init | {"nndsvd", "nndsvda", "random"} or None | None | Initialization method for W and H. |
max_iter | int | 200 | Maximum number of multiplicative update iterations before stopping. |
tol | float | 1e-4 | Stopping tolerance on relative reconstruction error change. |
l1_ratio | float | 0.0 | Regularization mixing parameter in [0.0, 1.0] (0.0 for L2, 1.0 for L1). |
alpha_W | float | 0.0 | Constant multiplying the regularization terms applied to W. |
alpha_H | float or None | None | Constant multiplying the regularization terms applied to H. Defaults to alpha_W when None. |
random_state | int or None | None | Random seed used when init="random". |
device | str or None | None | Target compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type / Shape | Description |
|---|---|---|
components_ | ndarray of shape (n_components_, n_features_in_) | Factorization dictionary matrix H. |
n_components_ | int | Actual number of components learned. |
reconstruction_err_ | float | Frobenius norm of X - W @ H on the training data. |
n_iter_ | int | Number of iterations executed during fit. |
n_features_in_ | int | Number of features seen during fit. |
device_ | str | Active resolved compute device string. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y=None) -> NMF | Learn an NMF factorization for non-negative data X. |
transform | transform(X) -> ndarray | Compute the non-negative representation W for X given fixed components_. |
fit_transform | fit_transform(X, y=None) -> ndarray | Fit the model to X and return the transformed matrix W. |
inverse_transform | inverse_transform(W) -> ndarray | Reconstruct data back to feature space via W @ components_. |
to | to(device) -> NMF | Move execution target to a new hardware device. |
save | save(path) -> None | Save the fitted model to a binary .dit file. |
load | NMF.load(path, device=None) -> NMF | Load a saved model from a .dit file (also supported via ochreml.load). |
Example
import numpy as np
import ochreml
from ochreml.cross_decomposition import NMF
rng = np.random.default_rng(42)
W_true = rng.uniform(0.1, 2.0, size=(100, 3))
H_true = rng.uniform(0.1, 2.0, size=(3, 8))
X = W_true @ H_true + rng.uniform(0.0, 0.05, size=(100, 8))
nmf = NMF(n_components=3, init="nndsvda", max_iter=300, random_state=42)
W = nmf.fit_transform(X)
X_recon = nmf.inverse_transform(W)
print("W shape:", W.shape, "H shape:", nmf.components_.shape)
print("Reconstruction error:", round(nmf.reconstruction_err_, 4))
print("Iterations:", nmf.n_iter_)
nmf.save("nmf_model.dit")
loaded = ochreml.load("nmf_model.dit", device="cpu")
assert np.allclose(W, loaded.transform(X), atol=1e-4)