【问题标题】:How to implement Tensorflow batch normalization in LSTM如何在 LSTM 中实现 Tensorflow 批量归一化
【发布时间】:2018-04-05 12:51:53
【问题描述】:

我当前的 LSTM 网络是这样的。

rnn_cell = tf.contrib.rnn.BasicRNNCell(num_units=CELL_SIZE)
init_s = rnn_cell.zero_state(batch_size=1, dtype=tf.float32)  # very first hidden state
outputs, final_s = tf.nn.dynamic_rnn(
    rnn_cell,              # cell you have chosen
    tf_x,                  # input
    initial_state=init_s,  # the initial hidden state
    time_major=False,      # False: (batch, time step, input); True: (time step, batch, input)
)

# reshape 3D output to 2D for fully connected layer
outs2D = tf.reshape(outputs, [-1, CELL_SIZE])
net_outs2D = tf.layers.dense(outs2D, INPUT_SIZE)

# reshape back to 3D
outs = tf.reshape(net_outs2D, [-1, TIME_STEP, INPUT_SIZE])

通常,我将tf.layers.batch_normalization 应用为批量标准化。但我不确定这是否适用于 LSTM 网络。

b1 = tf.layers.batch_normalization(outputs, momentum=0.4, training=True)
d1 = tf.layers.dropout(b1, rate=0.4, training=True)

# reshape 3D output to 2D for fully connected layer
outs2D = tf.reshape(d1, [-1, CELL_SIZE])                       
net_outs2D = tf.layers.dense(outs2D, INPUT_SIZE)

# reshape back to 3D
outs = tf.reshape(net_outs2D, [-1, TIME_STEP, INPUT_SIZE])

【问题讨论】:

标签: python tensorflow neural-network lstm rnn


【解决方案1】:

基于paper“层标准化” - Jimmy Lei Ba、Jamie Ryan Kiros、Geoffrey E. Hinton

Tensorflow 现在带有 tf.contrib.rnn.LayerNormBasicLSTMCell 一个 LSTM 单元,具有层归一化和经常性 dropout。

查找文档here

【讨论】:

    【解决方案2】:

    如果您想对 RNN(LSTM 或 GRU)使用批量规范,您可以查看 this implementation,或阅读 blog post 的完整描述。

    然而,在序列数据中,层归一化比批量归一化更有优势。具体来说,“batch normalization 的效果取决于 mini-batch 的大小,如何将其应用于循环网络并不明显”(来自论文 Ba, et al. Layer normalization)。

    对于层归一化,它对每一层内的总和输入进行归一化。您可以查看 GRU 单元的层规范化implementation

    【讨论】:

      猜你喜欢
      • 2020-04-04
      • 2018-07-10
      • 1970-01-01
      • 2017-08-16
      • 2017-08-10
      • 2018-04-26
      • 2019-06-16
      • 2017-07-01
      • 1970-01-01
      相关资源
      最近更新 更多