【问题标题】:How to use variable batch size for bidirectional RNN in tensorflow如何在张量流中为双向 RNN 使用可变批量大小
【发布时间】:2016-03-23 21:23:54
【问题描述】:

似乎 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)

如何使用不同的批量大小进行训练和测试?

【问题讨论】:

    标签: python tensorflow recurrent-neural-network


    【解决方案1】:

    双向代码适用于可变批量大小。例如,看一下this test code,它创建了一个tf.placeholder(..., shape=(None, input_size))(其中None 表示批量大小可以是可变的)。

    您可以通过少量修改将您的代码 sn-p 转换为使用可变批量大小:

    # 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 2016-03-28
      • 2018-09-29
      • 1970-01-01
      相关资源
      最近更新 更多