[英]Convert 5D tensor to 4D tensor in PyTorch
In PyTorch I have a 5D tensor X
of dimensions B x 9 x C x H x W
.在 PyTorch 我有一个尺寸为
B x 9 x C x H x W
的 5D 张量X
I want to convert it into a 4D tensor Y
with dimensions B x 9C x H x W
such that concatenation happens channel wise.我想将其转换为尺寸为
B x 9C x H x W
的 4D 张量Y
,以便连接以通道方式进行。
To illustrate let,为了说明让,
a = X[1,0,:,:,:]
b = X[1,1,:,:,:]
c = X[1,2,:,:,:]
...
i = X[1,8,:,:,:]
Then in the tensor Y
, a to i
should be channel wise concatenated.然后在张量
Y
中, a to i
应该按通道连接。
You can easily broadcast to a new shape with torch.reshape
:您可以使用
torch.reshape
轻松广播到新形状:
b, n, c, h, w = X.shape
X = X.reshape(b, n*c, h, w)
Try:尝试:
import torch
x = torch.rand(3, 4, 3, 2, 6)
print(x.shape)
y=x.flatten(start_dim=1, end_dim=2)
print(y.shape)
torch.Size([3, 4, 3, 2, 6])
torch.Size([3, 12, 2, 6])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.