Skip to Content
OchreML
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 multiplication A @ B and A^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 NMF
NMF(
    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

ParameterTypeDefaultDescription
n_componentsint or NoneNoneNumber of components. If None, all input features are kept.
init{"nndsvd", "nndsvda", "random"} or NoneNoneInitialization method for W and H.
max_iterint200Maximum number of multiplicative update iterations before stopping.
tolfloat1e-4Stopping tolerance on relative reconstruction error change.
l1_ratiofloat0.0Regularization mixing parameter in [0.0, 1.0] (0.0 for L2, 1.0 for L1).
alpha_Wfloat0.0Constant multiplying the regularization terms applied to W.
alpha_Hfloat or NoneNoneConstant multiplying the regularization terms applied to H. Defaults to alpha_W when None.
random_stateint or NoneNoneRandom seed used when init="random".
devicestr or NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeType / ShapeDescription
components_ndarray of shape (n_components_, n_features_in_)Factorization dictionary matrix H.
n_components_intActual number of components learned.
reconstruction_err_floatFrobenius norm of X - W @ H on the training data.
n_iter_intNumber of iterations executed during fit.
n_features_in_intNumber of features seen during fit.
device_strActive resolved compute device string.

Methods

MethodSignatureDescription
fitfit(X, y=None) -> NMFLearn an NMF factorization for non-negative data X.
transformtransform(X) -> ndarrayCompute the non-negative representation W for X given fixed components_.
fit_transformfit_transform(X, y=None) -> ndarrayFit the model to X and return the transformed matrix W.
inverse_transforminverse_transform(W) -> ndarrayReconstruct data back to feature space via W @ components_.
toto(device) -> NMFMove execution target to a new hardware device.
savesave(path) -> NoneSave the fitted model to a binary .dit file.
loadNMF.load(path, device=None) -> NMFLoad 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)

On this page