{"action":"created","issue":{"url":"https://api.github.com/repos/PaddlePaddle/Paddle/issues/1104","repository_url":"https://api.github.com/repos/PaddlePaddle/Paddle","labels_url":"https://api.github.com/repos/PaddlePaddle/Paddle/issues/1104/labels{/name}","comments_url":"https://api.github.com/repos/PaddlePaddle/Paddle/issues/1104/comments","events_url":"https://api.github.com/repos/PaddlePaddle/Paddle/issues/1104/events","html_url":"https://github.com/PaddlePaddle/Paddle/issues/1104","id":199721041,"number":1104,"title":"修改seqToseq的目标函数(cost function)","user":{"login":"coollip","id":18412946,"avatar_url":"https://avatars.githubusercontent.com/u/18412946?v=3","gravatar_id":"","url":"https://api.github.com/users/coollip","html_url":"https://github.com/coollip","followers_url":"https://api.github.com/users/coollip/followers","following_url":"https://api.github.com/users/coollip/following{/other_user}","gists_url":"https://api.github.com/users/coollip/gists{/gist_id}","starred_url":"https://api.github.com/users/coollip/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coollip/subscriptions","organizations_url":"https://api.github.com/users/coollip/orgs","repos_url":"https://api.github.com/users/coollip/repos","events_url":"https://api.github.com/users/coollip/events{/privacy}","received_events_url":"https://api.github.com/users/coollip/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":{"login":"lcy-seso","id":5842774,"avatar_url":"https://avatars.githubusercontent.com/u/5842774?v=3","gravatar_id":"","url":"https://api.github.com/users/lcy-seso","html_url":"https://github.com/lcy-seso","followers_url":"https://api.github.com/users/lcy-seso/followers","following_url":"https://api.github.com/users/lcy-seso/following{/other_user}","gists_url":"https://api.github.com/users/lcy-seso/gists{/gist_id}","starred_url":"https://api.github.com/users/lcy-seso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lcy-seso/subscriptions","organizations_url":"https://api.github.com/users/lcy-seso/orgs","repos_url":"https://api.github.com/users/lcy-seso/repos","events_url":"https://api.github.com/users/lcy-seso/events{/privacy}","received_events_url":"https://api.github.com/users/lcy-seso/received_events","type":"User","site_admin":false},"assignees":[{"login":"lcy-seso","id":5842774,"avatar_url":"https://avatars.githubusercontent.com/u/5842774?v=3","gravatar_id":"","url":"https://api.github.com/users/lcy-seso","html_url":"https://github.com/lcy-seso","followers_url":"https://api.github.com/users/lcy-seso/followers","following_url":"https://api.github.com/users/lcy-seso/following{/other_user}","gists_url":"https://api.github.com/users/lcy-seso/gists{/gist_id}","starred_url":"https://api.github.com/users/lcy-seso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lcy-seso/subscriptions","organizations_url":"https://api.github.com/users/lcy-seso/orgs","repos_url":"https://api.github.com/users/lcy-seso/repos","events_url":"https://api.github.com/users/lcy-seso/events{/privacy}","received_events_url":"https://api.github.com/users/lcy-seso/received_events","type":"User","site_admin":false}],"milestone":null,"comments":3,"created_at":"2017-01-10T03:00:14Z","updated_at":"2017-01-10T07:08:21Z","closed_at":null,"body":"Hi，我之前试用过demo中的seqToseq示例，成功训练了机器翻译nmt模型，工作正常。\r\n\r\n最近看了一篇将align信息引入到cost function中的论文：\r\nWenhu Chen, “guided alignment training for topic-aware neural machine translation”.\r\n\r\n这篇论文的思路其实比较简单：\r\nseqToseq中的attention可以看做对齐信息，但这个对齐没有fast align的强对齐效果好，作者希望模型在训练过程中能够参考fast align的强对齐结果，对attention进行调整。基于这个思路，作者先在线下用fast align对语料进行了对齐，然后定义了fast align结果和网络attention之间的cost，将其加入到cross entropy的cost中，即：\r\n![image](https://cloud.githubusercontent.com/assets/18412946/21791214/46c43b38-d71c-11e6-944e-46d78519891f.png)\r\n![image](https://cloud.githubusercontent.com/assets/18412946/21791222/57bdd976-d71c-11e6-95ef-7f2ce29b6ad4.png)\r\n![image](https://cloud.githubusercontent.com/assets/18412946/21791224/5b9ae2b4-d71c-11e6-9bb7-cc8889c95318.png)\r\n其中：\r\nHD就是目前seqToseq demo中使用的cross entropy\r\nG是作者定义的align cost。\r\n可以看到，最后cost function是HD和G两者的加权和。w1、w2控制了两者的比例。\r\n\r\nG作为align cost，其实就是mean squared error。G中的A是二维矩阵，即fast align的对齐结果，Ati是target sentence第t个token和src sentence第i个token的对齐情况，若fast align的结果认为这两者是对齐的，则Ati被置为1(实际计算时会对A进行归一化，使得每一行的和是1)。\r\nalpha则是网络中的attention。\r\n\r\n\r\n\r\n\r\n\r\n**OK，上面说完了背景，现在说说我怎么在paddle中尝试实现这一方案。**\r\n1. 首先用fast align对语料做了对齐，为每个sentence pair生成了对应的A，A的行数为该pair中target sentence的token数，A的列数为src sentence的token数。根据fast align的结果，将A中相应的位置置为1，最后再对每一行进行了归一化。\r\n\r\n2. 完成1后，就要将A作为训练信息通过dataprovider传入到paddle中。\r\n由于后面要计算A和attention的align cost，所以我先看了下demo中的attention，其代码在simple_attention中：\r\n`    attention_weight = fc_layer(input=m,\r\n                                size=1,\r\n                                act=SequenceSoftmaxActivation(),\r\n                                param_attr=softmax_param_attr,\r\n                                name=\"%s_softmax\" % name,\r\n                                bias_attr=False) `\r\n\r\n我的理解是：simple_attention方法在解码端每个time step都会执行一次，在当前t时刻时，这里的attention_weight是一个序列，序列长度是当前sentence pair的src len，序列中每个元素是一个一维的浮点数，第i个元素表示当前解码时刻t的target token和src第i个token的attention值。\r\n\r\n相应地，我也将fast align的结果A设置为类似的格式，采用了\r\ndense_vector_sub_sequence(1)\r\n这种格式，假设训练样本sentence pair的src包含3个token，target包含2个token，则A的形式举例如下：\r\n[ \r\n[ [0.5],[0.5],[0]  ],   //target第1个token和src每个token的对齐结果\r\n[ [0],[0.5],[0.5]  ],   //target第2个token和src每个token的对齐结果\r\n]\r\n\r\n我按照这种格式将A传进了paddle，具体如下：\r\n`        \r\n        a = data_layer(name='target_source_align', size=1)\r\n\r\n        decoder = recurrent_group(name=decoder_group_name,\r\n                                  step=gru_decoder_with_attention,\r\n                                  input=[\r\n                                      StaticInput(input=encoded_vector,\r\n                                                  is_seq=True),\r\n                                      StaticInput(input=encoded_proj,\r\n                                                  is_seq=True),\r\n                                      trg_embedding\r\n                                  ])\r\n`\r\n\r\n**现在我有几个问题：**\r\n1. 我上述的理解和处理流程是否正确？\r\n2. 如何将align_info这种sub_sequence传入到recurrent_group->gru_decoder_with_attention中？\r\n3. 如果attention_weight是长度为src len的序列，那么怎么与a计算上面式子中定义的align cost(即G)\r\n4. 如何将两个cost加权在一起进行训练？\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/PaddlePaddle/Paddle/issues/comments/271502349","html_url":"https://github.com/PaddlePaddle/Paddle/issues/1104#issuecomment-271502349","issue_url":"https://api.github.com/repos/PaddlePaddle/Paddle/issues/1104","id":271502349,"user":{"login":"lcy-seso","id":5842774,"avatar_url":"https://avatars.githubusercontent.com/u/5842774?v=3","gravatar_id":"","url":"https://api.github.com/users/lcy-seso","html_url":"https://github.com/lcy-seso","followers_url":"https://api.github.com/users/lcy-seso/followers","following_url":"https://api.github.com/users/lcy-seso/following{/other_user}","gists_url":"https://api.github.com/users/lcy-seso/gists{/gist_id}","starred_url":"https://api.github.com/users/lcy-seso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lcy-seso/subscriptions","organizations_url":"https://api.github.com/users/lcy-seso/orgs","repos_url":"https://api.github.com/users/lcy-seso/repos","events_url":"https://api.github.com/users/lcy-seso/events{/privacy}","received_events_url":"https://api.github.com/users/lcy-seso/received_events","type":"User","site_admin":false},"created_at":"2017-01-10T07:08:21Z","updated_at":"2017-01-10T07:08:21Z","body":"@coollip 你好~ 对流程的理解，大致是没有问题的。\r\n\r\n先说结论，你需要的模型如果不修改Paddle的 C++ 代码，无法通过配置直接配出来。\r\n\r\n下面是关于上面提到的 4 个问题。\r\n\r\n1. 正确的做法是把 attention weight 拿到 recurrent_layer_group 外面，然后和通过data_provider 给进去的fast_align 得到的“正确的”对齐信息，通过 cost layer （paddle 有 MSE 这个cost layer）G 这一部分 error。但是 attention weight 目前无法拿到 recurrent_layer_group 外面。下面会详细说明。\r\n\r\n2. 在你的这个例子里面，align info 是无法传进 recurrent_layer_group 里面的。\r\n\r\n3. 有了2 的答案，3 这种做法就行不通了。\r\n\r\n4. 接 2  个 cost paddle 是可以直接支持的，直接定义 2 个cost 就可以，cost layer 的接口中，可以指定权值。"}}
{"actor":{"display_login":"lcy-seso"}}