본문 바로가기
IT 기록/머신러닝

[머신러닝]03-2.Linear Regression의 cost 최소화 Tensorflow 구현

by Lazy Quant 2018. 7. 11.
반응형

※본 포스팅은 직접 강의하는 내용이 아닌, 김성훈 교수님의 머신러닝 강의를 정리하기 위한 포스팅입니다.

김성훈 교수님의 강의는 모두를 위한 머신러닝/딥러닝(http://hunkim.github.io/ml)에서 들을 수 있습니다.


3-1.Cost 함수 그래프 보기

# 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()
예제 3-1 결과

참고) matplotlib : 다양한 데이터를 많은 방법으로 도식화할 수 있도록 하는 파이썬 라이브러리(pyplot 모듈 사용)

참고) tf.set_random_seed(seed) : 난수를 생성하기 위해 사용한다. seed의 값에 따라 다른 난수가 생성된다. 예제에서는 동일한 난수를 생성하기 위해(재현성을 위해) 동일한 seed '777'을 사용하였다. 


3-2.Cost gradient update

# 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)

#위 4줄은 아래 수식의 미분을 소스코드로 표현한 것


# 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))


예제 3-2 결과

step cost W

0 1.9531559 [0.3530584] 1 0.55556446 [0.65496445] 2 0.15802719 [0.81598103] 3 0.04494996 [0.90185654] 4 0.012785784 [0.9476568] 5 0.003636841 [0.9720836] 6 0.0010344847 [0.98511124] 7 0.00029424974 [0.99205935] 8 8.369887e-05 [0.995765] 9 2.3806637e-05 [0.99774134] 10 6.772017e-06 [0.9987954] 11 1.9260958e-06 [0.9993576] 12 5.477728e-07 [0.9996574] 13 1.5589518e-07 [0.99981725] 14 4.4308663e-08 [0.99990255] 15 1.2606658e-08 [0.999948] 16 3.5881709e-09 [0.9999723] 17 1.0196951e-09 [0.9999852] 18 2.8887825e-10 [0.99999213] 19 8.02487e-11 [0.9999958] 20 2.3405278e-11 [0.99999774]


W의 결과 값이 1에 수렴한다.

참고) tf.global_variables_initializer() : 모델의 다른 연산을 실행하기 전에 반드시 명시적으로 변수 초기화를 실행해야 한다. 가장 쉬운 방법으로는 모든 변수를 초기화 하는 연산을 모델 사용 전에 실행하는 것이다. 이 때 tf.global_variables_initializer()을 사용한다.


3-3.Cost tf optimizer

# 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)

#아래 미분을 표현한 소스코드를 위 2줄로 해결 #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(100): print(step, sess.run(W)) sess.run(train)


예제 3-3 결과

W=5.0로 실행 했을 때
0 5.0
1 1.2666664
2 1.0177778
3 1.0011852
4 1.000079
5 1.0000052
6 1.0000004
7 1.0
8 1.0
9 1.0

W=-9.0로 실행 했을 때
0 -9.0
1 0.33333397
2 0.9555556
3 0.99703705
4 0.9998025
5 0.9999868
6 0.9999991
7 0.99999994
8 1.0
9 1.0

어떠한 W값으로 실행해도 결국 같은 결과 값을 같게 된다.


반응형

댓글