第一种(推荐)只保存和加载模型参数:
# 存
torch.save(the_model.state_dict(), PATH)
# 取
the_model = TheModelClass(*args, **kwargs)
the_model.load_state_dict(torch.load(PATH)
第二种保存和加载整个模型:
# 存
torch.save(the_model, PATH)
# 取
the_model = torch.load(PATH)
第二种情况下,序列化的数据被绑定到特定的类和固定的目录结构,所以当在其他项目中使用时,或者在一些严重的重构器之后它可能会以各种方式break。