Search code examples
luatorch

Multiply tensor lua


I'm new to lua,

Why the below code does not work?

local data = torch.Tensor(100, 4)
--data tensor is read from file
local w = torch.Tensor(1, 4):zero()
local tmp = w * data[5]:t()

Solution

  • data[5] is a 1-D tensor and transpose only works for 2-D tensors. So you can use the indexing operator as follow to obtain an extra dimension:

     -- matrix-matrix operation: result is a 1x1 tensor
     local tmp = w * data[{{5}}]:t()
    

    Or alternatively squeeze the first singleton dimension of w:

     -- dot prod between 1D tensors: result is a number
     local tmp = w:squeeze() * data[5]