Reputation: 111
I am new to Lua and I am trying to learn it. Recently I came across below line of code which I believe is extracting some value from the table.
local context = context_proto[{{1, batch_size}, {1, source_l*imgH}}]
I have not seen this particular approach to read the table before. I would highly appreciate if anyone can help me understand what exactly above code is doing.
Upvotes: 1
Views: 150
Reputation: 28994
The code you see here doesn't make too much sense in native Lua without further code. It is commonly used in Torch. I found your snippet in a torch related script online. So I guess that's a valid guess.
I'm not very experienced with Torch, but from what I see in the documentation this will give you a sub-tensor of context_proto. row 1-batchSize and col source_l * imgH.
I think it is called slicing and it is covered in the following demo/tutorial: https://github.com/torch/demos/blob/master/tensors/slicing.lua
print 'more complex slicing can be done using the [{}] operator'
print 'this operator lets you specify one list/number per dimension'
print 'for example, t2 is a 2-dimensional tensor, therefore'
print 'we should pass 2 lists/numbers to the [{}] operator:'
print ''
t2_slice1 = t2[{ {},2 }]
t2_slice2 = t2[{ 2,{} }] -- equivalent to t2[2]
t2_slice3 = t2[{ {2},{} }]
t2_slice4 = t2[{ {1,3},{3,4} }]
t2_slice5 = t2[{ {3},{4} }]
t2_slice6 = t2[{ 3,4 }]
...
Please refer to the torch documentation for more details.
Upvotes: 1
Reputation: 1505
From the Lua Documentation:
The type table implements associative arrays, that is, arrays that can be indexed not only with numbers, but with any Lua value except nil and NaN.
That code is using a table as an index into another table. It might be clearer if it were written as follows:
local contextIndex = {{1, batch_size}, {1, source_l*imgH}}
local context = context_proto[contextIndex]
Upvotes: 1