Skip to content

Commit ac15471

Browse files
ssnlzou3519
authored andcommitted
clarify when to use as_tuple in torch.nonzero (pytorch#32051)
Summary: Pull Request resolved: pytorch#31798 Differential Revision: D19272332 Pulled By: zou3519 fbshipit-source-id: 954d086a7b9f1a719e0dac303a4253bf7ec8e9f4
1 parent 49364eb commit ac15471

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

torch/_torch_docs.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3751,25 +3751,37 @@ def merge_dicts(*dicts):
37513751
r"""
37523752
nonzero(input, *, out=None, as_tuple=False) -> LongTensor or tuple of LongTensors
37533753
3754-
**When** :attr:`as_tuple` **is false or unspecified:**
3754+
.. note::
3755+
:func:`torch.nonzero(..., as_tuple=False) <torch.nonzero>` (default) returns a
3756+
2-D tensor where each row is the index for a nonzero value.
3757+
3758+
:func:`torch.nonzero(..., as_tuple=True) <torch.nonzero>` returns a tuple of 1-D
3759+
index tensors, allowing for advanced indexing, so ``x[x.nonzero(as_tuple=True)]``
3760+
gives all nonzero values of tensor ``x``. Of the returned tuple, each index tensor
3761+
contains nonzero indices for a certain dimension.
3762+
3763+
See below for more details on the two behaviors.
3764+
3765+
3766+
**When** :attr:`as_tuple` **is ``False`` (default)**:
37553767
37563768
Returns a tensor containing the indices of all non-zero elements of
37573769
:attr:`input`. Each row in the result contains the indices of a non-zero
37583770
element in :attr:`input`. The result is sorted lexicographically, with
37593771
the last index changing the fastest (C-style).
37603772
3761-
If :attr:`input` has `n` dimensions, then the resulting indices tensor
3773+
If :attr:`input` has :math:`n` dimensions, then the resulting indices tensor
37623774
:attr:`out` is of size :math:`(z \times n)`, where :math:`z` is the total number of
37633775
non-zero elements in the :attr:`input` tensor.
37643776
3765-
**When** :attr:`as_tuple` **is true:**
3777+
**When** :attr:`as_tuple` **is ``True``**:
37663778
37673779
Returns a tuple of 1-D tensors, one for each dimension in :attr:`input`,
37683780
each containing the indices (in that dimension) of all non-zero elements of
37693781
:attr:`input` .
37703782
3771-
If :attr:`input` has `n` dimensions, then the resulting tuple contains `n` tensors
3772-
of size `z`, where `z` is the total number of
3783+
If :attr:`input` has :math:`n` dimensions, then the resulting tuple contains :math:`n`
3784+
tensors of size :math:`z`, where :math:`z` is the total number of
37733785
non-zero elements in the :attr:`input` tensor.
37743786
37753787
As a special case, when :attr:`input` has zero dimensions and a nonzero scalar
@@ -3780,8 +3792,8 @@ def merge_dicts(*dicts):
37803792
out (LongTensor, optional): the output tensor containing indices
37813793
37823794
Returns:
3783-
LongTensor or tuple of LongTensor: If :attr:`as_tuple` is false, the output
3784-
tensor containing indices. If :attr:`as_tuple` is true, one 1-D tensor for
3795+
LongTensor or tuple of LongTensor: If :attr:`as_tuple` is ``False``, the output
3796+
tensor containing indices. If :attr:`as_tuple` is ``True``, one 1-D tensor for
37853797
each dimension, containing the indices of each nonzero element along that
37863798
dimension.
37873799

0 commit comments

Comments
 (0)