티스토리 뷰

TensorFlow Dropout

 - Dropout은 over-fitting을 줄이기 위한 regularization 기술이다.

 - 네트워크에서 일시적으로 유닛(인공 뉴런, artificial neurons)을 배제하고, 그 배제된 유닛의 연결을 모두 끊는다.

 - 위의 그림은 dropout의 작동 원리를 설명하고 있다.

 - 텐서플로우에선 드롭아웃을 구현할 때 tf.nn.dropout() 함수를 사용한다.

 - tf.nn.dropout()을 사용한 예를 확인해보자.




1
2
3
4
5
6
7
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])
cs

 - tf.nn.dropout()함수는 두 인자를 받는데,

 - hidden_layer : dropout을 적용할 텐서

 - keep_prob : 주어진 유닛을 유지할 확률 즉, drop하지 않을 확률


 - keep_prob을 사용하면 drop할 유닛의 수를 조절할 수 있다.

 - drop된 유닛을 보상하기위해 tf.nn.dropout() 함수는 drop되지 않은 모든 유닛에 1 / keep_prob을 곱한다.

 - 학습을 진행할 땐 무난한 keep_prob 값은 0.5이다.

 - 테스트를 진행할 땐 keep_prob 값을 1.0으로 두어 모든 유닛을 유지하고 모델의 성능을 극대화한다.






Quiz 1

 - 아래의 코드를 보자. 문법적으로 틀린게 없으나 정확도(Accuracy)가 극도록 낮은것을 확인할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
...
 
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})
cs

 - 그 이유는 학습할때의 keep_prob : 0.5를 할당 / 테스트할때의 keep_prob : 1.0을 할당하여

 - 정확도를 극대화 하여야한다. 

 - 학습시에만 유닛들을 drop하는것이다.






Quiz 2

 - ReLU 그리고 dropout 레이어를 적용해보도록 하자.

 - ReLU 레이어와 dropout 레이어를 사용하여 모델을 만들자.

 - 이 때, dropout 레이어의 keep_prob placeholder는 0.5로 셋팅한다.

 - 그리고 모델의 logits을 출력한다.

 - 코드가 실행될 때 마다 output이 달라지는데 그 이유는 dropout한 유닛이 랜덤하게 변하기 때문이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Quiz Solution
# Note: You can't run code in this tab
import tensorflow as tf
 
hidden_layer_weights = [
    [0.10.20.4],
    [0.40.60.6],
    [0.50.90.1],
    [0.80.20.8]]
out_weights = [
    [0.10.6],
    [0.20.1],
    [0.70.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.02.03.04.0], [0.10.20.30.4], [11.012.013.014.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}))
cs

>>


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함