I finetuned the PyTorch vgg16 pretrained model with CIFAR-10 data.
Now, I load this model via load_state_dict()
and check the model weights using model.parameters()
. I noticed, that some of the weights are different each time I load the model.
How can that be? I'm not changing the model at all inbetween, so shouldn't the weights stay constant?
This is the code I'm trying it with:
import torch
from torch import nn
from torchvision.models import vgg16
import numpy as np
vgg = vgg16(pretrained=True)
vgg.classifier[6] = nn.Linear(in_features=4096, out_features=10)
vgg.load_state_dict(torch.load("vgg16_model.pth", map_location='cpu'), strict=False)
params1 = np.array([param.detach().numpy() for param in vgg.parameters()])
vgg2 = vgg16(pretrained=True)
vgg2.classifier[6] = nn.Linear(in_features=4096, out_features=10)
vgg2.load_state_dict(torch.load("vgg16_model.pth", map_location='cpu'), strict=False)
params2 = np.array([param.detach().numpy() for param in vgg2.parameters()])
print(np.array_equal(params1, params2))
Output:
False
Edit: I did check the model parameters before converting them to arrays and there are definitely some parameters which are different, regardless/before the conversion to arrays.