Search code examples
luatorch

What are the most idiomatic ways to combine tensors in torch?


I'm confronted with concatenating three tensors together so that 3 px1 tensors become one 3px1 tensor.

The most succinct I could come up with was:

torch.Tensor{v2:totable(),v4:totable(),v6:totable()}:view(3*p,1)

Are there ways to do this without converting to tables and back to tensors? It seems like there should be a generic way of concatenating tensors along some specified dimension assuming they have compatible shapes.

I can see how it would be possible to write such a function, does one not exist?


Solution

  • a = torch.randn(3,1)
    b = torch.randn(3,1)
    c = torch.randn(3,1)
    
    d = torch.cat(a,b,1):cat(c,1)
    
    print(d)