我在 cifar 10 数据集上使用 pytorch 测试了一个相对简单的模型。
但是我遇到了这个奇怪的错误,我无法理解为什么:
x_shape: torch.Size([64, 64, 8, 8])
[...]
x_shape: torch.Size([16, 64, 8, 8])
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-6-74e3c9f1b35c> in <cell line: 53>()
62 image, label = image.to(device), label.to(device)
63 optimizer.zero_grad()
---> 64 pred = model(image)
65 loss = criterion(pred, label)
66 total_train_loss += loss.item()
6 frames
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
603 self.groups,
604 )
--> 605 return F.conv3d(
606 input, weight, bias, self.stride, self.padding, self.dilation, self.groups
607 )
这里是我使用的模型,如果 stackoverflow 允许的话,我可以发送整个测试/数据初始化,但基本上我在检查之前将图像缩小了两两,看起来就像我收到错误的地方...
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(
in_channels = 3,
out_channels = 32,
kernel_size = 5,
stride = 1,
padding = 2
)
self.conv2 = nn.Conv2d(
in_channels=32,
out_channels=64,
kernel_size=5,
stride=1,
padding=2
)
self.conv3 = nn.Conv3d(
in_channels=64,
out_channels=64,
kernel_size=5,
stride=1,
padding=2
)
self.pool = nn.MaxPool2d(2,2)
self.fc1 = nn.Linear(1024, 0)
self.fc2 = nn.Linear(0, 1024)
self.fc3 = nn.Linear(1024, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
print('x_shape:',x.shape)
x = self.pool(F.relu(self.conv3(x)))
x = torch.flatten(x, 1) # flatten all dimensions except batch
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return (x)
我尝试更改模型上的值,但无济于事。
您需要将
self.conv3 = nn.Conv3d
更改为 self.conv3 = nn.Conv2d