IncSVD: A python package for dynamic Truncated SVD
IncSVD is a python package capable of dynamically maintaining the incremental Truncated Singular Value Decomposition (Truncated SVD) of evolving matrices.
This is the code implementation of the paper "Fast Updating Truncated SVD for Representation Learning with Sparse Matrices" (ICLR'24).
Install and Setup.
It is recommended to use python 3.8 or higher and install IncSVD with the following command.
pip install IncSVD
User Guide
Initialize the IncSVD using an $m \times n$ matrix data_init given the rank (dimension) k for which the truncated SVD needs to be computed. Here is an example to initialize a $1000 \times 1000$ matrix with truncated dimension $k = 64$.
class EvolvingMatrix(data, k, sparse = True, method = "RPI")
- data: data matrix
Data matrix used to initialize truncated SVD.
The format of this initial matrix can be
np.ndarrayorscipy.sparse. - k: int Truncated dimension in the maintained SVD. It should be a positive integer not greater than the length of the sides of the original matrix.
- sparse: bool, default=True Set true implies the update process will employ the proposed acceleration methods [1]. Set false implies using the original methods (ZhaSimon's [2], Vecharynski's [3], Yamazaki's [4]).
- method: {'ZhaSimon', 'GKL', 'RPI'}, default='RPI'
Method for SVD update.
It can be chosen from
['ZhaSimon', 'GKL', 'RPI'].
import scipy.sparse
import IncSVD.EvolvingMatrix as EM
m, n, k = 1000, 1000, 64
# data_init = scipy.sparse.rand(m, n, density=0.005).tocsr()
M = EM.EvolvingMatrix(data_init, k, sparse=True, method="ZhaSimon")
Row / Column Update
Expand the original matrix in row or column dimensions and update the corresponding SVD results.
add_row(update_matrix, l=None, t=None)
- update_matrix: data matrix
Data matrix to be appended.
This matrix should have the same number of columns as the original matrix. If type
sparseis True, this matrix should be inscipy.sparse._csr.csr_matrix. - l: {int, None}
Approximate dimension for the update matrix.
When using GKL or RPI, two methods that require an approximation space, it is necessary to set
l. The update matrix of rankswill be approximated with a smaller space of ranklfor further speedup. Set toNonewhen GKL and RPI is not applied. - t: {int, None}
The number of iterations to perform in the random power iteration.
It needs to be set when using the RPI. This approximation is more accurate when
tis larger, while the computational overhead is larger. Set toNonewhen RPI is not applied.
add_column(update_matrix, l=None, t=None)
- update_matrix: data matrix
Data matrix to be appended.
This matrix should have the same number of rows as the original matrix. If type
sparseis True, this matrix should be inscipy.sparse._csc.csc_matrix. - l: {int, None}
Approximate dimension for the update matrix.
Set to
Nonewhen GKL and RPI is not applied. - t: {int, None}
The number of iterations to perform in the random power iteration.
Set to
Nonewhen RPI is not applied.
''' Add rows '''
# data_append_row = scipy.sparse.rand(m, 10, density=0.005).tocsr() * 10
M.add_row(data_append_row)
''' Add columns '''
# data_append_col = scipy.sparse.rand(n, 10, density=0.005).tocsc() * 10
M.add_column(data_append_col)
Low-rank Update.
Perform low-rank update $\overline{\mathbf{A}} = \mathbf{A} + \mathbf{D}_m \cdot \mathbf{E}_m^{\top}$, where $\mathbf{A}$ is the original matrix, $\mathbf{D}_m \in \mathbb{R}^{m \times s}$ and $\mathbf{E}_m \in \mathbb{R}^{n \times s}$.
update_weight(self, update_matrix_B, update_matrix_C, l=None, t=None):
- update_matrix_B: data matrix
Data matrix to be appended.
This matrix should have the same number of columns as the original matrix. If type
sparseis True, this matrix should be inscipy.sparse._csc.csc_matrix. - update_matrix_C: data matrix
Data matrix to be appended.
This matrix should have the same number of columns as the original matrix. If type
sparseis True, this matrix should be inscipy.sparse._csc.csc_matrix. - l: {int, None}
Approximate dimension for the update matrix.
Set to
Nonewhen GKL and RPI is not applied. - t: {int, None}
The number of iterations to perform in the random power iteration.
Set to
Nonewhen RPI is not applied.
''' Low-rank update '''
# data_dm = scipy.sparse.rand(m, 10, density=0.005).tocsc() * 10
# data_em = scipy.sparse.rand(n, 10, density=0.005).tocsc() * 10
M.update_weight(data_dm, data_em)
Get the result.
Queries the current SVD result.
''' Get the result. '''
Uk, Sigmak, Vk = M.Uk, M.Sigmak, M.Vk
''' Get a row of singular vectors. '''
# i = 0
Uk0, Vk0 = M.Uki(i), M.Vki(i)
''' A low-rank approximation of data matrix can be obtained from. '''
Ak = Uk @ np.diag(Sigmak) @ Vk.T
Contact Us
If you have any questions about this code repository, feel free to raise an issue or email denghaoran@zju.edu.cn.
Citations.
If you find our work useful, please consider citing the following paper:
@inproceedings{deng2024incsvd,
title={Fast Updating Truncated {SVD} for Representation Learning with Sparse Matrices},
author={Haoran Deng and Yang Yang and Jiahe Li and Cheng Chen and Weihao Jiang and Shiliang Pu},
booktitle={The Twelfth International Conference on Learning Representations},
year={2024},
url={https://openreview.net/forum?id=CX2RgsS29V}
}
Metadata
Release files for IncSVD 0.2.7
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 | Actions |
|---|---|---|---|---|
| incsvd-0.2.7.tar.gz | 16.0 kB | Without provenance | Details for incsvd-0.2.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| IncSVD-0.2.7-py3-none-any.whl | Python 3 | none | any | Details for IncSVD-0.2.7-py3-none-any.whl |
Total release size: 26.4 kB
Release files / incsvd-0.2.7.tar.gz
| Download URL | incsvd-0.2.7.tar.gz |
|---|---|
| Size | 16.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a4cb01ddedee0535a944b87798e7c02989ee9fa7f95f6b539472c0f945a8b626
|
|
BLAKE2b-256 checksum How to use checksums |
f23b922af636bbba4119e73eb887c4f7e4f1adcbe38bd3c4fad910f096bb4f42
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.15
|
Release files / IncSVD-0.2.7-py3-none-any.whl
| Download URL | IncSVD-0.2.7-py3-none-any.whl |
|---|---|
| Size | 10.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e7cc084648fec3284add4baecb149672450f315fe85507ad1b547ec795e4382c
|
|
BLAKE2b-256 checksum How to use checksums |
bc5d8c39d16ebd99e7029d119fcd1980fdf642f4dd54d5a517771c982b2ee76d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.15
|
Security for IncSVD 0.2.7
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.