我一直在关注官方 PyTorch 文档(http://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html)中的蚂蚁和蜜蜂迁移学习教程。我试图通过更改最后一层来预测两个类之一来微调 VGG19 模型。我可以使用以下代码修改最后一个 fc 层。
但是在执行 train_model 函数时出现错误。错误是“/opt/conda/conda-bld/pytorch_1513368888240/work/torch/lib/THC/generic/THCTensorMathBlas.cu:243 的大小不匹配”。知道问题是什么吗?
model_conv = torchvision.models.vgg19(pretrained=True)
for param in model_conv.parameters():
param.requires_grad = False
model_conv = nn.Sequential(*list(model_conv.classifier.children())[:-1] +
[nn.Linear(in_features=4096, out_features=2)])
if use_gpu:
model_conv = model_conv.cuda()
criterion = nn.CrossEntropyLoss()
optimizer_conv = optim.SGD(model_conv._modules['6'].parameters(), lr=0.001, momentum=0.9)
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_conv, step_size=7, gamma=0.1)
model_conv = train_model(model_conv, criterion, optimizer_conv, exp_lr_scheduler, num_epochs=25)