Skip to main content

A PyTorch implementation on Non-negative Matrix Factorization.

Project description

PyPI Python License

A PyTorch implementation on Non-negative Matrix Factorization.

Installation

This package is published on PyPI:

pip install nmf-torch

Usage

NMF

Given a non-negative numeric matrix X of shape M-by-N (M is number of samples, N number of features) in either numpy array or torch tensor structure, run the following code:

from nmf import run_nmf
H, W, err = run_nmf(X, n_components=20)

will decompose X into two new non-negative matrices:

  • H of shape (M, 20), representing the transformed coordinates of samples regarding the 20 components;

  • W of shape (20, N), representing the composition of each component in terms of features;

along with the loss between X and its approximation H*W.

Advanced Settings

By default, run_nmf function uses the batch HALS solver for NMF decomposition. In total, there are other solvers available in NMF-torch:

  • HALS: Hierarchical Alternative Least Squares ([Kimura et al., 2015]). The default.

  • MU: Multiplicative Update. Set algo='mu' in run_nmf function.

  • BPP: Alternative non-negative least squares with Block Principal Pivoting method ([Kim & Park, 2011]). Set algo='bpp' in run_nmf function.

Besides, each solver has two modes: batch and online. The online mode is a modified version which is scalable for input matrix of a large number of samples. You can set mode='online' in run_nmf function to switch to use the online mode.

The default beta loss is Frobenius (L2) distance, which is the most commonly used. By changing beta_loss parameter in run_nmf function, users can specify other beta loss metrics:

  • KL divergence: beta_loss='kullback-leibler' or beta_loss=1.0;

  • Itakura-Saito divergence: beta_loss='itakura-saito' or beta_loss=0;

  • Or any non-negative float number.

Notice that since online mode only works for L2 loss, if you specify other beta loss, run_nmf will automatically switch back to batch mode.

For the other parameters in run_nmf function, please type help(run_nmf) in your Python interpreter to view.


Data Integration using Integrative NMF (iNMF)

In this case, we have a list of k batches, with their corresponding non-negative numeric matrices to be integrated. Let X be such a list, and all matrices in X have the same number of features, i.e. each Xi in X has shape (Mi, N), where Mi is number of samples in batch i, and N is number of features.

The following code:

from nmf import integrative_nmf
H, W, V, err = integrative_nmf(X, n_components=20)

will perform iNMF, which results in the following non-negative matrices:

  • H: List of matrices of shape (Mi, 20), each of which represents the transformed coordinates of samples regarding components of the corresponding batch;

  • W of shape (20, N), representing the common composition (shared information) across the given batches in terms of features;

  • V: List of matrices of the same shape (20, N), each of which represents the batch-specific composition in terms of features of the corresponding batch,

along with the overall L2 loss between Xi and its approximation Hi * (W + Vi) for each batch i.

Advanced Settings

Similarly as in run_nmf function above, integrative_nmf provides 2 modes (batch and online) and 3 solvers: HALS, MU, and BPP. By default, batch HALS is used. You can switch to other solvers and modes by specifying algo and mode parameters.

There is another important parameter lam for the coefficient for regularization terms, with default value 5.0. If set to 0, then no regularization will be applied.

Notice that only L2 loss is accepted in iNMF.

For the other parameters in integrative_nmf function, please type help(integrative_nmf) in your Python interpreter to view.

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

nmf-torch-0.1.1.tar.gz (158.3 kB view details)

Uploaded Source

Built Distributions

nmf_torch-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (705.4 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

nmf_torch-0.1.1-cp39-cp39-macosx_10_9_x86_64.whl (142.9 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

nmf_torch-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (757.7 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

nmf_torch-0.1.1-cp38-cp38-macosx_10_9_x86_64.whl (151.8 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

nmf_torch-0.1.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (659.5 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

nmf_torch-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl (156.3 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

Details for the file nmf-torch-0.1.1.tar.gz.

File metadata

  • Download URL: nmf-torch-0.1.1.tar.gz
  • Upload date:
  • Size: 158.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.7.10

File hashes

Hashes for nmf-torch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5d2c967e5caa6f9cd4bb662290a4f34942a1dc51778be28e283ce57233ef3ce6
MD5 32a79ff2806d7b592087c04dbf010643
BLAKE2b-256 8592b3b0ace261e2da347ccd35fc7afa080d68c1f992827190a4991d14280516

See more details on using hashes here.

File details

Details for the file nmf_torch-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nmf_torch-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 69de5ea83adbb0ad79415b5b6f7d525b5c511ff5c9e1f97acdc563ea86c6d6ab
MD5 9daeef71a22e24b6ee1a8c49b7d26bc1
BLAKE2b-256 47c4efed0bf014017ce18baec317fbef6b81c2be68e5dea5703ee6f9a4ccff27

See more details on using hashes here.

File details

Details for the file nmf_torch-0.1.1-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: nmf_torch-0.1.1-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 142.9 kB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.7.10

File hashes

Hashes for nmf_torch-0.1.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 dd159cbcff0c6cb2792aa062dc6dff04cbe8254627c5b7cb69bbedba3728b9f0
MD5 78b559e33ebd8a1b78679bdd093401d1
BLAKE2b-256 e0a1eab01c63164819733031fca67c1be371bc88b9e0d1789a45dce2beab0d65

See more details on using hashes here.

File details

Details for the file nmf_torch-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nmf_torch-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5386cc43584db5e4ffbb03c1a29e43277e4d726d508b0f1340054ae5eb14deec
MD5 8485823b956e807d07177c886aa20eb9
BLAKE2b-256 0aab40eff3daef53f5893f106a410a03045e718a35547b1f37d0131593a876ee

See more details on using hashes here.

File details

Details for the file nmf_torch-0.1.1-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: nmf_torch-0.1.1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 151.8 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.7.10

File hashes

Hashes for nmf_torch-0.1.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3b2d036a2fd95956fa83f87e15b3609692da5b2af9006125ef248f423f157a6f
MD5 818fd5a01ace3a82ec87230a183ffc88
BLAKE2b-256 9c613be73d3e560fcbf1bcc29a0126964dc1ef9865ec6a0c116cdf8eaa7bac7d

See more details on using hashes here.

File details

Details for the file nmf_torch-0.1.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nmf_torch-0.1.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 05449f76e09eb5d0a465dd9a848e6134cac8450cc1dd82421121ac23eb8fce58
MD5 6571b564a5708a188b061f2b54aafb63
BLAKE2b-256 6199a04150e6a0f61bad9244fe927b214a59cdf4fdb71c1e8f6327c984b869a3

See more details on using hashes here.

File details

Details for the file nmf_torch-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: nmf_torch-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 156.3 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.7.10

File hashes

Hashes for nmf_torch-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1cd6d8790886aca4119e5914ee0b36f6d4b7b89c7fcb03f1da97202acfc01801
MD5 6baf4f1555b23abd4a9c8e6da8bd5a7f
BLAKE2b-256 cb0d26bd1110d591071d1c003dff497dce20181c50b35a7c28d4a1093aabfa35

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