简体   繁体   中英

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM