【发布时间】:2017-12-22 08:05:23
【问题描述】:
我最近发现 LayerNormBasicLSTMCell 是 LSTM 的一个版本,实现了 Layer Normalization 和 dropout。因此,我将使用 LSTMCell 的原始代码替换为 LayerNormBasicLSTMCell。这种变化不仅将测试准确率从 ~96% 降低到 ~92%,而且训练时间更长(~33 小时)(原始训练时间约为 6 小时)。所有参数都相同:epochs 数(10),堆叠层数(3),隐藏向量大小数(250),dropout keep prob(0.5),...硬件也是一样的。
我的问题是:我在这里做错了什么?
我的原始模型(使用 LSTMCell):
# Batch normalization of the raw input
tf_b_VCCs_AMs_BN1 = tf.layers.batch_normalization(
tf_b_VCCs_AMs, # the input vector, size [#batches, #time_steps, 2]
axis=-1, # axis that should be normalized
training=Flg_training, # Flg_training = True during training, and False during test
trainable=True,
name="Inputs_BN"
)
# Bidirectional dynamic stacked LSTM
##### The part I changed in the new model (start) #####
dropcells = []
for iiLyr in range(3):
cell_iiLyr = tf.nn.rnn_cell.LSTMCell(num_units=250, state_is_tuple=True)
dropcells.append(tf.nn.rnn_cell.DropoutWrapper(cell=cell_iiLyr, output_keep_prob=0.5))
##### The part I changed in the new model (end) #####
MultiLyr_cell = tf.nn.rnn_cell.MultiRNNCell(cells=dropcells, state_is_tuple=True)
outputs, states = tf.nn.bidirectional_dynamic_rnn(
cell_fw=MultiLyr_cell,
cell_bw=MultiLyr_cell,
dtype=tf.float32,
sequence_length=tf_b_lens, # the actual lengths of the input sequences (tf_b_VCCs_AMs_BN1)
inputs=tf_b_VCCs_AMs_BN1,
scope = "BiLSTM"
)
我的新模型(使用 LayerNormBasicLSTMCell):
...
dropcells = []
for iiLyr in range(3):
cell_iiLyr = tf.contrib.rnn.LayerNormBasicLSTMCell(
num_units=250,
forget_bias=1.0,
activation=tf.tanh,
layer_norm=True,
norm_gain=1.0,
norm_shift=0.0,
dropout_keep_prob=0.5
)
dropcells.append(cell_iiLyr)
...
【问题讨论】:
-
一个想法:[stackoverflow.com/questions/43234667/… 可能是问题吗?
tf.layers.batch_normalization中的均值和方差似乎没有自动更新。我想知道tf.contrib.rnn.LayerNormBasicLSTMCell是否有同样的问题。 -
@FariborzGhavamian,我对两个归一化函数都使用了第二种方法(即
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)和with tf.control_dependencies(update_ops):...)。 -
关于训练时间:我在tensorflow网站上找到的:tensorflow.org/performance/performance_guide#common_fused_ops。您可以打开一个名为
fused的参数并获得 12%-30% 的加速。 -
@FariborzGhavamian,感谢您提供的指南链接。我将通读整个性能指南。它们看起来都非常有用。我会试试看(包括参数
fused)。 -
@FariborzGhavamian,fused参数与batch_normalization有关,与layer_normalization无关。
标签: tensorflow normalization lstm