gpt4 book ai didi

python - PyTorch 3 reshape 错误

转载 作者:行者123 更新时间:2023-12-01 09:30:52 25 4
gpt4 key购买 nike

在 Python 中使用 PyTorch 训练 CNN 时,出现以下错误:

RuntimeError: invalid argument 2: size '[-3 x 3136]' is invalid for input with 160000 elements at /opt/conda/conda-bld/pytorch-cpu_1515613813020/work/torch/lib/TH/THStorage.c:41

这与下面模型中的 x.view 行有关:

class Net(nn.Module):

def __init__(self):
super(Net,self).__init__()
self.conv1 = nn.Conv2d(3,32,5,padding=2) # 1 input, 32 out, filter size = 5x5, 2 block outer padding
self.conv2 = nn.Conv2d(32,64,5,padding=2) # 32 input, 64 out, filter size = 5x5, 2 block padding
self.fc1 = nn.Linear(64*7*7,1024) # Fully connected layer
self.fc2 = nn.Linear(1024,2) #Fully connected layer 2 out.

def forward(self,x):
x = F.max_pool2d(F.relu(self.conv1(x)), 2) # Max pool over convolution with 2x2 pooling
x = F.max_pool2d(F.relu(self.conv2(x)), 2) # Max pool over convolution with 2x2 pooling
x = x.view(-1,64*7*7) # tensor.view() reshapes the tensor
x = F.relu(self.fc1(x)) # Activation function after passing through fully connected layer
x = F.dropout(x, training=True) #Dropout regularisation
x = self.fc2(x) # Pass through final fully connected layer
return F.log_softmax(x) # Give results using softmax

model = Net()
print(model)

我不确定这是否是由于图像具有 3 个 channel 或完全是其他原因造成的。我知道这个命令应该将图像 reshape 为单维数组,为完全连接层做好准备,因此当错误声明输入 160000 个元素时,我不确定如何解决此问题。

最佳答案

我假设您的输入图像的大小可能是200x200px(我所说的大小是指高度x宽度,而不是取图像的数量 channel 考虑)。

虽然您的 nn.Conv2d 层被定义为输出相同大小的张量(conv1 为 32 个 channel ,con2 为 64 个 channel ) ),F.max_pool2d 的定义方式是将高度和宽度除以 2。

因此,经过 2 次最大池操作后,张量的大小为 200/(2 * 2) x 200/(2 * 2) = 50x50px 。利用 conv2 的 64 个 channel ,您将获得 64 * 50 * 50 = 160000 个元素。

现在,您需要调整 view() ,以便将形状 (batch_size, 64, 50, 50) 的输入转换为 (batch_size , 64 * 50 * 50) (保留元素数量)。您需要类似地调整第一个全连接层。

import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np

class Net(nn.Module):

def __init__(self):
super(Net,self).__init__()
self.conv1 = nn.Conv2d(3,32,5,padding=2) # 1 input, 32 out, filter size = 5x5, 2 block outer padding
self.conv2 = nn.Conv2d(32,64,5,padding=2) # 32 input, 64 out, filter size = 5x5, 2 block padding
self.fc1 = nn.Linear(64*50*50,1024) # Fully connected layer
self.fc2 = nn.Linear(1024,2) #Fully connected layer 10 out.

def forward(self,x):
x = F.max_pool2d(F.relu(self.conv1(x)), 2) # Max pool over convolution with 2x2 pooling
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2) # Max pool over convolution with 2x2 pooling
x = x.view(-1,64*50*50) # tensor.view() reshapes the tensor
x = F.relu(self.fc1(x)) # Activation function after passing through fully connected layer
x = F.dropout(x, training=True) #Dropout regularisation
x = self.fc2(x) # Pass through final fully connected layer
return F.log_softmax(x) # Give results using softmax

model = Net()
print(model)

x = np.ones((1, 3, 200, 200))
x = torch.tensor(x)
x = model.forward(x)
print(x)

关于python - PyTorch 3 reshape 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49980801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com