Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 4 - Replacing the Classifier #35

Closed
ldkong1205 opened this issue Jun 12, 2020 · 2 comments
Closed

Chapter 4 - Replacing the Classifier #35

ldkong1205 opened this issue Jun 12, 2020 · 2 comments

Comments

@ldkong1205
Copy link

transfer_model.fc = nn.Sequential(nn.Linear(transfer_model.fc.in_features, 500),
nn.ReLU(),
nn.Dropout(),
nn.Linear(500,2))

AttributeError: 'Sequential' object has no attribute 'in_features'

@MarcusFra
Copy link
Contributor

Hmm, I cannot reproduce this error.
Did you run the https://github.com/falloutdurham/beginners-pytorch-deep-learning/blob/master/chapter4/Chapter%204.ipynb or did you copy the code from the book?
Maybe it's due to the defintion of transfer_model - did you run transfer_model = models.resnet50(pretrained=True) earlier?
After loading this pretrained model you can check the structure of the fc block by print(transfer_model.fc). It should return Linear(in_features=2048, out_features=1000, bias=True).

After redefining this with transfer_model.fc = nn.Sequential(nn.Linear(transfer_model.fc.in_features, 500), ...)) you should get

Sequential(
  (0): Linear(in_features=2048, out_features=500, bias=True)
  (1): ReLU()
  (2): Dropout(p=0.5, inplace=False)
  (3): Linear(in_features=500, out_features=2, bias=True)

when you run print(transfer_model.fc).

@ldkong1205
Copy link
Author

Hmm, I cannot reproduce this error.
Did you run the https://github.com/falloutdurham/beginners-pytorch-deep-learning/blob/master/chapter4/Chapter%204.ipynb or did you copy the code from the book?
Maybe it's due to the defintion of transfer_model - did you run transfer_model = models.resnet50(pretrained=True) earlier?
After loading this pretrained model you can check the structure of the fc block by print(transfer_model.fc). It should return Linear(in_features=2048, out_features=1000, bias=True).

After redefining this with transfer_model.fc = nn.Sequential(nn.Linear(transfer_model.fc.in_features, 500), ...)) you should get

Sequential(
  (0): Linear(in_features=2048, out_features=500, bias=True)
  (1): ReLU()
  (2): Dropout(p=0.5, inplace=False)
  (3): Linear(in_features=500, out_features=2, bias=True)

when you run print(transfer_model.fc).

Thank you for your reply. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants