gpt4 book ai didi

python - 为什么每次保存模型时哈希值都会改变?

转载 作者:行者123 更新时间:2023-12-02 03:29:53 27 4
gpt4 key购买 nike

我正在使用 torch.save() 来保存模型文件。然而,每次我保存它时,它都会改变。为什么会这样?

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')

使用md5sum *.pth:

779f0fefca47d17a0644033f9b65e594  netG_1.pth
476f502ec2d1186c349cdeba14983d09 netG_2.pth
b0ceec8ac886a11b79f73fc04f51c6f9 netG.pth

模型是此类的一个实例:

https://github.com/taoxugit/AttnGAN/blob/master/code/model.py#L397

最佳答案

未定义__hash__方法的类将根据其id对其实例进行哈希处理。对于 CPython,这意味着每次保存和重新加载实例时,它都会更改哈希,因为它在内存中的位置发生了变化。

这是一个概念证明。

class Foo:
pass

instance = Foo()

print('hash:', hex(hash(instance)))
print('id: ', hex(id(instance)))

输出

hash: 0x22f3f57608
id: 0x22f3f576080

确切的转换是hash(o) == id(o)//16

关于python - 为什么每次保存模型时哈希值都会改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52152144/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com