Pytorch运行时错误:mat1和mat2形状不能相乘

fhity93d  于 2023-03-12  发布在  其他
关注(0)|答案(1)|浏览(442)

我正在Pytorch上构建CNN,并收到以下错误消息:
运行时错误:mat1和mat2形状不能相乘(32x32768和512x256)
我构建了以下模型:

def classifier_block(input, output, kernel_size, stride, last_layer=False):
  if not last_layer:
    x = nn.Sequential(
        nn.Conv2d(input, output, kernel_size, stride, padding=3),
        nn.BatchNorm2d(output),
        nn.LeakyReLU(0.2, inplace=True)
    )
  else:
    x = nn.Sequential(
        nn.Conv2d(input, output, kernel_size, stride),
        nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
    )
  return x

class Classifier(nn.Module):
  def __init__(self, input_dim, output):
    super(Classifier, self).__init__()
    self.classifier = nn.Sequential(
        classifier_block(input_dim, 64, 7, 2),
        classifier_block(64, 64, 3, 2),
        classifier_block(64, 128, 3, 2),
        classifier_block(128, 256, 3, 2),
        classifier_block(256, 512, 3, 2, True)
    )
    print('CLF: ',self.classifier)
    
    self.linear = nn.Sequential(
        nn.Linear(512, 256),
        nn.ReLU(inplace=True),
        nn.Linear(256, 128),
        nn.ReLU(inplace=True),
        nn.Linear(128, 64),
        nn.ReLU(inplace=True),
        nn.Linear(64, output)
    )
    print('Linear: ', self.linear)
  
  def forward(self, image):
    print('IMG: ', image.shape)
    x = self.classifier(image)
    print('X: ', x.shape)
    return self.linear(x.view(len(x), -1))

输入图像的大小为512x512。下面是我的训练块:

loss_train = []
loss_val = []

for epoch in range(epochs):
  print('Epoch: {}/{}'.format(epoch, epochs))
  total_train = 0
  correct_train = 0
  cumloss_train = 0
  classifier.train()
  for batch, (x, y) in enumerate(train_loader):
    x = x.to(device)
    print(x.shape)
    print(y.shape)
    output = classifier(x)
    loss = criterion(output, y.to(device))
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    print('Loss: {}'.format(loss))

任何建议都将不胜感激。

yzxexxkh

yzxexxkh1#

您需要在分类器部分匹配层的形状。
修复此部分:

self.linear = nn.Sequential(
    nn.Linear(512, 256),
    nn.ReLU(inplace=True),
    nn.Linear(256, 128),
    nn.ReLU(inplace=True),
    nn.Linear(128, 64),
    nn.ReLU(inplace=True),
    nn.Linear(64, output)
)

对此:

self.linear = nn.Sequential(
    nn.Linear(32768, 256),
    nn.ReLU(inplace=True),
    nn.Linear(256, 128),
    nn.ReLU(inplace=True),
    nn.Linear(128, 64),
    nn.ReLU(inplace=True),
    nn.Linear(64, output)
)

相关问题