Obtaining the shape (4, 1, 84, 84) with pytorch

jga*_*uth 2 python pytorch

Suppose I have four pytorch tensors (tensor1, tensor2, tensor3, tensor4). Each tensor is of shape (1, 1, 84, 84). The first dimension is the number of tensors, the second dimension is the number of colors (e.g. grayscale in our example) and the last two dimensions represent the height and the width of the image.

I want to stack them so that I get the shape (4, 1, 84, 84).

我试过了torch.stack((tensor1, tensor2, tensor3, tensor4), dim=0),但我得到了一个形状(4, 1, 1, 84, 84)

我怎样才能堆叠这些张量,以便形状是 (4, 1, 84, 84)

ccl*_*ccl 5

您可以使用连接函数:

a = torch.ones(1,1,84,84)
b = torch.ones(1,1,84,84)
c = torch.cat((a,b), 0) # size[2,1,84,84]
Run Code Online (Sandbox Code Playgroud)