How can I set the bias of the classifier of Mobilenetv2 to 55? Can I use this method? sth like this:
model = torchvision.models.mobilenet_v2(pretrained=pretrained)
model.classifier = nn.Linear(model.last_channel, 1)
model.classifier.bias = nn.Parameter(55)
If you want to set all the zeros(default values of the bias, in case of a normal torchvision model, it is a tensor of len 1000 for 1000 classes of imagenet) to 55, then the best way to do it is:
In your case, you are changing the number of output channels to 1, so you can avoid the “len(model.classifier.bias)” part, however including that will just make it more dynamic.
Important thing to note here is the torch.FloatTensor(), because torch.nn.Parameter() requires a tensor, and since you want the gradient to be updated on the bias, as it learns, it should be a float tensor.
Thank you for your answer. I applied what you said but it is really strange, my output is still negative. do you have any suggestion? I think the bias didn’t have any effect on the output.
You are welcome.
For that, I will need much more details, like the task, the network etc.
It might be easier for you to debug it by looking at the output of each layer and where it is deviating from the expected.