Skip to content

Commit e8155b4

Browse files
author
Cédric Deltheil
committedJan 12, 2015
doc: mention ByteTensor for querying/filling elems
1 parent aa150a7 commit e8155b4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
 

‎doc/tensor.md

+24
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,11 @@ In the same spirit, `index` might be a [LongStorage](storage.md),
768768
specifying the position (in the Tensor) of the element to be
769769
retrieved.
770770

771+
If `index` is a `ByteTensor` in which each element is 0 or 1 then it acts as a
772+
selection mask used to extract a subset of the original tensor. This is
773+
particularly useful with [logical operators](maths.md#logical-operations-on-tensors)
774+
like [`torch.le`](maths.md#torchlea-b).
775+
771776
Example:
772777
```lua
773778
> x = torch.Tensor(3,3)
@@ -795,6 +800,12 @@ Example:
795800
> = x[torch.LongStorage{2,3}] -- yet another way to return row 2, column 3
796801
6
797802

803+
> = x[torch.le(x,3)] -- torch.le returns a ByteTensor that acts as a mask
804+
805+
1
806+
2
807+
3
808+
[torch.DoubleTensor of dimension 3]
798809
```
799810

800811
<a name="torch.Tensor.set"/>
@@ -1152,6 +1163,9 @@ The indexing operator [] can be used to combine narrow/sub and
11521163
select in a concise an efficient way. It can also be used
11531164
to copy, and fill (sub) tensors.
11541165

1166+
This operator also works with an input mask made of a `ByteTensor` with 0 and 1
1167+
elements, e.g with a [logical operator](maths.md#logical-operations-on-tensors).
1168+
11551169
```lua
11561170
> x = torch.Tensor(5, 6):zero()
11571171
> print(x)
@@ -1202,6 +1216,16 @@ to copy, and fill (sub) tensors.
12021216
0 4 0 -1 0 0
12031217
0 5 0 -1 0 0
12041218
[torch.DoubleTensor of dimension 5x6]
1219+
1220+
> x[torch.lt(x,0)] = -2 -- sets all negative elements to -2 via a mask
1221+
> print(x)
1222+
1223+
0 1 1 -2 0 0
1224+
0 2 2 -2 0 0
1225+
0 3 0 -2 0 0
1226+
0 4 0 -2 0 0
1227+
0 5 0 -2 0 0
1228+
[torch.DoubleTensor of dimension 5x6]
12051229
```
12061230

12071231
<a name="torch.Tensor.index"/>

0 commit comments

Comments
 (0)
Please sign in to comment.