@@ -768,6 +768,11 @@ In the same spirit, `index` might be a [LongStorage](storage.md),
768
768
specifying the position (in the Tensor) of the element to be
769
769
retrieved.
770
770
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
+
771
776
Example:
772
777
``` lua
773
778
> x = torch .Tensor (3 ,3 )
@@ -795,6 +800,12 @@ Example:
795
800
> = x [torch .LongStorage {2 ,3 }] -- yet another way to return row 2, column 3
796
801
6
797
802
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 ]
798
809
```
799
810
800
811
<a name =" torch.Tensor.set " />
@@ -1152,6 +1163,9 @@ The indexing operator [] can be used to combine narrow/sub and
1152
1163
select in a concise an efficient way. It can also be used
1153
1164
to copy, and fill (sub) tensors.
1154
1165
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
+
1155
1169
``` lua
1156
1170
> x = torch .Tensor (5 , 6 ):zero ()
1157
1171
> print (x )
@@ -1202,6 +1216,16 @@ to copy, and fill (sub) tensors.
1202
1216
0 4 0 - 1 0 0
1203
1217
0 5 0 - 1 0 0
1204
1218
[torch .DoubleTensor of dimension 5 x6]
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 5 x6]
1205
1229
```
1206
1230
1207
1231
<a name =" torch.Tensor.index " />
0 commit comments