pytorch 运行时错误:mat1和mat2形状不能相乘(114688x7和512x8)迁移学习

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

我使用的是一个预先训练的迁移学习模型,称为:来自torchvision的MaxVit-t。我还使用ISIC-2019数据集,该数据集包含8个类。我使用以下代码将输出为8的分类器图层更改为8

# Set the manual seeds
torch.manual_seed(42)
torch.cuda.manual_seed(42)

# Recreate the classifier layer and seed it to the target device
model.classifier = torch.nn.Sequential(
    torch.nn.Dropout(p=0.2, inplace=True), 
    torch.nn.Linear(in_features=512, 
                   out_features=8, # same number of output units as our number of classes
                    bias=True)).to(device)

然后,我使用以下代码来定义损失和优化器

# Define loss and optimizer
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

最后使用以下代码训练模型:

# Set the random seeds
torch.manual_seed(42)
torch.cuda.manual_seed(42)

# Start the timer
from timeit import default_timer as timer 
start_time = timer()

# Setup training and save the results
results = engine.train(model=model,
                       train_dataloader=train_dataloader,
                       test_dataloader=test_dataloader,
                       optimizer=optimizer,
                       loss_fn=loss_fn,
                       epochs=5,
                       device=device)

# End the timer and print out how long it took
end_time = timer()
print(f"[INFO] Total training time: {end_time-start_time:.3f} seconds")

这给了我以下错误:

RuntimeError                              Traceback (most recent call last)
<ipython-input-135-6189b11a087a> in <module>
      8 
      9 # Setup training and save the results
---> 10 results = engine.train(model=model,
     11                        train_dataloader=train_dataloader,
     12                        test_dataloader=test_dataloader,

7 frames
/usr/local/lib/python3.9/dist-packages/torch/nn/modules/linear.py in forward(self, input)
    112 
    113     def forward(self, input: Tensor) -> Tensor:
--> 114         return F.linear(input, self.weight, self.bias)
    115 
    116     def extra_repr(self) -> str:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (114688x7 and 512x8)

我试图找出我做错了什么,但不能找出,有人能帮助我吗?我用了下面的教程为我的代码:https://www.learnpytorch.io/06_pytorch_transfer_learning/#what-is-transfer-learning

bvjveswy

bvjveswy1#

错误信息很明显--嵌入层和fc层之间的形状不匹配,我认为这是因为在fc层之前缺少AdaptiveAvgPool(1)和flatten操作。
以下是MaxViT分类器的torchvision实现:

self.classifier = nn.Sequential(
            nn.AdaptiveAvgPool2d(1),
            nn.Flatten(),
            nn.LayerNorm(block_channels[-1]),
            nn.Linear(block_channels[-1], block_channels[-1]),
            nn.Tanh(),
            nn.Linear(block_channels[-1], num_classes, bias=False),
        )

参考:https://pytorch.org/vision/main/_modules/torchvision/models/maxvit.html#maxvit_t

相关问题