1. 指數衰減學習率
tf.train.exponential_decay
先使用較大的學習率快速得到一個較優解,然後隨着迭代逐步減小學習率,使模型在訓練後期更加穩定。
global_step = tf.Variable(0,trainable=False)
learning_rate = tf.train.exponential_decay(learning_rate, global_step, decay_steps, decay_rate, staircase=False)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost,global_step = global_step)
2. 滑動平均
tf.train.ExponentialMovingAverage
滑動平均記錄了一段時間內模型中所有參數 和各自的平均值。利用滑動平均值可以增強模型的泛化能力。
其中,爲影子變量,爲待更新的變量,爲衰減率。決定了模型的更新速度,越大,模型越趨於穩定。實際應用中,一般設定爲接近1的數(如0.999或0.9999)。爲了使模型在訓練前期可以更新的更快,ExponentialMovingAverage
還提供了num_updates
參數設置的大小:
使用如下:
variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECCAY,global_step)
variable_averages_op = variable_averages.apply(tf.trainable_variables())
with tf.control_dependencies([optimizer,variable_averages_op]):
train_op= tf.no_op(name = 'train')
3. 正則化
Tensorflow提供了
tf.contrib.layers.l1_regularizer
tf.contrib.layers.l2_regularizer
來計算給定參數的L1/L2正則化項的值。
使用如下:
regularizer = tf.contrib.layers.l2_regularizer(REGULARIZER_RATE)
tf.add_to_collection('losses',regularizer(W1))
tf.add_to_collection('losses',regularizer(W2))
cem = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits = Y_, labels = Y))
cost = cem + tf.add_n(tf.get_collection('losses'))