Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

torch

lua

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?

like image 290
Kevin Bullaughey Avatar asked Jul 02 '15 13:07

Kevin Bullaughey


People also ask

How do you add two tensors together?

Two tensors of the same size can be added together by using the + operator or the add function to get an output tensor of the same shape.

How do you append to a Tensor?

How to append to a torch tensor? This is achieved by using the expand function which will return a new view of the tensor with its dimensions expanded to larger size. It is important to do because at some time if we have two tensors one is of smaller dimension and another is of larger one.


1 Answers

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)
like image 102
smhx Avatar answered Nov 18 '22 16:11

smhx