Search code examples
luatorch

How does the syntax of this torch statement work?


I am trying to understand how this particular torch statement works.

I am referring to line number 115 at

https://github.com/torch/tutorials/blob/master/2_supervised/1_data.lua#L115

The line reads

trdata[{ {1,trainData.data:size(1)} }] = trainData.data

I was reading through slicing of data in torch and I understood that something like

t4[{ {},1 }] 

means you are referring to "all the rows and 1st column" of tensor t4.

However, in the statement I printed above, we have a {} inside another { } outside. What does that mean?

I do understand that

trainData.data:size(1)

refers to the batch size of the trainData which probably is the number of images.

Thanks


Solution

  • This acts as a narrow. Please refer to these detailed explanations:

    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.

    e.g.:

    th> trsize = 10
    th> trdata = torch.Tensor(trsize, 3, 32, 32)
    th> subdata = trdata[{ {1, 5} }]
    th> subdata:size()
      5
      3
     32
     32
    [torch.LongStorage of size 4]