CNN_Linear Regression의 cost 최소화 알고리즘의 원리 설명
저번시간엔 X, Y그래프를 봤다면 이번엔 W, cost 그래프이다.
W가 random으로 어느 값을 갖든 W -> 1로 수렴한다는 내용이다.
수렴하는 원리는 미분하는 기울기값의 절대값이 점점 줄어드는 원리이다.
W와 cost값이 어떻게 변하는지 원리를 알려주고 있다.
1. 그래프상에 곡선에서 임의의 점으로부터 시작한다.
2. 임의의 점에서 접선을 그어 기울기를 나타낸다. 그 기울기의 절대값은 줄어드는 방향으로 움직인다.
3. 반복한다
4. 최소값(w=1)으로 수렴한다.
# Lab 2 Linear Regression
import tensorflow as tf
tf.set_random_seed(777) # for reproducibility
# Try to find values for W and b to compute y_data = W * x_data + b
# We know that W should be 1 and b should be 0
# But let's use TensorFlow to figure it out
W = tf.Variable(tf.random_normal([1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
# Now we can use X and Y in place of x_data and y_data
# # placeholders for a tensor that will be always fed using feed_dict
# See http://stackoverflow.com/questions/36693740/
'''
placeholder를 이용하여 x,y 노드를 만들수있다. 빈 노드를 만들고 노드에 값을 지정해줄수있다. 저번시간에 했던 x,y data를 미리 지정하지 않아도 된다.
'''
X = tf.placeholder(tf.float32, shape=[None])
Y = tf.placeholder(tf.float32, shape=[None])
# Our hypothesis XW+b
hypothesis = X * W + b
# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
# Minimize
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)
# Launch the graph in a session.
sess = tf.Session()
# Initializes global variables in the graph.
sess.run(tf.global_variables_initializer())
# Fit the line
for step in range(2001):
cost_val, W_val, b_val, _ = \
sess.run([cost, W, b, train],
feed_dict={X: [1, 2, 3], Y: [1, 2, 3]})
#feed_dict -> placeholder로 지정된 노드에 값을 입력한다.
if step % 20 == 0:
print(step, cost_val, W_val, b_val)
# Learns best fit W:[ 1.], b:[ 0]
# Testing our model
print(sess.run(hypothesis, feed_dict={X: [5]}))
print(sess.run(hypothesis, feed_dict={X: [2.5]}))
print(sess.run(hypothesis, feed_dict={X: [1.5, 3.5]}))
'''
첫번째 문장에서 hypothesis = 5가 나온다. w=1, b=0이기 때문에.
두번째 문장, 세번째 문장도 X와 hypothesis는 동일한 값을 갖게 된다.
'''
# Fit the line with new training data
for step in range(2001):
cost_val, W_val, b_val, _ = \
sess.run([cost, W, b, train],
feed_dict={X: [1, 2, 3, 4, 5],
Y: [2.1, 3.1, 4.1, 5.1, 6.1]})
if step % 20 == 0:
print(step, cost_val, W_val, b_val)
# Testing our model
print(sess.run(hypothesis, feed_dict={X: [5]}))
print(sess.run(hypothesis, feed_dict={X: [2.5]}))
print(sess.run(hypothesis, feed_dict={X: [1.5, 3.5]}))
'''
feed_dict 데이터로 x,y 그래프를 그리면 w=1, b=1.1이 나온다.
첫번째 출력은 5x1+1.1 = 6.1이 나오고
두번째,세번째도 같은 방법으로 hypothesis가 출력된다.
'''