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

使用文档平行语料训练第二步出错 #2

Closed
fansiawang opened this issue Dec 3, 2018 · 13 comments
Closed

使用文档平行语料训练第二步出错 #2

fansiawang opened this issue Dec 3, 2018 · 13 comments

Comments

@fansiawang
Copy link

fansiawang commented Dec 3, 2018

@Glaceon31
你好,按照readme的流程,我进行了如下操作:

  1. 利用句子级平行语料及文档级平行语料训练得到了基本的transformer模型;
  2. 使用文档级平行语料训练一个虚拟的context_transformer模型;
  3. 将第一步得到的基本transformer模型merge到第二步的虚拟模型中,以初始化context模型;
  4. 训练context模型。

其中2,3,4步我都只使用了文档级平行语料,词表和第1步的相同,没有改变。但是我在第四步时,出现了在读取features时找不到context的错误,具体的报错信息如下:

INFO:tensorflow:Restoring parameters from models/en2cn_t2t/sentence_doc/model.ckpt-95001
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:loss = 4.9753056, step = 95001
INFO:tensorflow:Saving checkpoints for 95002 into models/en2cn_t2t/sentence_doc/model.ckpt.
INFO:tensorflow:Saving checkpoints for 95002 into models/en2cn_t2t/sentence_doc/model.ckpt.
INFO:tensorflow:Validating model at step 95002
building context graph
use self attention
building encoder graph
building context graph
use self attention
building encoder graph
building context graph
use self attention
building encoder graph
building context graph
use self attention
building encoder graph
Traceback (most recent call last):
File "thumt/bin/trainer_ctx.py", line 431, in
main(parse_args())
File "thumt/bin/trainer_ctx.py", line 427, in main
sess.run(ops["train_op"])
File "/usr/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 546, in run
run_metadata=run_metadata)
File "/usr/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 1022, in run
run_metadata=run_metadata)
File "/usr/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 1113, in run
raise six.reraise(*original_exc_info)
File "/usr/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 1098, in run
return self._sess.run(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 1178, in run
run_metadata=run_metadata))
File "/disk1/wangfang/transformer/document-level/context-aware/thumt/Document-Transformer/thumt/utils/hooks.py", line 266, in after_run
self._session_config)
File "/disk1/wangfang/transformer/document-level/context-aware/thumt/Document-Transformer/thumt/utils/hooks.py", line 142, in _evaluate
predictions = eval_fn(placeholders)
File "thumt/bin/trainer_ctx.py", line 405, in
[model.get_inference_func()], f, params
File "/disk1/wangfang/transformer/document-level/context-aware/thumt/Document-Transformer/thumt/utils/inference.py", line 289, in create_inference_graph
states.append(model_fn0)
File "/disk1/wangfang/transformer/document-level/context-aware/thumt/Document-Transformer/thumt/models/contextual_transformer.py", line 500, in encoding_fn
context_output, encoder_output = encoding_graph(features, "infer", params)
File "/disk1/wangfang/transformer/document-level/context-aware/thumt/Document-Transformer/thumt/models/contextual_transformer.py", line 260, in encoding_graph
ctx_seq = features["context"]
KeyError: 'context'

为了方便作者可以定位我的错误,现我把具体的训练脚本也附上:

  • 第2步的虚拟训练脚本:

python thumt/bin/trainer_ctx.py
--input corpus/cn_en/trainData/doc_total.sub.en corpus/cn_en/trainData/doc_total.sub.cn
--context corpus/cn_en/trainData/doc_total.sub.en.ctx
--output models/en2cn_t2t/sentence_doc/dummy
--vocabulary corpus/cn_en/trainData/vocab.en corpus/cn_en/trainData/vocab.cn
--model contextual_transformer
--parameters train_steps=1

  • 第3步的combine脚本:

python thumt/scripts/combine_add.py
--model models/en2cn_t2t/sentence_doc/dummy
--part models/en2cn_t2t/sentence_only
--output models/en2cn_t2t/sentence_doc

  • 第4步的真正训练脚本:

python thumt/bin/trainer_ctx.py
--input corpus/cn_en/trainData/doc_total.sub.en corpus/cn_en/trainData/doc_total.sub.cn
--context corpus/cn_en/trainData/doc_total.sub.en.ctx
--output models/en2cn_t2t/sentence_doc
--vocabulary corpus/cn_en/trainData/vocab.en corpus/cn_en/trainData/vocab.cn
--model contextual_transformer
--parameters start_steps=95000,num_context_layers=1,batch_size=6250,device_list=[0,1,2,3],train_steps=1000000,save_checkpoint_steps=5000,keep_checkpoint_max=50,beam_size=5,eval_steps=5000

跪求大神可以看到我的留言,帮忙看一下是否我哪里的操作不当导致训练过程出错,万分感谢!

@Glaceon31
Copy link
Collaborator

可能是eval_steps的问题。我没有加上在训练期间每隔一定时间做一次evaluation的代码,把它去掉试试吧。

@fansiawang
Copy link
Author

fansiawang commented Dec 4, 2018

多谢,可能确实是我额外加了eval_steps的关系。
另外,我想请教一下,在只使用文档级平行语料进行第二步的训练时,loss的波动感觉比较大,并且与base相比,即使loss下降了,但是在dev上的bleu也下降了。(我的句子级平行语料约300w,文档级平行语料约170w(句对)。在第一阶段训练基本transformer模型时在5.5w次达到dev最优bleu,并将其作为context模型的基准模型继续训练。)

请问,你在得到论文里有提升效果的最优模型,在context模型训练这一步大概是训练了多少轮次呢?

@Glaceon31
Copy link
Collaborator

可以看一下学习率,如果是1的话可能太大了影响训练效果。通常来说应该loss会比较迅速地(几千轮内)降到比较低的值。我在200w句对上6w轮左右达到第一阶段dev最优,然后再训3w轮达到第二阶段dev最优

@fansiawang
Copy link
Author

我用的是代码里默认的值(看到最新的merge将原来默认学习率从1改到了0.1),感觉loss确实是在几千轮就开始从2.7左右降到了1.7左右,但是后面会反复震荡,而且并不是loss最小的时候dev的结果最优,这也是正常的吗?

@Glaceon31
Copy link
Collaborator

这个应该是正常的,和训练正常的NMT模型的情况差不多。

@fansiawang
Copy link
Author

我用了自己的数据,基本重现了论文的结论,加入额外的上下文信息可以比不加涨约1-1.5个bleu。大神厉害!另外,我额外发现了一个有意思的事情,在评测test数据时我不小心用的是dev的上下文,我发现正确的上下文和不正确的上下文似乎对最终的结果没什么影响(大约0.1-0.2个bleu),这个你觉得可以怎么理解呢?

@Glaceon31
Copy link
Collaborator

具体的原因我也说不准,只能猜测是模型学习到了一些特征,恰好dev和test上的这些特征比较相似。

@fansiawang
Copy link
Author

fansiawang commented Dec 21, 2018

你好,我看源码的时候发现一个和论文不太一致的地方,就是代码里是默认不使用context_gate的,使用的还是默认的残差连接,但论文里特意做了是否使用改进版残差连接的实验,证明还是使用好,那么为何在代码里又将其设置为false呢?这个是否和数据集相关,还是其实用不用差别不大,所以就默认不用了呢?(论文里对比实验是相差0.4个bleu)。另外,我发现代码里context_gate这部分的代码里注释掉了dropout,想和你请教一下缘由。

@Glaceon31
Copy link
Collaborator

感谢提醒,我忘记把context_gate设成默认true了,已修正。另注释掉dropout其实是无意的。

@fansiawang
Copy link
Author

好的,我还想确认一下细节,就是context_gate的实现细节里其实是调用了一个linear的层,这个层是否对应tf里的dense层呢?如果是的话,我用tf.layers.dense()替代的话,是否需要设置bias=False, activation=None呢?

@Glaceon31
Copy link
Collaborator

抱歉回复晚了,看了下dense的说明,应该是的。

@fansiawang
Copy link
Author

还想请教个问题,我在小规模数据(句子级300w,文档级170w)上测试该方法是有效的,与论文结论一致,可以增加1-2个点。但是如果句子级数据增加到2000w,在该模型基础上使用context框架学习额外的参数(还是使用那170w文档数据),结果bleu反而降了1-2个点,请问你在更大的数据集上验证过这个方法吗?

@Glaceon31
Copy link
Collaborator

在句子级数据和文档级数据有数量级或的差异,或者是两者的topic存在差异时,只使用文档级的来做第二步训练确实会比较不稳定。我们没有在文档翻译这个任务上试过很大的数据集,但在大规模带有上下文信息的词语翻译(即源端和目标端是有歧义的词语,context端是包含该词语的完整句子)上验证有效。我们也发现,在训练时将完整的句子也放进第二步训练时(context端的内容=源端)会增加训练的稳定性。对应你这个情况,可以尝试将2000w的句子级别数据也放进第二步训练以增加稳定性。

@abhranil08 abhranil08 mentioned this issue Mar 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants