tf.nn.bidirectional_dynamic_rnn输入sequence_length

时间:2018-05-09 04:10:50

标签: tensorflow

sequence_size = [4, 2, 3] ### batch_size:4  num_steps:2  embedding_size: 3
num_units = 2
dummy_sequences = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]],
                            [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]],
                            [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]],
                            [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]])
fw_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units)
bw_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units)

inputs = tf.placeholder(dtype=tf.float64, shape=sequence_size)
encoder_outputs, encoder_state = tf.nn.bidirectional_dynamic_rnn(cell_fw=fw_cell, cell_bw=bw_cell,
                                                                 inputs=inputs, sequence_length=sequence_size,
                                                                 dtype=tf.float64)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    output, state = sess.run([encoder_outputs, encoder_state], feed_dict={inputs: dummy_sequences})
    print(output, state)

我编写了一个示例来测试tensorflow中rnn的用法,我遇到了参数sequence_length的问题。如果我删除参数sequence_length,代码将正确运行。那么,设置sequence_length的正确方法是什么。这让我很困惑,因为我已经按sequence_lengthbatch_sizenum_steps的顺序设置了embedding_size。非常感谢您的回答。

错误如同:

ValueError: Dimension 0 in both shapes must be equal, but are 3 and 4 for 'bidirectional_rnn/fw/fw/while/Select' (op: 'Select') with input shapes: [3], [?,2], [4,2].

1 个答案:

答案 0 :(得分:0)

参数sequence_length需要在上面的示例中为每个样本处理的时间步数 - num_stepssequence_length必须是长度为batch_size的向量。

这不是输入的形状。假设您的输入与dummy_sequence [4,2,3]中的输入相同。您有4个样本,2个步长,每个样本由3个值表示。

因此你的sequence_length是[2,2,2,2]。如果所有样本长度相同,则可以省略此参数。否则,在达到该样本的最大时间步长后,网络将为每个样本的每个时间步输出零矢量输出。