Pytorch加载预训练模型前n层

import torch.nn as nn
import torchvision.models as models

class resnet(nn.Module):
def __init__(self):
super(resnet,self).__init__()
self.model = models.resnet18(pretrained=True)
self.encoder = nn.Sequential(*list(self.model.children())[:-1])
def forward(self,x):
y = self.encoder(x)
return y
net = resnet()

你可能感兴趣的:(Pytorch加载预训练模型前n层)