Sketched matrix decompositions for PyTorch
Project description
skerch
: Sketched matrix decompositions for PyTorch
skerch
is a Python package to compute different decompositions (SVD, Hermitian Eigendecomposition, diagonal, subdiagonal, triangular, block-triangular) of linear operators via sketched methods.
- Built on top of PyTorch, with natural support for CPU and CUDA interoperability, and very few dependencies otherwise
- Works on matrices and matrix-free operators of potentially very large dimensionality
- Support for sketched measurements in a fully distributed fashion via HDF5 databases
References:
- Streaming Low-Rank Matrix Approximation with an Application to Scientific Simulation Joel A. Tropp, Alp Yurtsever, Madeleine Udell, and Volkan Cevher. 2019. SIAM Journal on Scientific Computing 41 (4): A2430–63.
- Stochastic diagonal estimation: probabilistic bounds and an improved algorithm Robert A. Baston and Yuji Nakatsukasa. 2022. CoRR abs/2201.10684.
See the documentation for more details, including examples for other decompositions and use cases.
Installation and basic usage
Install via:
pip install skerch
The sketched SVD of a linear operator op
of shape (h, w)
can be then computed simply via:
from skerch.decompositions import ssvd
q, u, s, vt, pt = ssvd(
op,
op_device=DEVICE,
op_dtype=DTYPE,
outer_dim=NUM_OUTER,
inner_dim=NUM_INNER,
)
Where the number of outer and inner measurements for the sketch is specified, and q @ u @ diag(s) @ vt @ pt
is a PyTorch matrix that approximates op
, where q, p
are thin orthonormal matrices of shape (h, NUM_OUTER)
and (NUM_OUTER, w)
respectively, and u, vt
are small orthogonal matrices of shape (NUM_OUTER, NUM_OUTER)
.
The op
object must simply satify the following criteria:
- It must have a
op.shape = (height, width)
attribute - It must implement the
w = op @ v
right-matmul operator, receiving and returning PyTorch vectors/matrices - It must implement the
w = v @ op
left-matmul operator, receiving and returning PyTorch vectors/matrices
skerch
provides a convenience PyTorch wrapper for the cases where op
interacts with NumPy arrays instead (e.g. SciPy linear operators like the ones used in CurvLinOps).
To get a good suggestion of the number of measurements required for a given shape and budget, simply run:
python -m skerch prio_hpars --shape=100,200 --budget=12345
The library also implements cheap a-posteriori methods to estimate the error of the obtained sketched approximation:
from skerch.a_posteriori import a_posteriori_error
from skerch.linops import CompositeLinOp, DiagonalLinOp
# (q, u, s, vt, pt) previously computed via ssvd
sketched_op = CompositeLinOp(
(
("Q", q),
("U", u),
("S", DiagonalLinOp(s)),
("Vt", vt),
("Pt", pt),
)
)
(f1, f2, frob_err) = a_posteriori_error(
op, sketched_op, NUM_A_POSTERIORI, dtype=DTYPE, device=DEVICE
)[0]
print("Estimated Frob(op):", f1**0.5)
print("Estimated Frob(sketched_op):", f2**0.5)
print("Estimated Frobenius Error:", frob_err**0.5)
For a given NUM_A_POSTERIORI
measurements (30 is generally OK), the probability of frob_err**0.5
being wrong by a certain amount can be queried as follows:
python -m skerch post_bounds --apost_n=30 --apost_err=0.5
See Getting Started, Examples, and API docs for more details.
Developers
Contributions are most welcome under this repo's LICENSE. Feel free to open an issue with bug reports, feature requests, etc.
The documentation contains a For Developers section with useful guidelines to interact with this repo.
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
Built Distribution
File details
Details for the file skerch-0.7.0.tar.gz
.
File metadata
- Download URL: skerch-0.7.0.tar.gz
- Upload date:
- Size: 1.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1023080a3aaffd372dc168cc326e828c3f721378700674627899a21a00907c2 |
|
MD5 | 4c3e5a8526650ee7cfdbaefc3c4bd106 |
|
BLAKE2b-256 | 7fa89a09f896c4904ac4a9e25b034c8138a9754000fd9772a47c4e474a8629f9 |
File details
Details for the file skerch-0.7.0-py3-none-any.whl
.
File metadata
- Download URL: skerch-0.7.0-py3-none-any.whl
- Upload date:
- Size: 78.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37bc3dd94179e83970433e1948c9b38c1b8a810d650e2d2716bf4b040bb961e6 |
|
MD5 | 24f6f4a9db152fe84837560cc481c8bb |
|
BLAKE2b-256 | 67ab08999bcd9266d35c268edea58b5c70fdc5acea5ddde36ce8613241c84aee |