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

模型转换为pytorch的ckpt并加载之后,同样的id 进行embedding的结果不同 #75

Closed
randomtutu opened this issue Apr 3, 2019 · 21 comments
Labels
wontfix This will not be worked on

Comments

@randomtutu
Copy link

我在自己的数据集上,取得了比较大的提升,因此想要继续修改模型。

因为paddle文档比较少,因此想要把转换成pytorch。

我使用 #37 上的脚本,将paddle的ckpt转换为dict,再通过简单的名字对应和tensor的转换,将对应权重加载到pytorch中。

但是加载后做测试,结果相差很多。

为了找到不同的地方,我将两个框架的模型跑同一数据的前向传播,将每一步的数据进行对比。
结果发现,两个框架上同样的embedding权重(weight),对相同id的embedding居然不同。
我暂时没有对比两个框架的实现方法有何异同。

我推测要么是两个框架的embedding实现方法有差别,要么就是这个脚本保存的权重出现了一点问题。

希望有在做同样的事情的同学和我联系,一起交流。

@randomtutu
Copy link
Author

问题已经解决,主要是模型的LaynerNorm的eps不一样导致的。

一楼的问题主要是因为在从scope 里面传出数据的时候没有加 persistable = True。

@Mansterteddy
Copy link

@randomtutu 请问可以share一下你的pytorch转化脚本么?另外,ERNIE和BERT的vocab.txt是不一样的,请问你是怎么解决这个问题的?

@huntzhan
Copy link

@randomtutu 请教一下,你用的是 https://github.com/huggingface/pytorch-pretrained-BERT 的实现吗?方便 share 一下你的脚本吗?:)

@Mansterteddy
Copy link

已成功转成pytorch版本,发现效果不错,可以参考huggingface将tensorflow转为pytorch的脚本。

@randomtutu
Copy link
Author

@randomtutu 请教一下,你用的是 https://github.com/huggingface/pytorch-pretrained-BERT 的实现吗?方便 share 一下你的脚本吗?:)

参考楼下的思路,脚本很简单哈,我这里因为是内网,就暂时不发了

@randomtutu
Copy link
Author

已成功转成pytorch版本,发现效果不错,可以参考huggingface将tensorflow转为pytorch的脚本。

你好,希望留一个联系方式交流一下,我还在沿着这个模型做更多的工作。微信563056419

@arlenzhu
Copy link

问题已经解决,主要是模型的LaynerNorm的eps不一样导致的。

一楼的问题主要是因为在从scope 里面传出数据的时候没有加 persistable = True。

你好请问persistable = True是加在哪里?对paddlepaddle框架不是很熟悉

@wq343580510
Copy link

问题已经解决,主要是模型的LaynerNorm的eps不一样导致的。

一楼的问题主要是因为在从scope 里面传出数据的时候没有加 persistable = True。

看了下eps都是1e-5啊

@huntzhan
Copy link

问题已经解决,主要是模型的LaynerNorm的eps不一样导致的。

一楼的问题主要是因为在从scope 里面传出数据的时候没有加 persistable = True。

Hi @randomtutu , 请教一下你是在哪里设置 persistable=True 的?

我观察到 ernie_encoder.py 有设置 persistable=True 的,不知道是否与你的做法相关

https://github.com/PaddlePaddle/LARK/blob/b9dae026c25602b96adf7ee776ff9f894c912338/ERNIE/ernie_encoder.py#L78-L81

@huntzhan
Copy link

问题已经解决,主要是模型的LaynerNorm的eps不一样导致的。
一楼的问题主要是因为在从scope 里面传出数据的时候没有加 persistable = True。

看了下eps都是1e-5啊

pytorch-pretrained-BERT 里是 1e-12 😂

https://github.com/huggingface/pytorch-pretrained-BERT/blob/d76a57b0ba198eee27b3777f57fcabb6aba8b965/pytorch_pretrained_bert/modeling.py#L231-L234

@huntzhan
Copy link

感谢 @randomtutu,按照他的发现把所有 LayerNorm 的 epsilon 设成 1e-5 了,然后可以保证五位小数精度与 PaddlePaddle 的结果一致

@wq343580510
Copy link

感谢 @randomtutu,按照他的发现把所有 LayerNorm 的 epsilon 设成 1e-5 了,然后可以保证五位小数精度与 PaddlePaddle 的结果一致

所以请问persistable需要加吗?

@fyubang
Copy link

fyubang commented Apr 27, 2019

请问pytorch的persistable在哪加啊?

@fyubang
Copy link

fyubang commented Apr 27, 2019

感谢 @randomtutu,按照他的发现把所有 LayerNorm 的 epsilon 设成 1e-5 了,然后可以保证五位小数精度与 PaddlePaddle 的结果一致

请问一下如果设置成1e-12训练的话,效果会差多少?

@huntzhan
Copy link

@wq343580510 @fyubang
@randomtutu 交流后,确认 persistable 不需要加
转成 torch 之后可以复现 paper 里的结果

@fyubang
Copy link

fyubang commented Apr 27, 2019

@wq343580510 @fyubang
@randomtutu 交流后,确认 persistable 不需要加
转成 torch 之后可以复现 paper 里的结果

你好,能加一下我微信交流一下吗?zhaofubang0014 。最近有个项目,试了bert和ernie,ernie的表现却一直没有比bert好。

@wq343580510
Copy link

遇到一个奇怪的问题,用paddle代码finetune的模型确实比bert好,而且转成pytorch后预估打分一致,但是将作者提供的预训练模型转成pytorch版后,再进行微调结果不理想。试了下应该不是最后一层分类的初始化方式不同导致的,具体原因还在查(一个是先finetune再转,一个是先将pretrain model转格式,再在pytorch代码finetune) 有老哥遇到类似的问题吗?

@fyubang
Copy link

fyubang commented Apr 28, 2019

遇到一个奇怪的问题,用paddle代码finetune的模型确实比bert好,而且转成pytorch后预估打分一致,但是将作者提供的预训练模型转成pytorch版后,再进行微调结果不理想。试了下应该不是最后一层分类的初始化方式不同导致的,具体原因还在查(一个是先finetune再转,一个是先将pretrain model转格式,再在pytorch代码finetune) 有老哥遇到类似的问题吗?

我是直接转格式再在torch上fine tune的,一直没有得到特别好的效果。(因为数据流和最终的metrics比较麻烦,所以一直没在paddlepaddle上fine tune过)。你两种情况结果差多少个点啊?

@wq343580510
Copy link

遇到一个奇怪的问题,用paddle代码finetune的模型确实比bert好,而且转成pytorch后预估打分一致,但是将作者提供的预训练模型转成pytorch版后,再进行微调结果不理想。试了下应该不是最后一层分类的初始化方式不同导致的,具体原因还在查(一个是先finetune再转,一个是先将pretrain model转格式,再在pytorch代码finetune) 有老哥遇到类似的问题吗?

我是直接转格式再在torch上fine tune的,一直没有得到特别好的效果。(因为数据流和最终的metrics比较麻烦,所以一直没在paddlepaddle上fine tune过)。你两种情况结果差多少个点啊?

ernie能在我的任务上提高一个点左右的auc(相对于bert),但我先转完pytorch后finetune还有点问题在排查,建议你先用paddle的代码跑通看看效果

@huntzhan
Copy link

遇到一个奇怪的问题,用paddle代码finetune的模型确实比bert好,而且转成pytorch后预估打分一致,但是将作者提供的预训练模型转成pytorch版后,再进行微调结果不理想。试了下应该不是最后一层分类的初始化方式不同导致的,具体原因还在查(一个是先finetune再转,一个是先将pretrain model转格式,再在pytorch代码finetune) 有老哥遇到类似的问题吗?

我转成 pytorch 之后在 LCQMC 上 finetune 可以得到与论文匹配到结果,用的是 https://github.com/PaddlePaddle/LARK/blob/develop/ERNIE/script/run_lcqmc.sh 里的参数

@stale
Copy link

stale bot commented May 21, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Feel free to reopen it. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label May 21, 2020
@stale stale bot closed this as completed May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

6 participants