I got a strange behaviour regarding an equal check for the weights for the vgg16 machine learning model
loading two times the model
import torch
from torch import nn
from torchvision.models import vgg16
import numpy as np
import torchvision.models as models
model = models.vgg16(weights='IMAGENET1K_V1')
torch.save(model.state_dict(), 'vgg16_model.pth')
vgg = vgg16(pretrained=True)
vgg.load_state_dict(torch.load("vgg16_model.pth", map_location='cpu'), strict=True)
params1 = np.array([param.detach().numpy() for param in vgg.parameters()], dtype=object)
vgg2 = vgg16(pretrained=True)
vgg2.load_state_dict(torch.load("vgg16_model.pth", map_location='cpu'), strict=True)
params2 = np.array([param.detach().numpy() for param in vgg2.parameters()], dtype=object)
note that I didn't replace any layer, if I do the check with np.assert_equals
np.array_equal(params1, params2)
I got False
But if I check the nested arrays iteratively the arrays are equals:
for val1, val2 in zip(params1, params2):
print(np.array_equal(val1, val2))
What am I missing? Is it due to the way of how I create the array at the start, as dtype=object
?
python version 3.9.13
numpy version 1.21.5