Search code examples
syntaxluatorch

Double curly-brace syntax in Torch


I am trying to better understand this syntax in Lua when using Torch:

local ten = torch.Tensor{{1,2,3},{4,5,6},{7,8,9}}

for i=1,(#ten)[2] do
    print(ten[i][{{2}}])
end

Specifically the line

    print(ten[i][{{2}}])

When I run the code, it prints

 2
[torch.DoubleTensor of size 1]

 5
[torch.DoubleTensor of size 1]

 8
[torch.DoubleTensor of size 1]

I am familiar with table literals and their basic syntax, but what is the purpose of wrapping the '2' in double curly-braces, and how does it work in the engine?


Solution

  • The following answer was posted on GitHub when I asked the same question: https://github.com/torch/torch7/issues/501#issuecomment-171290546


    Have a look at this part of the documentation.

    • When you have single curly-braces, you are creating a selection of the tensor. So ten[{1}] is equivalent to ten[1], which is in turn equivalent to ten:select(1,1). If you have several indices like ten[{1,2}], this is also equivalent to the slower ten[1][2] (because the latter returns 2 times a tensor, whereas the former only returns it once). If you select on a 1D tensor, your output is a number.
    • When you have double curly-braces, it returns a narrow of the tensor, and a narrowed tensor is always a tensor (even if it only has one element). With double curly-braces, you can specify a range in which the tensor will be narrowed, which is not possible with single curly-braces. For example, you can do ten[{{1,2},1}], which will be a 1D tensor of dimension 2, and if your do ten[{{1,2},{2}}] it will return a 2D tensor of size 2x1.

    For more information, have a look at select and narrow. One last note, the doc for select is not precisely correct, as it's possible to do a select on a 1D tensor, and the output is a number.