Issue
I just want fine tuning ResNet18 on cifar10 datasets. so I just want to change the last linear layer from 1000 to 10.
I tried use children
function to get the previous layers
ResModel = resnet18(weights=ResNet18_Weights)
model = nn.Sequential(
*list(ResModel.children())[:-1],
nn.Linear(512,10)
)
so it raised error
RuntimeError: mat1 and mat2 shapes cannot be multiplied (32768x1 and 512x10)
and then I tried this way ResModel.fc=nn.Linear(512,10)
it works fine.
so why?
Solution
The difference between stacking all layers into a single nn.Sequential
and overriding only the last layer is the forward
function:
Your ResModel
is of type torchvision.models.ResNet
, while your model
is a simple nn.Sequential
. The forward
pass of ResNet
has an additional flatten
operation before the last linear layer -- you do not have this operation in your nn.Sequential
model
.
Answered By - Shai
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.