11115
(李坤树)
July 14, 2017, 9:14am
1
Can anyone tell me what does the following code mean in the Transfer learning tutorial ?
model_ft = models.resnet18(pretrained=True)
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, 2)
I can see that this code is use to adjuest the last fully connected layer to the ‘ant’ and ‘bee’ poblem. But I can’t find anything in the pytorch documents about .fc.in_feature
, So what to do if I wants to transfer another pre-trained model, say .vgg19_bn
, to another classification problem?
alexis-jacq
(Alexis David Jacq)
July 14, 2017, 9:39am
2
in_feature is the number of inputs for your linear layer:
# constructor of nn.Lienar
def __init__(self, in_features, out_features, bias=True):
super(Linear, self).__init__()
self.in_features = in_features # num inputs
self.out_features = out_features # num outputs
self.weight = Parameter(torch.Tensor(out_features, in_features))
if bias:
self.bias = Parameter(torch.Tensor(out_features))
You can still check in the sources: https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/linear.py
5 Likes
11115
(李坤树)
July 14, 2017, 9:53am
3
Thank you very much! You helped a lot!
1 Like
model_ft is a type of <class ‘torchvision.models.resnet.ResNet’>.
model_ft.fc is a type of <class ‘torch.nn.modules.linear.Linear’>.