Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何save,load 训练好的EfficientGlobalPointerBert 模型及Tokenizer 。 #39

Closed
browserliu opened this issue Mar 26, 2022 · 10 comments
Labels
question Further information is requested

Comments

@browserliu
Copy link

你好:
在 gobalpoint_bert.ipynb 中的 EfficientGlobalPointerBert 和 gobalpoint_bert 如何save,load 模型及Tokenizer 如何存储和读取训练模型?
直接save EfficientGlobalPointerBert; model.module; 均出错。

@xiangking
Copy link
Owner

xiangking commented Mar 26, 2022

目前框架中使用的还是torch保存权重的方式

本质上ark-nlp中model.module还是torch模型,所以支持torch的保存方式,如下(PS:下面的module便是ipynb中的dl_module):

# 保存模型的参数
torch.save(model.module.state_dict(), 'net_params.pth')

# 加载模型参数
module = Net()
module.load_state_dict(torch.load(PATH))

同时也支持transformers的保存格式:

# 保存模型bin
module.save_pretrained("PATH") # 注意,这里的PATH只要到保存文件夹名就行

至于Tokenizer暂未单独提供保存方式,可以使用pickle等就行保存,同时也支持使用transformers的方式进行保存:

# 保存transformers tokenizer
tokenizer.vocab.save_pretrained("PATH") # 注意,这里的PATH只要到保存文件夹名就行

@xiangking xiangking added the question Further information is requested label Mar 26, 2022
@paulpaulzhang
Copy link

你好,我使用了这些保存方式,虽然成功保存,并且加载,但预测时似乎并没有调用加载的权重,f1在训练时eval很高,但单独加载模型eval时基本等于0,感觉权重没有被加载上

@xiangking
Copy link
Owner

xiangking commented Apr 1, 2022 via email

@paulpaulzhang
Copy link

解决了,就是因为cat2id顺序变了,现在sort后结果已经正常,感谢!

@xiangking
Copy link
Owner

好的,也感谢使用和提issue

@browserliu
Copy link
Author

收到,多谢。

@YangFW
Copy link

YangFW commented Aug 22, 2022

你好,我也遇到类似的问题,f1结果很好,但是保存模型参数重新加载后,再测试,结果很不行了,请问你具体是如何处理的呀

@paulpaulzhang
Copy link

paulpaulzhang commented Aug 22, 2022 via email

@YangFW
Copy link

YangFW commented Aug 22, 2022

你好,请问可以私加下你联系方式吗,我按照你这个方式排序后,没有效果,不知道是不是方式不对

@paulpaulzhang
Copy link

paulpaulzhang commented Aug 22, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants