Tensorflow로 간단한 Linear regression 알고리즘 구현: 두 판 사이의 차이

기술노트
(컴퓨터 과학 용어 정리 - Tensorflow로 간단한 Linear regression 알고리즘 구현 추가)
 
편집 요약 없음
1번째 줄: 1번째 줄:
==== [딥러닝] Tensorflow로 간단한 Linear regression 알고리즘 구현 ====
==== [딥러닝] Tensorflow로 간단한 Linear regression 알고리즘 구현 ====


<br>
시험 점수를 예측하거나 연속적인 값을 예측해야 때는 회귀(regression)를 사용한다. 여기서는 TensorFlow를 이용한 간단한 선형 회귀(Linear Regression) 모델 구현을 다룬다.
 
시험 점수를 예상해야 (0~100) > regression을 사용
 
regression을 사용하는 예제를 살펴보자
 
<br>
 
<br>
 
여러 x와 y 값을 가지고 그래프를 그리며 가장 근접하는 선형(Linear)을 찾아야 한다.
 
이 선형을 통해서 앞으로 사용자가 입력하는 x 값에 해당하는 가장 근접한 y 값을 출력해낼 수 있는 것이다.
 
<img src="https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=http%3A%2F%2Fcfile21.uf.tistory.com%2Fimage%2F99A1B3365AF1362C106BFF">
 
<br>
 
현재 파란 선이 가설 H(x)에 해당한다.
 
실제 입력 값들 (1,1) (2,2) (3,3)과 선의 거리를 비교해서 근접할수록 좋은 가설을 했다고 말할 수 있다.
 
<br>
 
<br>
 
이를 찾기 위해서 Hypothesis(가설)을 세워 cost(비용)을 구해 W와 b의 값을 도출해야 한다.
 
<br>
 
===== '''Linear regression 알고리즘의 최종 목적 : cost 값을 최소화하는 W와 b를 찾자''' =====


<br>
여러 x와 y 값을 기반으로 가장 근접한 직선(선형 모델)을 찾아야 한다. 이 선을 통해 입력된 새로운 x 값에 대응하는 y 값을 예측할 수 있다.


<img src="https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=http%3A%2F%2Fcfile25.uf.tistory.com%2Fimage%2F99FEFF355AF13568288AE8">
[[File:https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=http%3A%2F%2Fcfile21.uf.tistory.com%2Fimage%2F99A1B3365AF1362C106BFF|center|600px]]


* H(x) : 가설
현재 파란 선은 가설 H(x)에 해당한다. 예를 들어, 데이터가 (1,1), (2,2), (3,3)이라면 선과 이 점들 간의 거리를 최소화하는 것이 좋은 가설이다.


* cost(W,b) : 비용
이를 위해 다음과 같은 개념을 사용한다:


* W : weight
'''H(x)''': 예측 가설


* b : bias
'''cost(W,b)''': 비용 함수


* m : 데이터 개수
'''W''': 가중치(기울기)


* H(x^(i)) : 예측 값
'''b''': 절편


* y^(i) : 실제 값
'''m''': 데이터 개수


<br>
'''H(x^(i))''': 예측값


'''(예측값 - 실제값)의 제곱을 하는 이유는?'''
'''y^(i)''': 실제값


> 양수가 나올 수도 있고, 음수가 나올 수도 있다. 또한 제곱을 하면, 거리가 먼 결과일 수록 값은 더욱 커지게 되어 패널티를 더 줄 수 있는 장점이 있다.
=== (예측값 - 실제값)의 제곱을 사용하는 이유 ===
음수 또는 양수 차이를 제거하고, 오차가 클수록 큰 페널티를 주기 위해 제곱을 사용한다.


<br>
[[File:https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=http%3A%2F%2Fcfile25.uf.tistory.com%2Fimage%2F99FEFF355AF13568288AE8|center|600px]]


<img src="https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=http%3A%2F%2Fcfile28.uf.tistory.com%2Fimage%2F99400A405AF13743173F87">
===== '''Linear regression 알고리즘의 최종 목적: cost 값을 최소화하는 W와 b를 찾자''' =====


이제 실제로, 파이썬을 이용해서 Linear regression을 구현해보자
== 미리 x와 y 값을 주었을 때 ==


<br>
<br>
===== '''미리 x와 y 값을 주었을 때''' =====
<syntaxhighlight>python
import tensorflow as tf
== X and Y data ==
x_train = [1, 2, 3]
x_train = [1, 2, 3]
y_train = [1, 2, 3]
y_train = [1, 2, 3]
 
W = tf.Variable(tf.random_normal([1]), name='weight')
W = tf.Variable(tf.random_normal([1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
b = tf.Variable(tf.random_normal([1]), name='bias')
 
== Our hypothesis XW+b ==
hypothesis = x_train * W + b
hypothesis = x_train * W + b // 가설 정의
== cost/loss function ==
cost = tf.reduce_mean(tf.square(hypothesis - y_train))
cost = tf.reduce_mean(tf.square(hypothesis - y_train))
 
#Minimize
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)
train = optimizer.minimize(cost)
 
== Launch the graph in a session. ==
sess = tf.Session()
sess = tf.Session()
== Initializes global variables in the graph. ==
sess.run(tf.global_variables_initializer())
sess.run(tf.global_variables_initializer())
 
== Fit the line ==
for step in range(2001):
for step in range(2001):
    sess.run(train)
sess.run(train)
    if step % 20 == 0:
if step % 20 == 0:
        print(step, sess.run(cost), sess.run(W), sess.run(b))
print(step, sess.run(cost), sess.run(W), sess.run(b))
</syntaxhighlight>
 
<br>
 
<img src="https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=http%3A%2F%2Fcfile4.uf.tistory.com%2Fimage%2F9963B34B5AF131E022AF07">
 
<syntaxhighlight>
x_train = [1, 2, 3]
y_train = [1, 2, 3]
</syntaxhighlight>
 
<br>
 
2000번 돌린 결과, [W = 1, b = 0]으로 수렴해가고 있는 것을 알 수 있다.
 
따라서, `H(x) = (1)x + 0`로 표현이 가능하다.
 
<br>
 
<br>
 
<syntaxhighlight>
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
</syntaxhighlight>
 
<br>
 
'''최소화 과정에서 나오는 learning_rate는 무엇인가?'''
 
GradientDescent는 Cost function이 최소값이 되는 최적의 해를 찾는 과정을 나타낸다.
 
이때 다음 point를 어느 정도로 옮길 지 결정하는 것을 learning_rate라고 한다.
 
<br>
 
'''learning rate를 너무 크게 잡으면?'''


* 최적의 값으로 수렴하지 않고 발산해버리는 경우가 발생(Overshooting)


<br>
2000번 반복 후, W는 1에 가까워지고 b는 0에 가까워진다. 따라서 가설은 H(x) = x로 수렴한다.


'''learning rate를 너무 작게 잡으면?'''
== learning_rate에 대하여 ==


* 수렴하는 속도가 너무 느리고, local minimum에 빠질 확률 증가
너무 크면 발산(over-shooting)


<br>
너무 작으면 수렴 속도가 느리고 local minimum에 갇힐 수 있음


> 보통 learning_rate는 0.01에서 0.5를 사용하는 것 같아보인다.
보통 0.01 ~ 0.5 사이 값을 사용한다.


<br>
== placeholder를 이용하여 값 전달하기 ==


<br>
===== placeholder를 이용해서 실행되는 값을 나중에 던져줄 때 =====
<syntaxhighlight>python
import tensorflow as tf
W = tf.Variable(tf.random_normal([1]), name='weight')
W = tf.Variable(tf.random_normal([1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
b = tf.Variable(tf.random_normal([1]), name='bias')
 
X = tf.placeholder(tf.float32, shape=[None])
X = tf.placeholder(tf.float32, shape=[None])
Y = tf.placeholder(tf.float32, shape=[None])
Y = tf.placeholder(tf.float32, shape=[None])
 
== Our hypothesis XW+b ==
hypothesis = X * W + b
hypothesis = X * W + b
== cost/loss function ==
cost = tf.reduce_mean(tf.square(hypothesis - Y))
cost = tf.reduce_mean(tf.square(hypothesis - Y))
#Minimize
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)
train = optimizer.minimize(cost)
 
== Launch the graph in a session. ==
sess = tf.Session()
sess = tf.Session()
== Initializes global variables in the graph. ==
sess.run(tf.global_variables_initializer())
sess.run(tf.global_variables_initializer())
 
== Fit the line ==
for step in range(2001):
for step in range(2001):
    cost_val, W_val, b_val, _ = sess.run([cost, W, b, train],
cost_val, W_val, b_val, _ = sess.run([cost, W, b, train],
                                        feed_dict = {X: [1, 2, 3, 4, 5],
feed_dict={X: [1, 2, 3, 4, 5], Y: [2.1, 3.1, 4.1, 5.1, 6.1]})
                                                      Y: [2.1, 3.1, 4.1, 5.1, 6.1]})
if step % 20 == 0:
    if step % 20 == 0:
print(step, cost_val, W_val, b_val)
        print(step, cost_val, W_val, b_val)
</syntaxhighlight>
 
<br>
 
<img src="https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=http%3A%2F%2Fcfile30.uf.tistory.com%2Fimage%2F9966EB3E5AF134071A0326">
 
<syntaxhighlight>
feed_dict = {X: [1, 2, 3, 4, 5],
            Y: [2.1, 3.1, 4.1, 5.1, 6.1]})
</syntaxhighlight>
 
2000번 돌린 결과, [W = 1, b = 1.1]로 수렴해가고 있는 것을 알 수 있다.


즉, `H(x) = (1)x + 1.1`로 표현이 가능하다.


<br>
[[File:https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=http%3A%2F%2Fcfile30.uf.tistory.com%2Fimage%2F9966EB3E5AF134071A0326|center|600px]]


<br>
최종적으로 가설 H(x) = x + 1.1로 수렴한다.


이 구현된 모델을 통해 x값을 입력해서 도출되는 y값을 아래와 같이 알아볼 수 있다.
== 예측 결과 확인 ==


<img src="https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=http%3A%2F%2Fcfile1.uf.tistory.com%2Fimage%2F99E0674F5AF138C21C1D8B">
훈련된 모델로 예측을 수행하면 입력한 x값에 따라 적절한 y값이 출력된다.

2025년 5월 6일 (화) 06:53 판

[딥러닝] Tensorflow로 간단한 Linear regression 알고리즘 구현

시험 점수를 예측하거나 연속적인 값을 예측해야 할 때는 회귀(regression)를 사용한다. 여기서는 TensorFlow를 이용한 간단한 선형 회귀(Linear Regression) 모델 구현을 다룬다.

여러 x와 y 값을 기반으로 가장 근접한 직선(선형 모델)을 찾아야 한다. 이 선을 통해 입력된 새로운 x 값에 대응하는 y 값을 예측할 수 있다.

현재 파란 선은 가설 H(x)에 해당한다. 예를 들어, 데이터가 (1,1), (2,2), (3,3)이라면 선과 이 점들 간의 거리를 최소화하는 것이 좋은 가설이다.

이를 위해 다음과 같은 개념을 사용한다:

H(x): 예측 가설

cost(W,b): 비용 함수

W: 가중치(기울기)

b: 절편

m: 데이터 개수

H(x^(i)): 예측값

y^(i): 실제값

(예측값 - 실제값)의 제곱을 사용하는 이유

음수 또는 양수 차이를 제거하고, 오차가 클수록 더 큰 페널티를 주기 위해 제곱을 사용한다.

Linear regression 알고리즘의 최종 목적: cost 값을 최소화하는 W와 b를 찾자

미리 x와 y 값을 주었을 때

x_train = [1, 2, 3] y_train = [1, 2, 3]

W = tf.Variable(tf.random_normal([1]), name='weight') b = tf.Variable(tf.random_normal([1]), name='bias')

hypothesis = x_train * W + b cost = tf.reduce_mean(tf.square(hypothesis - y_train))

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) train = optimizer.minimize(cost)

sess = tf.Session() sess.run(tf.global_variables_initializer())

for step in range(2001): sess.run(train) if step % 20 == 0: print(step, sess.run(cost), sess.run(W), sess.run(b))


2000번 반복 후, W는 1에 가까워지고 b는 0에 가까워진다. 따라서 가설은 H(x) = x로 수렴한다.

learning_rate에 대하여

너무 크면 발산(over-shooting)

너무 작으면 수렴 속도가 느리고 local minimum에 갇힐 수 있음

보통 0.01 ~ 0.5 사이 값을 사용한다.

placeholder를 이용하여 값 전달하기

W = tf.Variable(tf.random_normal([1]), name='weight') b = tf.Variable(tf.random_normal([1]), name='bias')

X = tf.placeholder(tf.float32, shape=[None]) Y = tf.placeholder(tf.float32, shape=[None])

hypothesis = X * W + b cost = tf.reduce_mean(tf.square(hypothesis - Y)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) train = optimizer.minimize(cost)

sess = tf.Session() sess.run(tf.global_variables_initializer())

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)


최종적으로 가설 H(x) = x + 1.1로 수렴한다.

예측 결과 확인

훈련된 모델로 예측을 수행하면 입력한 x값에 따라 적절한 y값이 출력된다.