Skip to main content

A pytorch package for Non-negative Matrix Factorization

Project description

Non-negative Matrix Fatorization in PyTorch

build codecov Documentation Status PyPI version

PyTorch is not only a good deep learning framework, but also a fast tool when it comes to matrix operations and convolutions on large data. A great example is PyTorchWavelets.

In this package I implement NMF, PLCA and their deconvolutional variations in PyTorch based on torch.nn.Module, so the models can be moved freely among CPU/GPU devices and utilize parallel computation of cuda. We also utilize the computational graph from torch.autograd to derive updated coefficients so the amount of codes is reduced and easy to maintain.

Modules

NMF

Basic NMF and NMFD module minimizing beta-divergence using multiplicative update rules.

The interface is similar to sklearn.decomposition.NMF with some extra options.

  • NMF: Original NMF algorithm.
  • NMFD: 1-D deconvolutional NMF algorithm.
  • NMF2D: 2-D deconvolutional NMF algorithm.
  • NMF3D: 3-D deconvolutional NMF algorithm.

PLCA

Basic PLCA and SIPLCA module using EM algorithm to minimize KL-divergence between the target distribution and the estimated distribution.

  • PLCA: Original PLCA (Probabilistic Latent Component Analysis) algorithm.
  • SIPLCA: Shift-Invariant PLCA algorithm (similar to NMFD).
  • SIPLCA2: 2-D deconvolutional SIPLCA algorithm.
  • SIPLCA3: 3-D deconvolutional SIPLCA algorithm.

Usage

Here is a short example of decompose a spectrogram using deconvolutional NMF:

import torch
import librosa
from torchnmf.nmf import NMFD
from torchnmf.metrics import kl_div

y, sr = librosa.load(librosa.util.example_audio_file())
y = torch.from_numpy(y)
windowsize = 2048
S = torch.stft(y, windowsize, 
               window=torch.hann_window(windowsize),
               return_complex=True).abs().cuda()
S = S.unsqueeze(0)

R = 8   # number of components
T = 400 # size of convolution window

net = NMFD(S.shape, rank=R, T=T).cuda()
# run extremely fast on gpu
net.fit(S)      # fit to target matrix S
V = net()
print(kl_div(V, S))        # KL divergence to S

A more detailed version can be found here. See our documentation to find out more usage of this package.

Compare to sklearn

The barchart shows the time cost per iteration with different beta-divergence. It shows that pytorch-based NMF has a much more constant process time across different beta values, which can take advantage when beta is not 0, 1, or 2. This is because our implementation use the same computational graph regardless which beta-divergence are we minimizing. It runs even faster when computation is done on GPU. The test is conducted on a Acer E5 laptop with i5-7200U CPU and GTX 950M GPU.

Installation

pip install torchnmf

Requirements

  • PyTorch
  • tqdm

Tips

  • If you notice significant slow down when operating on CPU, please flush denormal numbers by torch.set_flush_denormal(True).

TODO

  • Support sparse matrix target (only on NMF module).
  • Regularization.
  • NNDSVD initialization.
  • 2/3-D deconvolutional module.
  • PLCA.
  • Documentation.
  • ipynb examples.
  • Refactor PLCA module.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchnmf-0.3.4.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

torchnmf-0.3.4-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

Details for the file torchnmf-0.3.4.tar.gz.

File metadata

  • Download URL: torchnmf-0.3.4.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for torchnmf-0.3.4.tar.gz
Algorithm Hash digest
SHA256 5c75f439ade49b7b6f1351db5549d61cad196d29a2a4319c3fd8c58471817a07
MD5 6d1ab86f46bd9acb2c30dbc1132e8abd
BLAKE2b-256 c13341c6657a46f6bfe6a5acf7c94e0bf9df4a9538c8e12e6d54e1781cfd0e2f

See more details on using hashes here.

File details

Details for the file torchnmf-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: torchnmf-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 24.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for torchnmf-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2e917c4b17f5383007beb2cce1229443e7bde15d0a82001750fb0e4aa20a685c
MD5 73508bb219155c70a86294b257a4bb50
BLAKE2b-256 f403990e9e983d156d927a114fd088c7cde28c85d8b4be4e7be8d24a86a73584

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page