LSTM 输出维度错误
Created by: Akeepers
paddle API文档中关于fluid.layers.lstm的说明有误:
- 输出的维度信息和文档不符:rnn_out:LSTM hidden的输出结果的Tensor,数据类型与input一致,维度为 [seq_len,batch_size,hidden_size],运行示例代码,得到的是[batch_size, seq_len, hidden_size]
- 代码示例有问题:layt_c.shape 应该为last_c.shape
以下是根据API文档中的示例所写的测试代码
import paddle.fluid.layers as layers
emb_dim = 256
vocab_size = 10000
data = fluid.layers.data(name='x', shape=[-1, 100, 1],
dtype='int64')
emb = fluid.layers.embedding(input=data, size=[vocab_size, emb_dim], is_sparse=True)
batch_size = 20
max_len = 100
dropout_prob = 0.2
seq_len = 100
hidden_size = 150
num_layers = 1
init_h = layers.fill_constant([num_layers, batch_size, hidden_size], 'float32', 0.0)
init_c = layers.fill_constant([num_layers, batch_size, hidden_size], 'float32', 0.0)
print emb.shape # (-1L, 100L, 256L)
rnn_out, last_h, last_c = layers.lstm(emb, init_h, init_c, max_len, hidden_size, num_layers, dropout_prob=dropout_prob)
print rnn_out.shape # (-1, 100, 150)
print last_h.shape # (1, 20, 150)
print last_c.shape # (1, 20, 150)```