Non-negative Matrix Fatorization in PyTorch
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
NMFmodule). - Regularization.
- NNDSVD initialization.
- 2/3-D deconvolutional module.
- PLCA.
- Documentation.
- ipynb examples.
- Refactor PLCA module.
Metadata
Release files for torchnmf 0.3.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| torchnmf-0.3.5.tar.gz | 19.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| torchnmf-0.3.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 39.3 kB
Release files / torchnmf-0.3.5.tar.gz
| Download URL | torchnmf-0.3.5.tar.gz |
|---|---|
| Size | 19.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eb07535b908f67b4582c3581c2b72463ceb508f20b1269e99a8708e809b85da5
|
|
BLAKE2b-256 checksum How to use checksums |
6c912310993b5df27fceb032f52914b5687fb14e2b57fc6e83df40a5b4079ecb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / torchnmf-0.3.5-py3-none-any.whl
| Download URL | torchnmf-0.3.5-py3-none-any.whl |
|---|---|
| Size | 20.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a062c4d83cc2c97e18692a9d5b9e8b1ec4aaf759df65fdf268356422bcf29bd4
|
|
BLAKE2b-256 checksum How to use checksums |
c58e55429512077dc1ddf301e07d0a8ded9bd41b4bcb4a60c6a394520181b68b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Security for torchnmf 0.3.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.