Reputation: 515
I am using torch.save() to save a model file. However, everytime I save it, it changes. Why so?
netG_1 = torch.load('netG.pth')
netG_2 = torch.load('netG.pth')
torch.save(netG_1, 'netG_1.pth')
torch.save(netG_2, 'netG_2.pth')
Using md5sum *.pth
:
779f0fefca47d17a0644033f9b65e594 netG_1.pth
476f502ec2d1186c349cdeba14983d09 netG_2.pth
b0ceec8ac886a11b79f73fc04f51c6f9 netG.pth
The model is an instance of this class:
https://github.com/taoxugit/AttnGAN/blob/master/code/model.py#L397
Upvotes: 2
Views: 779
Reputation: 22324
A class which does not define the __hash__
method will have its instances hashed according to their id
. As for CPython, this means everytime the instance is saved and reloaded, it changes hash since its position in memory changed.
Here is a proof of concept.
class Foo:
pass
instance = Foo()
print('hash:', hex(hash(instance)))
print('id: ', hex(id(instance)))
hash: 0x22f3f57608
id: 0x22f3f576080
The exact transformation is hash(o) == id(o) // 16
.
Upvotes: 5