torch.sparse — PyTorch 1.10.1 documentation
pytorch.org › docs › stablePyTorch provides torch.Tensor to represent a multi-dimensional array containing elements of a single data type. By default, array elements are stored contiguously in memory leading to efficient implementations of various array processing algorithms that relay on the fast access to array elements. However, there exists an important class of multi-dimensional arrays, so-called sparse arrays, where the contiguous memory storage of array elements turns out to be suboptimal.
torch.sparse.mm — PyTorch 1.10.1 documentation
pytorch.org › docs › stablePerforms a matrix multiplication of the sparse matrix mat1 and the (sparse or strided) matrix mat2. Similar to torch.mm (), If mat1 is a. ( n × m) (n \times m) (n× m) tensor, mat2 is a. ( m × p) (m \times p) (m× p) tensor, out will be a. ( n × p) (n \times p) (n× p) tensor. mat1 need to have sparse_dim = 2 . This function also supports backward for both matrices.
torch.sparse — PyTorch 1.10.1 documentation
https://pytorch.org/docs/stable/sparse.htmlDense dimensions always follow sparse dimensions, that is, mixing of dense and sparse dimensions is not supported. Uncoalesced sparse COO tensors ¶ PyTorch sparse COO tensor format permits uncoalesced sparse tensors, where there may be duplicate coordinates in the indices; in this case, the interpretation is that the value at that index is the sum of all duplicate …
How to convert a dense matrix to a sparse one? - PyTorch Forums
discuss.pytorch.org › t › how-to-convert-a-denseSep 25, 2017 · January 22, 2018, 3:11am #3. To reformat ezyang’s answer as a simple function, def to_sparse(x): """ converts dense tensor x to sparse format """ x_typename = torch.typename(x).split('.')[-1] sparse_tensortype = getattr(torch.sparse, x_typename) indices = torch.nonzero(x) if len(indices.shape) == 0: # if all elements are zeros return sparse_tensortype(*x.shape) indices = indices.t() values = x[tuple(indices[i] for i in range(indices.
python - How to convert a PyTorch sparse_coo_tensor into a ...
stackoverflow.com › questions › 64553148Oct 27, 2020 · I create a sparse_coo tensor in PyTorch: import torch # create indices i = torch.tensor ( [ [0, 1, 1], [2, 0, 2]]) # create values v = torch.tensor ( [3, 4, 5], dtype=torch.float32) # create sparse_coo_tensor sparse_tensor = torch.sparse_coo_tensor (i, v, [2, 4]) Now I want to convert a PyTorch sparse tensor into a PyTorch dense tensor.
torch.Tensor.to_sparse — PyTorch 1.10.1 documentation
pytorch.org › torchtorch.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 ...