Shortcuts

torch.Tensor.to_sparse

Tensor.to_sparse(sparseDims) Tensor

Returns a sparse copy of the tensor. PyTorch supports sparse tensors in coordinate format.

Parameters

sparseDims (int, optional) – the number of sparse dimensions to include in the new sparse tensor

Example:

>>> d = torch.tensor([[0, 0, 0], [9, 0, 10], [0, 0, 0]])
>>> d
tensor([[ 0,  0,  0],
        [ 9,  0, 10],
        [ 0,  0,  0]])
>>> d.to_sparse()
tensor(indices=tensor([[1, 1],
                       [0, 2]]),
       values=tensor([ 9, 10]),
       size=(3, 3), nnz=2, layout=torch.sparse_coo)
>>> d.to_sparse(1)
tensor(indices=tensor([[1]]),
       values=tensor([[ 9,  0, 10]]),
       size=(3, 3), nnz=1, layout=torch.sparse_coo)
to_sparse(*, layout=None, blocksize=None, dense_dim=None) Tensor

Returns a sparse tensor with the specified layout and blocksize. If the self is strided, the number of dense dimensions could be specified, and a hybrid sparse tensor will be created, with dense_dim dense dimensions and self.dim() - 2 - dense_dim batch dimension.

Note

If the self layout and blocksize parameters match with the specified layout and blocksize, return self. Otherwise, return a sparse tensor copy of self.

Parameters
  • layout (torch.layout, optional) – The desired sparse layout. One of torch.sparse_coo, torch.sparse_csr, torch.sparse_csc, torch.sparse_bsr, or torch.sparse_bsc. Default: if None, torch.sparse_coo.

  • blocksize (list, tuple, torch.Size, optional) – Block size of the resulting BSR or BSC tensor. For other layouts, specifying the block size that is not None will result in a RuntimeError exception. A block size must be a tuple of length two such that its items evenly divide the two sparse dimensions.

  • dense_dim (int, optional) – Number of dense dimensions of the resulting CSR, CSC, BSR or BSC tensor. This argument should be used only if self is a strided tensor, and must be a value between 0 and dimension of self tensor minus two.

Example:

>>> x = torch.tensor([[1, 0], [0, 0], [2, 3]])
>>> x.to_sparse(layout=torch.sparse_coo)
tensor(indices=tensor([[0, 2, 2],
                       [0, 0, 1]]),
       values=tensor([1, 2, 3]),
       size=(3, 2), nnz=3, layout=torch.sparse_coo)
>>> x.to_sparse(layout=torch.sparse_bsr, blocksize=(1, 2))
tensor(crow_indices=tensor([0, 1, 1, 2]),
       col_indices=tensor([0, 0]),
       values=tensor([[[1, 0]],
                      [[2, 3]]]), size=(3, 2), nnz=2, layout=torch.sparse_bsr)
>>> x.to_sparse(layout=torch.sparse_bsr, blocksize=(2, 1))
RuntimeError: Tensor size(-2) 3 needs to be divisible by blocksize[0] 2
>>> x.to_sparse(layout=torch.sparse_csr, blocksize=(3, 1))
RuntimeError: to_sparse for Strided to SparseCsr conversion does not use specified blocksize

>>> x = torch.tensor([[[1], [0]], [[0], [0]], [[2], [3]]])
>>> x.to_sparse(layout=torch.sparse_csr, dense_dim=1)
tensor(crow_indices=tensor([0, 1, 1, 3]),
       col_indices=tensor([0, 0, 1]),
       values=tensor([[1],
                      [2],
                      [3]]), size=(3, 2, 1), nnz=3, layout=torch.sparse_csr)

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources