似乎tensorflow不支持双向RNN的可变批量大小。在此示例中,sequence_length
与batch_size
绑定,这是一个Python整数:
_seq_len = tf.fill([batch_size], tf.constant(n_steps, dtype=tf.int64))
outputs, state1,state2 = rnn.bidirectional_rnn(rnn_fw_cell, rnn_bw_cell, input,
dtype="float",
sequence_length=_seq_len)
如何使用不同的批量大小进行培训和测试?
答案 0 :(得分:5)
双向代码适用于可变批量大小。例如,请查看this test code,其中会创建tf.placeholder(..., shape=(None, input_size))
(其中None
表示批量大小可以变化)。
您可以将代码段转换为使用可变批量大小并进行少量修改:
# Compute the batch size based on the shape of the (presumably fed-in) `input`
# tensor. (Assumes that `input = tf.placeholder(..., shape=[None, input_size])`.)
batch_size = tf.shape(input)[0]
_seq_len = tf.fill(tf.expand_dims(batch_size, 0),
tf.constant(n_steps, dtype=tf.int64))
outputs, state1, state2 = rnn.bidirectional_rnn(rnn_fw_cell, rnn_bw_cell, input,
dtype=tf.float32,
sequence_length=_seq_len)