我正在玩Pytorch库,并尝试使用Transfer Learning。
我的代码如下:
# get the model with pre-trained weights
resnet18 = models.resnet18(pretrained=True)
# freeze all the layers
for param in resnet18.parameters():
param.requires_grad = False
# print and check what the last FC layer is:
# Linear(in_features=512, out_features=1000, bias=True)
print(resnet18)
# set the final FC layer to what we require for our problem
resnet18.fc = nn.Linear(512, 10)
# unfreeze the last layer so it learns on our dataset
for param in resnet18.fc.parameters():
param.requires_grad = True
我将优化程序设置为:
# set optimizer
lr = 1e-2
optimizer = torch.optim.SGD(resnet18.parameters(), lr=lr, momentum=0.5)
在CIFAR10上训练该模型会使我的训练准确性非常差,仅为44%。
我在这里正确地进行转学吗?我期望会有更好的结果。如果有更多经验的人能好心地解释他们将如何处理这个问题,将不胜感激。
我在这里遵循了教程:
https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html