Udacity深度学习之Dropout
Dropout含义
droupout
在深度学习中用来防止过拟合,机器学习过拟合现象究竟是什么呢?具体可以可以周志华老师机器学习西瓜书(第二章)——模型评估与选择,同时解释为什么L1可以做特征选择,其系数为0(L2正则类似)来说。
用一张图来表示就是:Dropout
是一个降低过拟合的正则化技术。它在网络中暂时的丢弃一些单元(神经元),以及与它们的前后相连的所有节点。图 1
是 dropout
的工作示意图。TensorFlow
提供了一个 tf.nn.dropout()
函数,你可以用来实现 dropout
。
让我们来看一个 tf.nn.dropout()
的使用例子。
keep_prob = tf.placeholder(tf.float32) # probability to keep units
hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0])
hidden_layer = tf.nn.relu(hidden_layer)
hidden_layer = tf.nn.dropout(hidden_layer, keep_prob)
logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1])
上面的代码展示了如何在神经网络中应用 dropout
。
tf.nn.dropout()
函数有两个参数:
hidden_layer
:你要应用 dropout
的 tensor
keep_prob
:任何一个给定单元的留存率(没有被丢弃的单元)keep_prob
可以让你调整丢弃单元的数量。为了补偿被丢弃的单元,tf.nn.dropout()
把所有保留下来的单元(没有被丢弃的单元)* 1/keep_prob
在训练时,一个好的keep_prob
初始值是0.5
。
在测试时,把 keep_prob
值设为1.0
,这样保留所有的单元,最大化模型的能力。
练习
练习1
下面的代码,哪里出问题了?
语法没问题,但是测试准确率很低。
keep_prob = tf.placeholder(tf.float32) # probability to keep units
hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0])
hidden_layer = tf.nn.relu(hidden_layer)
hidden_layer = tf.nn.dropout(hidden_layer, keep_prob)
logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch_i in range(epochs):
for batch_i in range(batches):
sess.run(optimizer, feed_dict={
features: batch_features,
labels: batch_labels,
keep_prob: 0.5})
validation_accuracy = sess.run(accuracy, feed_dict={
features: test_features,
labels: test_labels,
keep_prob: 0.5})
原因:keep_prob
在验证测试准确率的时候应该设置成1。
练习2
# Quiz Solution
# Note: You can't run code in this tab
import tensorflow as tf
hidden_layer_weights = [
[0.1, 0.2, 0.4],
[0.4, 0.6, 0.6],
[0.5, 0.9, 0.1],
[0.8, 0.2, 0.8]]
out_weights = [
[0.1, 0.6],
[0.2, 0.1],
[0.7, 0.9]]
# Weights and biases
weights = [
tf.Variable(hidden_layer_weights),
tf.Variable(out_weights)]
biases = [
tf.Variable(tf.zeros(3)),
tf.Variable(tf.zeros(2))]
# Input
features = tf.Variable([[0.0, 2.0, 3.0, 4.0], [0.1, 0.2, 0.3, 0.4], [11.0, 12.0, 13.0, 14.0]])
# TODO: Create Model with Dropout
keep_prob = tf.placeholder(tf.float32)
hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0])
hidden_layer = tf.nn.relu(hidden_layer)
hidden_layer = tf.nn.dropout(hidden_layer, keep_prob)
logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1])
# TODO: Print logits from a session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(logits, feed_dict={keep_prob: 0.5}))
output:
[[ 1.1 6.6000004]
[ 0.9100001 1.016 ]
[33.74 43.38 ]]
Ref:
1、https://www.cs.toronto.edu/~hinton/absps/JMLRdropout.pdf