Python 如何修复:运行时错误:pyTorch中的大小不匹配
我是pyTorch新手,遇到以下大小不匹配错误:Python 如何修复:运行时错误:pyTorch中的大小不匹配,python,deep-learning,pytorch,Python,Deep Learning,Pytorch,我是pyTorch新手,遇到以下大小不匹配错误: RuntimeError: size mismatch, m1: [7 x 2092500], m2: [180 x 120] at ..\aten\src\TH/generic/THTensorMath.cpp:961 型号: class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(3, 200,
RuntimeError: size mismatch, m1: [7 x 2092500], m2: [180 x 120] at ..\aten\src\TH/generic/THTensorMath.cpp:961
型号:
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 200, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(200, 180, 5)
self.fc1 = nn.Linear(180, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84,5)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(x.shape[0], -1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
我是如何尝试将x=x.view(x.shape[0],-1)
更改为x=x.view(x.size(0),-1)
,但这也不起作用。图像尺寸为512x384。并使用了以下转换:
def load_dataset():
data_path = './dataset/training'
transform = transforms.Compose(
[transforms.Resize((512,384)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
train_dataset = torchvision.datasets.ImageFolder(root=data_path,transform=transform)
train_loader = torch.utils.data.DataLoader(train_dataset,batch_size=7,num_workers=0,shuffle=True)
return train_loader
问题是最后一个最大池层的输出维度与第一个完全连接层的输入维度不匹配。这是输入形状的最后一个最大池层之前的网络结构
(3,512,384)
:
表的最后一行表示MaxPool2d-4
输出宽度为125、高度为93的180个通道(滤波器输出)。因此,您需要第一个完全连接的层具有180*125*93=2092500
输入大小。这是很多,所以我建议您改进您的架构。在任何情况下,如果您将第一个完全连接的层的输入大小更改为2092500
,则它可以工作:
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 200, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(200, 180, 5)
#self.fc1 = nn.Linear(180, 120)
self.fc1 = nn.Linear(2092500, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84,5)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(x.shape[0], -1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
给出以下体系结构:
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 200, 508, 380] 15,200
MaxPool2d-2 [-1, 200, 254, 190] 0
Conv2d-3 [-1, 180, 250, 186] 900,180
MaxPool2d-4 [-1, 180, 125, 93] 0
Linear-5 [-1, 120] 251,100,120
Linear-6 [-1, 84] 10,164
Linear-7 [-1, 5] 425
================================================================
Total params: 252,026,089
Trainable params: 252,026,089
Non-trainable params: 0
(您可以使用包生成这些表。)
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 200, 508, 380] 15,200
MaxPool2d-2 [-1, 200, 254, 190] 0
Conv2d-3 [-1, 180, 250, 186] 900,180
MaxPool2d-4 [-1, 180, 125, 93] 0
Linear-5 [-1, 120] 251,100,120
Linear-6 [-1, 84] 10,164
Linear-7 [-1, 5] 425
================================================================
Total params: 252,026,089
Trainable params: 252,026,089
Non-trainable params: 0