我的 CNN 模型遇到错误
The size of tensor a (4) must match the size of tensor b (512) at non-singleton dimension 1
并尝试将我的输入尺寸减少到合适的尺寸。我怎样才能将
torch.Size([512, 4, 1000])
这个 3d 矩阵减少到 2d torch.Size([512, 1])
?
将
[512, 4, 1000]
减少为[512, 1]
会导致大量数据丢失,因为维度2
和3 (4 \* 1000 = 4000)
的乘积无法在不丢失数据的情况下压缩为1
。
只要你知道自己在做什么,尽管你可以做这样的事情:
input_tensor = torch.randn(512, 4, 1000) # your tensor
output_tensor = input_tensor.mean(dim=[1,2], keepdim=True)
print(output_tensor.shape) # should output torch.Size([512, 1])
但是如果您想保留数据,最好这样做:
input_tensor = torch.randn(512, 4, 1000) # your tensor
output_tensor = input_tensor.view(512, -1)
print(output_tensor.shape) # should output torch.Size([512, 4000])
注意:您可能已经知道这一点,但以防万一;
-1
充当 PyTorch 将填充的占位符,以保持张量中元素总数的正确