Pytorch固定某些层的操作
1. model = models.resnet18(pretrained=False,num_classes=CIFAR10_num_classes) def my_forward(model, x): mo = nn.Sequential(*list(model.children())[:-1]) feature = mo(x) feature = feature.view(x.size(0), -1) output= model.fc(feature) return feature, output
2. 在模型的forward函数的全连接层前面加上self.feature = x, 即可.