학습에서 레이어를 아주 깊게 쌓은 경우, 다음과 같이 Vanishing Gradient 문제가 나타날 수 있습니다.
이것은 Sigmoid 함수의 특성으로 일어나는 문제입니다. Sigmoid는 항상 1 이하의 값을 가지고 있으므로, 다음과 같이 레이어들을 쌓으면서 값이 계속해서 작아지게 됩니다.
1 2 3 4 5 6 | layer1 = tf.sigmoid(tf.matmul(X, W1) + b1) layer2 = tf.sigmoid(tf.matmul(layer1, W2) + b2) layer3 = tf.sigmoid(tf.matmul(layer2, W3) + b3) layer4 = tf.sigmoid(tf.matmul(layer3, W4) + b4) layer5 = tf.sigmoid(tf.matmul(layer4, W5) + b5) ... | cs |
따라서 이 문제를 해결하기 위해서 Sigmoid 함수 대신 ReLU 함수를 사용합니다.
Sigmoid 함수는 도출된 값을 항상 0과 1 사이의 값으로 조정하는데 비해, ReLU 함수는 도출된 값이 0 이하일 경우 0을, 그 이상일 경우 도출된 값 그대로 사용합니다.
따라서 ReLU 함수는 레이어가 경과함에 따라 값이 소멸하는 Vanishing Gradient 문제에 자유롭습니다. 이 함수는 다음과 같이 사용합니다.
1 2 3 4 5 6 7 | layer1 = tf.nn.relu(tf.matmul(X, W1) + b1) layer2 = tf.nn.relu(tf.matmul(layer1, W2) + b2) layer3 = tf.nn.relu(tf.matmul(layer2, W3) + b3) layer4 = tf.nn.relu(tf.matmul(layer3, W4) + b4) layer5 = tf.nn.relu(tf.matmul(layer4, W5) + b5) layer6 = tf.sigmoid(tf.matmul(layer5, W6) + b6) | cs |
레이어 1 ~ 5는 모두 relu 함수를 사용하고, 마지막 레이어6은 sigmoid 함수를 사용합니다. 마지막 출력은 0과 1 사이의 값이어야 하기 때문입니다.
SOURCE CODE
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import tensorflow as tf import numpy as np x_data = [[0, 0], [0, 1], [1, 0], [1, 1]] y_data = [[0], [1], [1], [0]] x_data = np.array(x_data, dtype=np.float32) y_data = np.array(y_data, dtype=np.float32) X = tf.placeholder(tf.float32, [None, 2]) Y = tf.placeholder(tf.float32, [None, 1]) W1 = tf.Variable(tf.random_normal([2, 10]), name='weight1') b1 = tf.Variable(tf.random_normal([10]), name='bias1') layer1 = tf.nn.relu(tf.matmul(X, W1) + b1) W2 = tf.Variable(tf.random_normal([10, 10]), name='weight2') b2 = tf.Variable(tf.random_normal([10]), name='bias2') layer2 = tf.nn.relu(tf.matmul(layer1, W2) + b2) W3 = tf.Variable(tf.random_normal([10, 10]), name='weight3') b3 = tf.Variable(tf.random_normal([10]), name='bias3') layer3 = tf.nn.relu(tf.matmul(layer2, W3) + b3) W4 = tf.Variable(tf.random_normal([10, 10]), name='weight4') b4 = tf.Variable(tf.random_normal([10]), name='bias4') layer4 = tf.nn.relu(tf.matmul(layer3, W4) + b4) W5 = tf.Variable(tf.random_normal([10, 1]), name='weight5') b5 = tf.Variable(tf.random_normal([1]), name='bias5') hypothesis = tf.sigmoid(tf.matmul(layer4, W5) + b5) cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis)) train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost) # Accuracy computation # True if hypothesis > 0.5 else False predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) # Launch graph with tf.Session() as sess: # Initialize TensorFlow variables sess.run(tf.global_variables_initializer()) for step in range(10001): sess.run(train, feed_dict={X: x_data, Y: y_data}) if step % 100 == 0: print(step, sess.run(cost, feed_dict={ X: x_data, Y: y_data}), sess.run([W1, W2, W3, W4, W5])) # Accuracy report h, c, a = sess.run([hypothesis, predicted, accuracy], feed_dict={X: x_data, Y: y_data}) print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a) | cs |
ReLu 함수는 tanh, Leakey ReLu, Maxout, ELU 함수 등으로 대체할 수 있습니다. 이 함수들에 대해서는 여기를 참고하세요.
'파이썬 > 머신러닝' 카테고리의 다른 글
[#E13] DROP OUT 이란? (0) | 2018.12.10 |
---|---|
[#E12] W 변수 (Weights) 초기화 방법 (0) | 2018.12.10 |
[#E10] 딥러닝의 기초 XOR 문제란? (0) | 2018.12.08 |
[#E9] 학습 데이터 전처리 (PreProcessing) (0) | 2018.12.08 |
[#E8] Fancy Softmax Regression이란? (0) | 2018.12.05 |