Search code examples
luaneural-networktorchtensor

How to effectively apply linear transform on 3-dimensional input vector in Torch?


Imagine we have a DoubleTensor - size: 5x32x3000 and we ant to convert it to DoubleTensor - size: 5x32x100 to feed in further. Now, what I would do is the following:

local seq = nn.Sequential()
seq:add(nn.SplitTable(1))
seq:add(nn.MapTable():add(nn.Linear(3000,100)))
seq:add(nn.JoinTable(1)):add(nn.View(5,32,100))

This looks a bit complicated, I feel like there should be a more efficient way. Can you come up with a better solution?


Solution

  • I have tried this, it will output size (5, 32, 1000) as you wanted

    data = torch.Tensor(5, 32, 3000)
    mul = torch.Tensor(3000, 1000)
    res = torch.mm(data:view(5*32, 3000), mul):view(5, 32, 1000)
    print(res:size())