출처 : 모두를위한 머신러닝 (http://hunkim.github.io/ml/)
Tensor Flow 에서 사용하는 Learning rate 에 관하여 이론과 예제를 통해 설명한다.¶
Learning Rate in Gradient Descent Optimizer¶
Gradient descent optimizer
를 사용할 때 learning_rate
을 사용하였다. learning_rate
을 직관적으로 표현하면 다음 그림처럼 나타낼 수 있다.
- Learning rate 이 너무 클 경우, 정확한 minimum point 에 도달하지 못하거나 데이터가 튀어서 무한대로 나갈수가 있다.
- Learning rate 이 너무 작을경우, 변화가 너무 적어서 최대 step 에 도달해도 minimum point를 찾지 못할 수가 있다.
- 따라서 Cost function 에 따라 적당한 learning rate 이 다르므로, 테스트를 통해 적당한 값을 찾아내는것이 중요하다.
예제¶
Learning rate 을 테스트하기위해 기본적인 작업을 셋팅함
In [8]:
import tensorflow as tf
tf.set_random_seed(777) # for reproducibility
x_data = [[1, 2, 1],
[1, 3, 2],
[1, 3, 4],
[1, 5, 5],
[1, 7, 5],
[1, 2, 5],
[1, 6, 6],
[1, 7, 7]]
y_data = [[0, 0, 1],
[0, 0, 1],
[0, 0, 1],
[0, 1, 0],
[0, 1, 0],
[0, 1, 0],
[1, 0, 0],
[1, 0, 0]]
# Evaluation our model using this test dataset
x_test = [[2, 1, 1],
[3, 1, 2],
[3, 3, 4]]
y_test = [[0, 0, 1],
[0, 0, 1],
[0, 0, 1]]
X = tf.placeholder("float", [None, 3])
Y = tf.placeholder("float", [None, 3])
W = tf.Variable(tf.random_normal([3, 3]))
b = tf.Variable(tf.random_normal([3]))
# tf.nn.softmax computes softmax activations
# softmax = exp(logits) / reduce_sum(exp(logits), dim)
hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)
# Cross entropy cost/loss
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))
매우 큰 Learning rate¶
5번재 step 에서 무한대가 나오고, 6 번째 step 에서부터 nan 이 출력되었다. 따라서 Cost function 이 nan 이 출력되는 경우 learning rate 이 너무 커서 그럴 수 있다.
In [7]:
optimizer = tf.train.GradientDescentOptimizer(
learning_rate=1.5).minimize(cost)
# Correct prediction Test model
prediction = tf.arg_max(hypothesis, 1)
is_correct = tf.equal(prediction, tf.arg_max(Y, 1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
# Launch graph
with tf.Session() as sess:
# Initialize TensorFlow variables
sess.run(tf.global_variables_initializer())
for step in range(201):
cost_val, W_val, _ = sess.run(
[cost, W, optimizer], feed_dict={X: x_data, Y: y_data})
if step < 10 or step > 195:
print(step, cost_val, W_val)
# predict
print("Prediction:", sess.run(prediction, feed_dict={X: x_test}))
# Calculate the accuracy
print("Accuracy: ", sess.run(accuracy, feed_dict={X: x_test, Y: y_test}))
매우 작은 Learning rate¶
Cost function의 변화가 너무 미미하다.
In [9]:
optimizer = tf.train.GradientDescentOptimizer(
learning_rate=1e-10).minimize(cost)
# Correct prediction Test model
prediction = tf.arg_max(hypothesis, 1)
is_correct = tf.equal(prediction, tf.arg_max(Y, 1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
# Launch graph
with tf.Session() as sess:
# Initialize TensorFlow variables
sess.run(tf.global_variables_initializer())
for step in range(201):
cost_val, W_val, _ = sess.run(
[cost, W, optimizer], feed_dict={X: x_data, Y: y_data})
if step < 10 or step > 195:
print(step, cost_val, W_val)
# predict
print("Prediction:", sess.run(prediction, feed_dict={X: x_test}))
# Calculate the accuracy
print("Accuracy: ", sess.run(accuracy, feed_dict={X: x_test, Y: y_test}))
'Dev > 딥러닝' 카테고리의 다른 글
07-3. Tensor flow 에서 epoch 과 batch 설명 및 예제 (0) | 2018.08.23 |
---|---|
07-2. Tensor Flow 에서 Data preprocessing (normalization) 구현 (0) | 2018.08.22 |
06-2. Tensorflow 에서 One hot 인코딩을 이용한 classification (0) | 2018.08.22 |
06-1. Tensor Flow 로 3종류 이상 Classfication (Soft max Classifier) (0) | 2018.08.20 |
05. Tensor Flow 로 Classification 예제 (binary) (0) | 2018.08.17 |
댓글