* (tuple of Tensors tensors, name dim, Tensor out)

jgauth :

Suppose I have the memory list list_of_tensors = [tensor1, tensor2, tensor3, tensor4]. Each element is a pytorch tensor of shape (1, 1, 84, 84).

I want to concatenate that list of tensors to get a tensor of shape (4, 1, 84, 84). torch.cat(TT, dim=0) might surely allow me to do that. TT must be a tuple of tensor, so torch.cat(*list_of_tensors, dim=0) or torch.cat((*list_of_tensors), dim=0) won't work.

How can I use list_of_tensors and torch.cat(???, dim=0) to create a new tensor of shape (4, 1, 84, 84)

Anton Ganichev :

You can use stack, and remove surplus dimension with squeeze

c = (torch.stack(list_of_tensors,dim=1)).squeeze(0)

now c.shape is (4, 1, 84, 84)

You can find explanation here: https://discuss.pytorch.org/t/how-to-turn-a-list-of-tensor-to-tensor/8868/6

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=410616&siteId=1