[딥러닝] Linear Regression의 cost 최소화의 TensorFlow 구현
딥러닝(Deep-Learning)

[딥러닝] Linear Regression의 cost 최소화의 TensorFlow 구현

반응형

[딥러닝] Linear Regression의 cost 최소화의 TensorFlow 구현




cost function

가설과 실제가 얼마나 차이나는지 확인하는 것


Gradient descent 알고리즘

Gradient descent - 경사를 내려가다

minimization problem 사용

어떤 점에서나 시작이 가능

W를 조금씩 바꾸면서 나아감 → cost(W,b) 절감 시도

반복

어떤 점에서 시작하든, 최소점에 도달할 수 있는 알고리즘

'미분'을 사용한다.



미분에 활용하기 편하게 수식을 약간 변경한 형태다.

여러번 실행시켜 W값이 변화되는 것이 cost를 minimize하는 값을 구하는 과정이다.



그래프에서 기울기가 +면 W식에서 점차 값이 감소하게 된다.

그래프에서 기울기가 -면 W식에서 cost(W)가 음수가 되어 -끼리 만나 +가 되어 점차 값이 증가하게 된다.







matplotlib 설치 (윈도우)


cmd 창에 다음과 같이 순서대로 입력하면 matplotlib이 설치된다. 이를 통해 Linear Regression의 모듈을 run해서 그래프 형태의 모습을 볼 수 있다.

python -m pip install -U pip setuptools

python -m pip install matplotlib





matplotlib을 이용한, Linear Regression 알고리즘의 그래프 형태 보기


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
import tensorflow as tf
import matplotlib.pyplot as plt
 
= [123]
= [123]
 
= 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()
# Initializes global variables in the graph.
sess.run(tf.global_variables_initializer())
# Variables for plotting cost function
W_val = []
cost_val = []
for i in range(-3050):
    feed_W = i * 0.1
    curr_cost, curr_W = sess.run([cost, W], feed_dict={W: feed_W})
    W_val.append(curr_W)
    cost_val.append(curr_cost)
 
# Show the cost function
plt.plot(W_val, cost_val)
plt.show()
 
cs


이전 게시글에서 작성한 Linear Regression 알고리즘 코드와 매우 유사한 형태다. matplotlib을 사용하기 위해 import를 시켜주고, plt로 W와 cost의 value 값을 가져와 show 함수로 그래프 형태를 나타내면 다음과 같다.






derivative를 사용한 Gradient Descent 알고리즘 최소화 방법


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
import tensorflow as tf
x_data = [123]
y_data = [123]
 
= tf.Variable(tf.random_normal([1]), name='weight')
= tf.placeholder(tf.float32)
= 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))
cs



W는 점점 1로 수렴하는 모습을 확인할 수 있다.


즉, matplotlib으로 확인한 그래프에서 볼 수 있듯이 x가 어디 위치든 결국 W가 1로 가는 곳으로 움직인다.





반응형