Rate this Page

torch.Tensor.to_sparse_csr#

Tensor.to_sparse_csr(dense_dim=None)Tensor#

Convert a tensor to compressed row storage format (CSR). Except forstrided tensors, only works with 2D tensors. If theself isstrided, then the number of dense dimensions could be specified, and ahybrid CSR tensor will be created, withdense_dim dense dimensionsandself.dim() - 2 - dense_dim batch dimension.

Parameters

dense_dim (int,optional) – Number of dense dimensions of theresulting CSR tensor. This argument should be used only ifself is a strided tensor, and must be a value between 0and dimension ofself tensor minus two.

Example:

>>>dense=torch.randn(5,5)>>>sparse=dense.to_sparse_csr()>>>sparse._nnz()25>>>dense=torch.zeros(3,3,1,1)>>>dense[0,0]=dense[1,2]=dense[2,1]=1>>>dense.to_sparse_csr(dense_dim=2)tensor(crow_indices=tensor([0, 1, 2, 3]),       col_indices=tensor([0, 2, 1]),       values=tensor([[[1.]],                      [[1.]],                      [[1.]]]), size=(3, 3, 1, 1), nnz=3,       layout=torch.sparse_csr)