python - 加载到新模型实例后,pytorch state_dict 的序列化更改
问题描述
为什么state_dict
在将 a 加载state_dict
到相同模型架构的新实例中后,从序列化 pytorch 获得的字节会发生变化?
看一看:
import binascii
import torch.nn as nn
import pickle
lin1 = nn.Linear(1, 1, bias=False)
lin1s = pickle.dumps(lin1.state_dict())
print("--- original model ---")
print(f"hash of state dict: {hex(binascii.crc32(lin1s))}")
print(f"weight: {lin1.state_dict()['weight'].item()}")
lin2 = nn.Linear(1, 1, bias=False)
lin2.load_state_dict(pickle.loads(lin1s))
lin2s = pickle.dumps(lin2.state_dict())
print("\n--- model from deserialized state dict ---")
print(f"hash of state dict: {hex(binascii.crc32(lin2s))}")
print(f"weight: {lin2.state_dict()['weight'].item()}")
印刷
--- original model ---
hash of state dict: 0x4806e6b6
weight: -0.30337071418762207
--- model from deserialized state dict ---
hash of state dict: 0xe2881422
weight: -0.30337071418762207
如您所见, (pickles of the) 的哈希值state_dict
不同,而权重被正确复制。我会假设state_dict
新模型中的 a 在各个方面都等于旧模型。看起来,它没有,因此不同的哈希值。
解决方案
这可能是因为 pickle 不会产生适合散列的 repr(请参阅Using pickle.dumps to hash mutable objects)。比较键可能是一个更好的主意,然后比较存储在 dict-keys 中的张量是否相等/接近。
下面是该想法的粗略实现。
def compare_state_dict(dict1, dict2):
# compare keys
for key in dict1:
if key not in dict2:
return False
for key in dict2:
if key not in dict1:
return False
for (k,v) in dict1.items():
if not torch.all(torch.isclose(v, dict2[k]))
return False
return True
但是,如果您仍然想对 state-dict 进行哈希处理并避免使用isclose
上面的比较,您可以使用下面的函数。
def dict_hash(dictionary):
for (k,v) in dictionary.items():
# it did not work without hashing the tensor
dictionary[k] = hash(v)
# dictionaries are not hashable and need to be converted to frozenset.
return hash(frozenset(sorted(dictionary.items(), key=lambda x: x[0])))
推荐阅读
- excel - 命名多个工作表
- openapi - 如何公开其他模型:从 Springfox 迁移到 Springdoc
- google-analytics - 自定义广告系列跟踪跨域谷歌分析
- python - session.query 返回不在数据库中的对象 - sqlalchemy
- vba - 使用活动工作表中的单元格选择将组合框填充到用户窗体中
- javascript - 通过小部件将 ISO-Date 替换为 HTML 文档中的普通日期
- javascript - 如何使用 onclientclick 调用多个 Javascript 函数
- python - 如何将 numpy 的混合元素类型附加到另一个数组?
- c# - Kubernetes 中的 Azure AD 身份验证无法取消对 message.State 的保护
- javascript - 如何通过反应导航访问 redux 商店?