본문 바로가기
Developer/Machine Learning

#7. Tensorflow로 linear regression cost 최소화 구현하기

by Doony 2017. 11. 26.

본 포스팅은 인프런에서 제공되는 "모두를 위한 딥러닝 - 기본적인 머신러닝과 딥러닝 강좌"를 참고하여 만들어졌습니다. 

인프런 URL: https://www.inflearn.com/

텐서플로우를 통해, 선형회귀에서 cost 최소화를 구현해보자.



  Tensorflow 로 linear regression 구현하기.

먼저 cost가 어떻게 생겨먹었는지 그래프로 보도록 하자. 일단 아래 깃허브에서 코드는 따왔다.

https://github.com/hunkim/DeepLearningZeroToAll/blob/master/lab-03-1-minimizing_cost_show_graph.py 

참고로 matplotlib은, 그래프를 띄우기 위해 가져오는 라이브러리로, 아래와 같이 콘솔창에서 미리 설치가 필요하다.


python -m pip install -U pip setuptools
python -m pip install matplotlib

# Lab 3 Minimizing Cost
import tensorflow as tf
import matplotlib.pyplot as plt
tf.set_random_seed(777) # for reproducibility
X = [1, 2, 3]
Y = [1, 2, 3]
W = tf.placeholder(tf.float32)
# Our hypothesis for linear model X * W
hypothesis = X * W
# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
# Launch the graph in a session.
sess = tf.Session()
# Variables for plotting cost function
W_history = []
cost_history = []
for i in range(-30, 50):
curr_W = i * 0.1
curr_cost = sess.run(cost, feed_dict={W: curr_W})
W_history.append(curr_W)
cost_history.append(curr_cost)
# Show the cost function
plt.plot(W_history, cost_history)
plt.show()



지난 시간에 설명했듯이, convex function 형태를 가져야 하는데, 위 그래프는 평범한 2차 함수로 나타나서 만족!

고로, 이제 Gradient Descent를 해보자. 즉, 기울이를 따라서 cost가 minimize되는 값을 찾아보자.



learning_rate = 0.1 gradient = tf.reduce_mean((W * X - Y) * X)
descent = W - learning_rate * gradient
update = W.assign(descent)

learning_rate은, W에서 빼주는 부분의 상수 값.

update는, W에다가 저 descent라는 값으로 assign하라는 것. 따라서 이제 업데이트를 하면, 값이 바뀐다.


# Lab 3 Minimizing Cost
import tensorflow as tf
tf.set_random_seed(777) # for reproducibility
x_data = [1, 2, 3]
y_data = [1, 2, 3]
# 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')
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
# Our hypothesis for linear model X * W
hypothesis = X * W
# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
# Minimize: Gradient Descent using derivative: W -= learning_rate * derivative
learning_rate = 0.1
gradient = tf.reduce_mean((W * X - Y) * X)
descent = W - learning_rate * gradient
update = W.assign(descent)
# Launch the graph in a session.
sess = tf.Session()
# Initializes global variables in the graph.
sess.run(tf.global_variables_initializer())
for step in range(21):
sess.run(update, feed_dict={X: x_data, Y: y_data})
print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(W))


위와 같이 코딩이 된다. 보다시피, gloval_variables_initializer가 또 들어가있다. 즉, 변수를 초기화시킨다.

그 후, update를 실행시켜 W에 새로운 값을 넣고.. feed_dict로 늘 데이터셋을 넣어준다.



자, 위에는 수동으로 직접 알고리즘을 넣어준거고, 이번에는 GradientDescentOptimizer로 실행해보자.

# Lab 3 Minimizing Cost
import tensorflow as tf
tf.set_random_seed(777) # for reproducibility
# tf Graph Input
X = [1, 2, 3]
Y = [1, 2, 3]
# Set wrong model weights
W = tf.Variable(5.0)
# Linear model
hypothesis = X * W
# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
# Minimize: Gradient Descent Magic
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
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())
for step in range(100):
print(step, sess.run(W))
sess.run(train)

초기 W값을 아예 5로 지정해주는 것. GradientDescentOptimizer 한줄로 간단하게 표현할 수 있다.

여기서, gradient, 즉 cost의 기울기값을 따로 빼오는 함수도 있다. 

gvs = optimizer.compute_gradients(cost)   

이런식으로..... 필요 시 수정할 수 있다고 한다.


#6. Linear Regression의 cost 최소화 알고리즘

#5. Tensorflow로 간단한 linear regression 구현하기

#4. Linear Regression 머신러닝 선형회귀


공감은 큰 힘이 됩니다.

두니월드로 놀러오세요.




댓글