lenskit.math.sparse#

Sparse matrix utility functions.

Functions

normalize_sparse_rows()

Normalize the rows of a sparse matrix.

safe_spmv(matrix, vector)

Sparse matrix-vector multiplication working around PyTorch bugs.

torch_sparse_from_scipy(M[, layout])

Convert a SciPy sps.coo_array into a torch sparse tensor.

torch_sparse_to_scipy(M)

lenskit.math.sparse.normalize_sparse_rows(matrix: Tensor, method: Literal['center'], inplace: bool = False) tuple[Tensor, Tensor]#
lenskit.math.sparse.normalize_sparse_rows(matrix: Tensor, method: Literal['unit'], inplace: bool = False) tuple[Tensor, Tensor]

Normalize the rows of a sparse matrix.

lenskit.math.sparse.torch_sparse_from_scipy(M, layout='coo')#

Convert a SciPy sps.coo_array into a torch sparse tensor.

Parameters:
Return type:

Tensor

lenskit.math.sparse.safe_spmv(matrix, vector)#

Sparse matrix-vector multiplication working around PyTorch bugs.

This is equivalent to torch.mv() for sparse CSR matrix and dense vector, but it works around PyTorch bug 127491 by falling back to SciPy on ARM.

Parameters:
Return type:

Tensor