Skip to content

Commit b39a2f2

Browse files
ezyangapaszke
authored andcommitted
Documentation for sparse tensors. (pytorch#1366)
1 parent d9f0139 commit b39a2f2

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ PyTorch is an optimized tensor library for deep learning using GPUs and CPUs.
2424

2525
torch
2626
tensors
27+
sparse
2728
storage
2829
nn
2930
optim

docs/source/sparse.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
.. currentmodule:: torch.sparse
2+
3+
Sparse tensors
4+
==============
5+
6+
.. warning::
7+
8+
This API is currently experimental and may change in the near future.
9+
10+
Torch supports sparse tensors in COO(rdinate) format, which can
11+
efficiently store and process tensors for which the majority of elements
12+
are zeros.
13+
14+
A sparse tensor is represented as a pair of dense tensors: a tensor
15+
which contains the actual values :class:`torch.sparse.values`, and a
16+
tensor which contains the coordinates of those values
17+
:class:`torch.sparse.indices`. A sparse tensor can be constructed
18+
by providing these two tensors, as well as the size of the sparse tensor
19+
(which cannot be inferred from these tensors!)
20+
21+
>>> i = torch.LongTensor([[0, 1], [2, 0]])
22+
>>> v = torch.FloatTensor([3, 4])
23+
>>> torch.sparse.FloatTensor(i, v, torch.Size([2,3])).to_dense()
24+
0 0 3
25+
4 0 0
26+
[torch.FloatTensor of size 2x2]
27+
28+
An empty sparse tensor can be constructed by specifying its size:
29+
30+
>>> torch.sparse.FloatTensor(2, 3)
31+
SparseFloatTensor of size 2x3 with indices:
32+
[torch.LongTensor with no dimension]
33+
and values:
34+
[torch.FloatTensor with no dimension]
35+
36+
Sparse tensors can have duplicate entries for an index; such a tensor is
37+
called non-coalesced. Duplicate entries are summed together when
38+
coalescing (or converting to another representation). Some operations
39+
(for example, :func:`torch.FloatTensor.add`) produce duplicate entries;
40+
if you repeatedly perform these operations, you should coalesce your
41+
sparse tensors to prevent them from growing too large.
42+
43+
.. class:: FloatTensor()
44+
45+
.. automethod:: add
46+
.. automethod:: add_
47+
.. automethod:: clone
48+
.. automethod:: contiguous
49+
.. automethod:: dim
50+
.. automethod:: div
51+
.. automethod:: div_
52+
.. automethod:: get_device
53+
.. automethod:: hspmm
54+
.. automethod:: indices
55+
.. automethod:: is_contiguous
56+
.. automethod:: mm
57+
.. automethod:: mul
58+
.. automethod:: mul_
59+
.. automethod:: nnz
60+
.. automethod:: resizeAs_
61+
.. automethod:: size
62+
.. automethod:: spadd
63+
.. automethod:: sparse_mask
64+
.. automethod:: spmm
65+
.. automethod:: sspaddmm
66+
.. automethod:: sspmm
67+
.. automethod:: sub
68+
.. automethod:: sub_
69+
.. automethod:: t_
70+
.. automethod:: toDense
71+
.. automethod:: transpose
72+
.. automethod:: transpose_
73+
.. automethod:: values
74+
.. automethod:: zero_

0 commit comments

Comments
 (0)