Algorithms

Each factorization algorithm is represented by a concrete subtype of AbstractNMFAlgorithm. You construct an instance to hold the algorithm's options and then run it with solve!, which updates W and H in place and returns a Result. nnmf does this for you when you pass the matching alg symbol.

Every constructor is keyword-only, and all except SPA are parameterized by the working element type T (for example MultUpdate{Float64}). Several defaults depend on T, so the type parameter also selects the tolerances.

alg symbolTypeW/H initialized
:multmse, :multdivMultUpdateboth
:projalsProjectedALSW only
:alspgradALSPGradboth
:cdCoordinateDescentboth
:greedycdGreedyCDboth
:spaSPAvia init=:spa

Running an algorithm

NMF.AbstractNMFAlgorithm — Type
AbstractNMFAlgorithm

Supertype for NMF algorithm specifications. Each concrete subtype bundles the options for one factorization algorithm; construct an instance of a concrete subtype and run it with solve!.

Concrete subtypes: MultUpdate, ProjectedALS, ALSPGrad, CoordinateDescent, GreedyCD, and SPA.

Examples

julia> X = rand(8, 6);

julia> W, H = NMF.randinit(X, 3);

julia> r = NMF.solve!(NMF.GreedyCD{Float64}(maxiter=100), X, W, H);

julia> size(r.W), size(r.H)
((8, 3), (3, 6))
source
NMF.solve! — Function
solve!(alg::AbstractNMFAlgorithm, X, W, H; io=stdout, rng=default_rng()) -> Result

Factorize X ≈ W*H using algorithm alg, updating W and H in place and returning a Result.

W and H must be preallocated to sizes (p, k) and (k, n), where X is p×n and k is the rank, and must already be initialized (see randinit, nndsvd, and spa). Some algorithms require both W and H to be initialized (e.g. MultUpdate); others need only W (e.g. ProjectedALS). Progress is written to io when alg is constructed with verbose=true, and rng supplies any randomness the algorithm uses.

Examples

julia> X = rand(8, 6);

julia> W, H = NMF.randinit(X, 3);

julia> r = NMF.solve!(NMF.MultUpdate{Float64}(maxiter=50), X, W, H);

julia> size(r.W), size(r.H)
((8, 3), (3, 6))
source

Multiplicative update

NMF.MultUpdate — Type
MultUpdate{T}(; obj=:mse, maxiter=100, tol=cbrt(eps(T)), update_H=true,
              lambda_w=zero(T), lambda_h=zero(T), verbose=false)

Multiplicative-update algorithm (Lee & Seung). Both W and H must be initialized before solve!.

obj selects the objective: :mse (mean squared error) or :div (divergence). lambda_w and lambda_h are L1 regularization coefficients for W and H. maxiter bounds the iterations, tol is the convergence tolerance on the relative change of W and H, update_H=false holds H fixed, and verbose=true prints per-iteration progress.

Reference: D. D. Lee and H. S. Seung, "Algorithms for Non-negative Matrix Factorization," Advances in NIPS, 2001.

Examples

julia> X = rand(8, 6);

julia> W, H = NMF.randinit(X, 3);

julia> r = NMF.solve!(NMF.MultUpdate{Float64}(obj=:mse, maxiter=50), X, W, H);

julia> size(r.W), size(r.H)
((8, 3), (3, 6))
source

Projected alternating least squares

NMF.ProjectedALS — Type
ProjectedALS{T}(; maxiter=100, tol=cbrt(eps(T)), update_H=true,
                lambda_w=cbrt(eps(T)), lambda_h=cbrt(eps(T)), verbose=false)

Naive projected alternating least squares. Each iteration solves for W or H by unconstrained least squares and then clamps negative entries to zero. Only W needs to be initialized before solve!.

lambda_w and lambda_h are L2 regularization coefficients for W and H. maxiter bounds the iterations, tol is the convergence tolerance on the relative change of W and H, update_H=false holds H fixed, and verbose=true prints per-iteration progress.

Examples

julia> X = rand(8, 6);

julia> W, H = NMF.randinit(X, 3);

julia> r = NMF.solve!(NMF.ProjectedALS{Float64}(maxiter=50), X, W, H);

julia> size(r.W), size(r.H)
((8, 3), (3, 6))
source

Alternating least squares with projected gradient descent

NMF.ALSPGrad — Type
ALSPGrad{T}(; maxiter=100, maxsubiter=200, tol=cbrt(eps(T)),
            tolg=eps(T)^(1/4), update_H=true, verbose=false)

Alternating least squares solved by projected gradient descent (Lin). Both W and H must be initialized before solve!.

Each outer iteration updates W and H by an inner projected-gradient solve. maxiter bounds the outer iterations and maxsubiter the inner ones; tolg is the gradient-norm tolerance (first-order optimality) for the inner solve. tol is the convergence tolerance on the relative change of W and H, update_H=false holds H fixed, and verbose=true prints per-iteration progress.

Reference: C.-J. Lin, "Projected Gradient Methods for Non-negative Matrix Factorization," Neural Computation, 19(10):2756-2779, 2007.

Examples

julia> X = rand(8, 6);

julia> W, H = NMF.randinit(X, 3);

julia> r = NMF.solve!(NMF.ALSPGrad{Float64}(maxiter=50, tolg=1.0e-6), X, W, H);

julia> size(r.W), size(r.H)
((8, 3), (3, 6))
source

Coordinate descent

NMF.CoordinateDescent — Type
CoordinateDescent{T}(; maxiter=100, tol=cbrt(eps(T)), update_H=true,
                     α=zero(T), l₁ratio=zero(T), regularization=:both,
                     shuffle=false, verbose=false)

Coordinate-descent solver using Fast Hierarchical Alternating Least Squares (Cichocki & Phan), following the scikit-learn implementation.

α scales the regularization terms and l₁ratio ∈ [0, 1] mixes the L1 and L2 penalties. regularization selects what the penalty applies to: :components (H), :transformation (W), :both, or :none. Set shuffle=true to randomize the coordinate order on each sweep. maxiter bounds the iterations, tol is the convergence tolerance on the relative change of W and H, update_H=false holds H fixed, and verbose=true prints per-iteration progress.

Reference: A. Cichocki and A.-H. Phan, "Fast local algorithms for large scale nonnegative matrix and tensor factorizations," IEICE Transactions on Fundamentals, 92-A(3):708-721, 2009.

Examples

julia> X = rand(8, 6);

julia> W, H = NMF.randinit(X, 3);

julia> r = NMF.solve!(NMF.CoordinateDescent{Float64}(maxiter=50, α=0.5, l₁ratio=0.5), X, W, H);

julia> size(r.W), size(r.H)
((8, 3), (3, 6))
source

Greedy coordinate descent

NMF.GreedyCD — Type
GreedyCD{T}(; maxiter=100, tol=cbrt(eps(T)), update_H=true,
            lambda_w=zero(T), lambda_h=zero(T), verbose=false)

Greedy coordinate-descent algorithm with variable selection (Hsieh & Dhillon). Both W and H must be initialized before solve!.

lambda_w and lambda_h are L1 regularization coefficients for W and H. maxiter bounds the iterations, tol is the convergence tolerance on the relative change of W and H, update_H=false holds H fixed, and verbose=true prints per-iteration progress.

Reference: C.-J. Hsieh and I. S. Dhillon, "Fast coordinate descent methods with variable selection for non-negative matrix factorization," Proc. 17th ACM SIGKDD, 1064-1072, 2011.

Examples

julia> X = rand(8, 6);

julia> W, H = NMF.randinit(X, 3);

julia> r = NMF.solve!(NMF.GreedyCD{Float64}(maxiter=50), X, W, H);

julia> size(r.W), size(r.H)
((8, 3), (3, 6))
source

Successive projection algorithm

NMF.SPA — Type
SPA(; obj=:mse)

Successive Projection Algorithm for separable NMF (Gillis & Vavasis). Intended for use with W and H produced by spa initialization: passing this to solve! assembles the Result and its objective value without further iterating.

obj selects the reported objective: :mse (mean squared error) or :div (divergence).

Reference: N. Gillis and S. A. Vavasis, "Fast and robust recursive algorithms for separable nonnegative matrix factorization," IEEE Transactions on Pattern Analysis and Machine Intelligence, 36(4):698-714, 2014.

Examples

julia> X = rand(8, 6);

julia> W, H = NMF.spa(X, 3);

julia> r = NMF.solve!(NMF.SPA(obj=:mse), X, W, H);

julia> size(r.W), size(r.H)
((8, 3), (3, 6))
source