Returns a sparse copy of the tensor. PyTorch supports sparse tensors in coordinate format.
>>> 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)
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
.
>>> 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)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4