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, 即可.
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Pytorch固定某些层的操作 - Python技术站