Torch7: torch.index, torch.select, etc not behaving as documentation describes

Created on 21 Apr 2016  路  7Comments  路  Source: torch/torch7

Torch.index, torch.select, etc are described to have in-place behaviour such that no memory is allocated and the original tensor/storage is modified. However, this doesn't seem to be the case usually, e.g.

th> a = torch.rand(3,5)
                                                                      [0.0001s] 
th> a
 0.0385  0.9918  0.6313  0.1635  0.8619
 0.8188  0.7725  0.7073  0.4598  0.6053
 0.0385  0.9918  0.6313  0.1635  0.8619
[torch.DoubleTensor of size 3x5]

Operate on indexed tensor, new tensor is returned:

th> a:index(1, torch.LongTensor{1,3}):add(-1)
-0.9615 -0.0082 -0.3687 -0.8365 -0.1381
-0.1812 -0.2275 -0.2927 -0.5402 -0.3947
[torch.DoubleTensor of size 2x5]

Old tensor stays the same:

th> a
 0.0385  0.9918  0.6313  0.1635  0.8619
 0.2956  0.8971  0.1659  0.7127  0.0222
 0.8188  0.7725  0.7073  0.4598  0.6053
[torch.DoubleTensor of size 3x5]

If this is the case isn't it a good idea to modify the documentation? Or am I misinterpreting this:

Extracting sub-tensors

_Each of these methods returns a Tensor which is a sub-tensor of the given tensor, with the same Storage. Hence, any modification in the memory of the sub-tensor will have an impact on the primary tensor, and vice-versa._

Most helpful comment

All 7 comments

Take a close look at https://github.com/torch/torch7/blob/master/doc/tensor.md#tensor-indexdim-index

The returned Tensor does _not_ use the same storage as the original Tensor

torch.select does use same storage, but torch.index does not (and cannot in a general case). As @iamalbert pointed out, it's documented.

Hi guys thanks for the replies, I had only been reading https://github.com/torch/torch7/blob/master/doc/tensor.md .

It could be worth updated this as just now it does explicitly say that a call on torch.index, torch.select etc will modify the original storage (as opposed to for example torch.indexCopy).

Thanks again

@cjmcmurtrie in torch.index in tensor.md it says this:

The returned Tensor does not use the same storage as the original Tensor

Ok cool but in that case am I misinterpreting this statement? :

_Each of these methods returns a Tensor which is a sub-tensor of the given tensor, with the same Storage. Hence, any modification in the memory of the sub-tensor will have an impact on the primary tensor, and vice-versa._

oh i see. the opening statement for the section is a bit wrong then, i'll change that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chenbohua3 picture chenbohua3  路  4Comments

lucasarb picture lucasarb  路  3Comments

kaleem0002 picture kaleem0002  路  10Comments

n3011 picture n3011  路  9Comments

i55code picture i55code  路  4Comments