【Pytorch】.pth权重文件解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangdongwei0/article/details/88956527

pytorch最后的权重文件是.pth格式的。

经常遇到的问题:

进行finutune时,改配置文件中的学习率,发现程序跑起来后竟然保持了以前的学习率, 并没有使用新的学习率。

原因:

首先查看.pth文件中的内容,我们发现它其实是一个字典格式的文件

其中保存了optimizer和scheduler,所以再次加载此文件时会使用之前的学习率。

我们只需要权重,也就是model部分,将其导出就可以了 

import torch

original = torch.load('path/to/your/checkpoint.pth')

new = {"model": original["model"]}
torch.save(new, 'path/to/new/checkpoint.pth')

猜你喜欢

转载自blog.csdn.net/wangdongwei0/article/details/88956527