栈式双向LSTM模型中的代码问题
阅读paddlepaddle book中第节情感分析栈式双向lstm网络模型代码中有以下疑问:
1. data = paddle.layer.data("word", paddle.data_type.integer_value_sequence(input_dim))
2. emb = paddle.layer.embedding(input=data, size=emb_dim)#5634*128
3. fc1 = paddle.layer.fc(input=emb, size=hid_dim, act=linear, bias_attr=bias_attr)#5634*512
4. lstm1 = paddle.layer.lstmemory(input=fc1, act=relu, bias_attr=bias_attr)#5634*128
5. fc_para_attr = paddle.attr.Param(learning_rate=1e-3)
6. lstm_para_attr = paddle.attr.Param(initial_std=0., learning_rate=1.)
7. para_attr = [fc_para_attr, lstm_para_attr]
8. inputs = [fc1, lstm1]
9. fc = paddle.layer.fc(input=inputs, size=hid_dim, act=linear, param_attr=para_attr, bias_attr=bias_attr)
其中第9行的前馈操作具体实现了什么,输入inputs包含两部分即 [fc1, lstm1],同时传入的参数para_attr也包含[fc_para_attr, lstm_para_attr],fc_para_attr, lstm_para_attr是否分别是fc1, lstm1层到fc层的连接权重矩阵?
是的,因为paddle中lstm的输入不包含序列数据的线性变换,所以在lstm之前一定要连接fc层
请参考这里 http://www.paddlepaddle.org/docs/develop/api/en/config/layer.html#lstmemory
10. inputs = [fc1, lstm1]
11. for i in range(2, stacked_num + 1):
12. fc = paddle.layer.fc(input=inputs, size=hid_dim, act=linear, param_attr=para_attr, bias_attr=bias_attr)#5634 * 512
13. lstm = paddle.layer.lstmemory(input=fc, reverse=(i % 2) == 0, act=relu, bias_attr=bias_attr)#5634 * 128
14. inputs = [fc, lstm]
15. fc_last = paddle.layer.pooling(input=inputs[0], pooling_type=paddle.pooling.Max())#21 * 512
16. lstm_last = paddle.layer.pooling(input=inputs[1], pooling_type=paddle.pooling.Max())#21 * 128
还有就是一个问题:
-
传给data的是batch size为21的文本列表,如[[文本1[1,4,7,3,...,145]],[文本2[1,5,6,7,13,...,787]]...],不同文本的长度不同。但infer查看emb的shape发现为5634*128,其中128是词向量的维度,但5634为21条样本中出现的所有词语数(非词表大小),直到最后fc_last层pooling时才出现样本数21,为什么传入的三个维度的batch,到emb层其shape显示为词语数*词嵌入维度?
-
15行shape为5634*128的fc经过pooling怎么得到了shape为21*128的fc_last?
fc = paddle.layer.fc(input=inputs, size=hid_dim, act=linear, param_attr=para_attr, bias_attr=bias_attr)
@kuke 嗯,lstmemory前先加一层前馈我知道。但是这行是lstm层之后的前馈层,他的input是一个列表[fc, lstm],同时参数列表para_attr也包含[fc_para_attr, lstm_para_attr]两部分,fc_para_attr, lstm_para_attr分别是fc1, lstm1层到fc层的连接权重矩阵吗?
@leyiwang 1.
为什么传入的三个维度的batch,到emb层其shape显示为词语数*词嵌入维度?
infer时一个batch的序列数据按第0维concat成了一个长序列,所以其维度变成了5634*128;
2.
15行shape为5634*128的fc经过pooling怎么得到了shape为21*128的fc_last?
注意这里的pooling方式是max pooling(一个序列的max pooling,得到的是一个时间步大小维度的向量,其每个元素都是从该序列不同时间步相同维度选出的一个最大值)
- fc_para_attr, lstm_para_attr 只是参数的属性,用来描述参数,例如参数名,学习率等,所以这里并非是权重矩阵。
15. fc = paddle.layer.fc(input=inputs, size=hid_dim, act=linear, param_attr=para_attr, bias_attr=bias_attr)
@kuke 非常感谢!最后再确认一下,infer操作本身会将batch中的元素按0维拉成长序列,事实上数据在网络模型中并未进行这种拉伸操作吗?paddlepaddle有木有比infer更直观的方法可以看到网络中层的shape信息。另,上述代码的前馈过程中,inputs的两部分[fc, lstm]和参数的两部分[fc_para_attr, lstm_para_attr]有什么联系,具体进行了怎样的操作?
您好,此issue在近一个月内暂无更新,我们将于今天内关闭。若在关闭后您仍需跟进提问,可重新开启此问题,我们将在24小时内回复您。因关闭带来的不便我们深表歉意,请您谅解~感谢您对PaddlePaddle的支持! Hello, this issue has not been updated in the past month. We will close it today for the sake of other user‘s experience. If you still need to follow up on this question after closing, please feel free to reopen it. In that case, we will get back to you within 24 hours. We apologize for the inconvenience caused by the closure and thank you so much for your support of PaddlePaddle Group!