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, 即可.

posted @ 2020-08-14 09:11  zonechen  阅读(713)  评论(0编辑  收藏  举报