一、保存和加载模型
保存模型有两种最基本的方式:
1、保存整个网络: torch.save(net, path1)
加载网络:model=torch.load(path1)
2、只保存网络参数:torch.save(net.state_dict(),path2)
加载网络参数:model.load_state_dict(torch.load(path2))
这种方式是官方推荐的方法,运行速度快,且占空间较小。需要注意的是 net.state_dict() 是将网络参数保存为字典形式(OrderedDict),load_state_dict() 加载的并不是网络参数的pth文件,而是字典。