![](/img/trans.png)
[英]Tensorflow: how to use RNN initial state in an estimator with different batch size for training and testing?
[英]How to use variable batch size for bidirectional RNN in tensorflow
似乎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)
如何使用不同的批次大小进行培训和测试?
双向代码可用于可变的批量大小。 例如,看一下下面的测试代码 ,它创建一个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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.