pytorch--报错 AttributeError: ‘Net’ object has no attribute ‘copy’

pytorch–报错 AttributeError: ‘Net’ object has no attribute ‘copy’
报错信息:AttributeError: ‘Net’ object has no attribute ‘copy’

分析:报错是发生在加载预训练模型时,很可能时两种pytorch加载预训练模型方式弄混了。

解决:按照下面思路加载预训练模型就好。

1.保存加载state_dict方式(推荐)

保存:torch.save(model.state_dict(), PATH) # 推荐的文件后缀名是pt或pth

加载:model = TheModelClass(*args, **kwargs)

model.load_state_dict(torch.load(PATH))

2.保存加载整个模型

保存:torch.save(model, PATH)

加载:model = torch.load(PATH)

猜你喜欢

转载自blog.csdn.net/universe_R/article/details/124397642