Home

Awesome

NMFLibrary: Non-negative Matrix Factorization Library

Authors: Hiroyuki Kasai

Last page update: July 22, 2022

Latest library version: 2.1 (see Release notes for more info)

<br />

Announcement

We are very welcome to your contribution. Please tell us

<br />

Introduction

The NMFLibrary is a pure-Matlab library of a collection of algorithms of non-negative matrix factorization (NMF). The solvers can be also called from python (see demo.py).

<br />

Bibliograph

If this library is useful for you, please cite this as presented below:

@misc{kasai_NMFLibrary_2017,
    Author = {Kasai, Hiroyuki},
     Title = {{NMFLibrary}: MATLAB library for non-negative matrix factorization (NMF)},
     Year  = {2017},
     Howpublished = {\url{https://github.com/hiroyuki-kasai/NMFLibrary}}
}
<br />

<a name="supp_solver"> List of solver algorithms available in NMFLibrary </a>

<br />

Algorithm configurations

CategoryName in example codesfunctionoptions.algother options
Frobenius-normFro-MUfro_mu_nmfmumetric='euc'
Modified Fro-MUfro_mu_nmfmod_mu
Accelerated Fro-MUfro_mu_nmfacc_mu
PGDpgd_nmfpgd
Direct PGDpgd_nmfdirect_pgd
Adaptive-step PGDpgd_nmfadp_step_pgd
ALSals_nmfals
Hierarchical ALSals_nmfhals_mu
Accelerated Hierarchical ALSals_nmfacc_hals_mu
ASGROUPanls_nmfanls_asgroup
ASGIVENSanls_nmfanls_asgivens
BPPanls_nmfanls_bpp
DivergenceDiv-MU-KLdiv_mu_nmfmetric='kl-div'
Div-MU-ALPHAdiv_mu_nmfmetric='alpha-div'
Div-MU-BETAdiv_mu_nmfmetric='beta-div'
Div-MU-ISdiv_mu_nmfmetric='beta-div' d_beta=0
Div-MU-KLdiv_mu_nmfmetric='beta-div' d_beta=1
Div-ADMM-ISdiv_admm_nmfmetric='beta-div' d_beta=0
Div-ADMM-KLdiv_admm_nmfmetric='beta-div' d_beta=1
KL-FPAkl_fpa_nmf
KL-BMDkl_bmd_nmf
SemiSemi-MUsemi_mu_nmf
Semi-BCDsemi_bcd_nmf
VariantNeNMFnenmf
GNMFGNMF
SDNMFSDNMF
RobustRobust-MUrobust_mu_nmf
SparseSparse-MU-EUCsparse_mu_nmfmetric='euc'
Sparse-MU-KLsparse_mu_nmfmetric='kl-div'
sparseNMFsparse_nmf
SC-NMFsc_nmf
Nonsmooth-NMFns_nmfmetric='euc', update_alg='apg'
Proj-Sparseproj_sparse_nmf
PALM-Sparse-Smoothpalm_sparse_smooth_nmf
OrthogonalDTPPdtpp_nmf
Orth-MUorth_mu_nmf
NMF-HALS-SOhals_so_nmf
ALT-ONMFalternating_onmf
SymmetricSymmANLSsymm_anls
SymmHALSsymm_halsacc
SymmNewtonsymm_newton
OnlineIncremental-NMFincremental_mu_nmf
Online-MUonline_mu_nmf
Accelerated Online-MUacc_online_mu_nmf
SPGspg_nmf
Robust-Online-MUrobust_online_mu_nmf
ASAG-MU-NMFasag_mu_nmf
Stochastic-MUsmu_nmf
SVRMUsvrmu_nmf
R-SVRMUsvrmu_nmfrobust=true
SAGMUsagmu_nmf
ProbabilisticPNMF-VBvb_pro_nmf
PNMF-VB-ARDvb_pro_nmfard=true
Prob-NMprob_nmf
DeepDeep-Semideep_semi_nmf
Deep-Bidir-Semideep_bidirectional_nmf
Deep-nsNMFdeep_ns_nmf
Deep-Multiview-Semideep_multiview_semi_nmf
ConvexConvex-MUconvex_mu_nmfsub_mode='std'
Kernel-Convex-MUconvex_mu_nmfsub_mode='kernel'
SeparableSPAspa
SNPAsnpa
ConvolutiveMU-Convmu_conv_nmf
Heur-MU-Convheuristic_mu_conv_nmf
ADMM-Y-Convadmm_y_conv_nmf
ADMM-Seq-Convadmm_seq_conv_nmf
Rank2Rank2-NMFrank2nmf
Nonnegative matrix tri-factorizationSep-Symm-NMTFsep_symm_nmtf
Nonnegative under-approximationrecursive_nmurecursive_nmu
Minimum-volumeminvol-NMFminvol_nmf
Weighted Low-Rank matrix approximationWLRAwlra
<br />

Folders and files

<pre> ./ - Top directory. ./README.md - This readme file. ./run_me_first.m - The scipt that you need to run first. ./demo.m - Demonstration script to check and understand this package easily. ./demo_face.m - Demonstration script to check and understand this package easily. ./demo.py - Demonstration script to use this package easily from python. |plotter/ - Contains plotting tools to show convergence results and various plots. |auxiliary/ - Some auxiliary tools for this project. |solver/ - Contains various optimization algorithms. |--- frobenius_norm/ - NMF solvers with Frobenius norm metric. |--- divergence/ - NMF solvers with various divertence metrics (KL, beta, alpha, IS). |--- online/ - Online/stochstic NMF solvers. |--- sparse/ - Sparse NMF solvers. |--- robust/ - Robust NMF solvers. |--- orthogonal/ - Orthogonal NMF solvers. |--- symmetric/ - Symmetric NMF solvers. |--- semi/ - Semi NMF solvers. |--- deep/ - Deep NMF solvers. |--- probabilistic/ - Probabilistic NMF solvers. |--- convex/ - Convex NMF solver. |--- convolutive/ - Convolutive NMF solvers. |--- minvol/ - Minimum-volume rank-deficient NMF. |--- nm_under_approx/ - Recursive non-negative matrix underapproximation. |--- nmtf/ - Separable symmetric nonnegative matrix tri-factorization. |--- projective_nmf/ - Projective NMF solver. |--- rank2/ - rank-two NMF solver. |--- weight_lowrank_aprox/ - Weighted Low-Rank matrix Approximation algorithm. |--- nenmf/ - Nesterov's accelerated NMF solver. |--- nnls/ - Solvers for nonnegativity-constrained least squares. |--- 3rd_party/ - Solvers provided by 3rd_party. |--- solver_health_check.m - Health check scripts for solvers. |applications/ - Some appplications using NMF. </pre> <br />

First to do

Run run_me_first for path configurations.

%% First run the setup script
run_me_first; 
<br />

Simplest usage example: 4 steps!

Just execute demo for the simplest demonstration of this package. .

%% Execute the demonstration script
demo; 

The "demo.m" file contains below.

%% generate synthetic data non-negative matrix V size of (mxn)
m = 500;
n = 100;
V = rand(m,n);
    
%% Initialize rank to be factorized
rank = 5;

%% perform factroization
% Fro-MU
options.alg = 'mu';
[w_mu, infos_mu] = fro_mu_nmf(V, rank, options);
% Hierarchical ALS
options.alg = 'hals';
[w_hals, infos_hals] = als_nmf(V, rank, options);        
    
%% plot
display_graph('epoch','cost', {'MU', 'HALS'}, {w_mu, w_hals}, {infos_mu, infos_hals});

<br />

Let's take a closer look at the code above bit by bit. The procedure has only 4 steps! <br />

Step 1: Generate data

First, we generate synthetic data of V of size (mxn).

m = 500;
n = 100;
V = rand(m,n);

Step 2: Define rank

We set the rank value.

rank = 5;

Step 3: Perform solver

Now, you can perform nmf solvers, e.g., Frobenius-norm MU and Hierarchical ALS (HALS), calling solver functions, i.e., fro_mu_nmf() function and als_nmf() function after setting some optimization options.

% Fro-MU
options.alg = 'mu';
[w_mu, infos_mu] = fro_mu_nmf(V, rank, options);
% Hierarchical ALS
options.alg = 'hals';
[w_hals, infos_hals] = als_nmf(V, rank, options); 

They return the final solutions of w and the statistics information that include the histories of epoch numbers, cost values, norms of gradient, the number of gradient evaluations and so on.

Step 4: Show result

Finally, display_graph() provides output results of decreasing behavior of the cost values in terms of the number of iterrations (epochs) and time [sec].

display_graph('epoch','cost', {'Fro-MU', 'HALS'}, {w_mu, w_hals}, {infos_mu, infos_hals});
display_graph('time','cost', {'Fro-MU', 'HALS'}, {w_mu, w_hals}, {infos_mu, infos_hals});

That's it!

<img src="http://www.kasailab.com/public/github/NMFLibrary/images/cost.png" width="600"> <br />

More plots

"demo_face.m" illustrates the learned basis (dictrionary) in case of CBCL face datasets.

The dataset is first loaded into V instead of generating synthetic data in Step 1.

V = importdata('./data/CBCL_face.mat');

Then, we can display basis elements (W: dictionary) obtained with different algorithms additionally in Step 4.

plot_dictionnary(w_mu.W, [], [7 7]); 
plot_dictionnary(w_hals.W, [], [7 7]); 
<img src="http://www.kasailab.com/public/github/NMFLibrary/images/face_dictionary.png" width="600"> <br />

How to use NMFLibrary from python

Step 1: Find the path to the MATLAB folder

Run matlabroot in the MATLAB command window.

matlabroot; 

Step 2: Install the Engine API

To install the engine API, choose one of the following. You must call this python install command in the specified folder. The followings are examples in case of R2022a.

cd "c:\Program Files\MATLAB\R2022a\extern\engines\python"
python setup.py install
cd "/usr/local/MATLAB/R2022a/bin/matlab/extern/engines/python"
python setup.py install
cd "/Applications/MATLAB_R2022a.app/extern/engines/python"
python setup.py install

Step 3: Run demonstration code

python demo.py

As for Steps 1 and 2, see more details here.

<br />

License

<br />

Acknowledge

<br />

Problems or questions

If you have any problems or questions, please contact the author: Hiroyuki Kasai (email: hiroyuki dot kasai at waseda dot jp)

<br />

Release notes