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:
_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._
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.
fixed docs in https://github.com/torch/torch7/commit/e7f6444be8693163b37a38ed49e2e815d35e9984
thanks for the feedback.
Most helpful comment
fixed docs in https://github.com/torch/torch7/commit/e7f6444be8693163b37a38ed49e2e815d35e9984
thanks for the feedback.