Lec 02 - Linear Regression의 가설(Hypothesis)과 비용(cost) 설명
https://www.youtube.com/watch?v=Hax03rCn3UI
- 시험성적 예측 (supervised learning) 선형 회귀분석
- x(hours), y(score) ->[[10,90], [9,80], [3,50], [2,30]] 의 경우.
- regression 모델로 training 시킴
- 예
- (x,y) ->[[1,1], [2,2], [3,3]] 의 경우
- 가설(Hypothesis) : linear regression.
- 최적의 선을 찾는 것이 학습과정
- 선형 회귀분석의 가설 : H(x) = Wx + b 에서, 가장 좋은 W와 b 를 찾아야 함.
- 어떤 가설이 좋은지를 찾는다는 것은, 실제 데이터와, 가설 H(x)에 의한 점과의 거리가 가까울 수록 좋은 모델임.
- 이렇게 거리를 측정하는 것을 Cost Function, Loss Function 이라고 함. 대부분 두 값의 차이의 제곱을 구하게 됨
- Cost function 의 정의
- 이 cost 함수는 W와 b 의 함수가 되는데, cost를 최소로 줄여주는 W, b를 찾는 것이 학습임.
Lab 02 TensorFlow로 간단한 linear regression을 구현하는 방법
- 테스트 소스코드가 있는 곳 : https://github.com/hunkim/DeepLearningZeroToAll
- 복습 : Linear Regression의 Hypothesis와 cost function.
- TF 연산을 사용하여 그래프를 Build
- Hypothesis
x_train = [1,2,3]
y_train = [1,2,3]
# tf의 Variable이란, TF가 t사용하는, 자체적으로 변경시키는, trainable 변수.
# [1]는 shape.
W = tf.Variable(tf.random_normal([1]), name = 'weight')
b = tf.Variable(tf.random_normal([1]), name = 'bias')
# 우리는 Linear Regression을 가정했으므로, W *x + b 가 hypothesis
hypothesis = x_train * W + b
- Cost function
# reduce_mean 은 평균을 구하라는 연산
cost = tf.reduce_mean(tf.square(hypothesis - y_train))
- GradientDescent : Minimize. 현재는 그냥 이렇게 쓴다고만 생각할 것.
# Minimize
optimizer = tf.train.GradientDescentOptimizer (learning_rate = 0.01)
train = optimizer.minimize(cost)
- 그래프를 실행/갱신 한뒤, 결과를 출력
# 세션에서 그래프를 실행시킨다.
sess = tf.Session()
#그래프에서 글로벌 변수(W, b)를 초기화한다.
sess.run(tf.global_variables_initializer())
# train을 실행시키면 여기에 연결된 optimizer-cost-hypothesis-W/b 가 모두 실행된다.
for step in range(2001) :
sess.run(train)
if step % 20 ==0 :
print(step, sess.run(cost), sess.run(W), sess.run(b))
- 결과
0 1.65899 [ 0.83828998] [-0.95781285]
20 0.0862329 [ 1.26493335] [-0.72846115]
40 0.0654389 [ 1.29238355] [-0.67667371]
1960 6.32628e-06 [ 1.00292134] [-0.00664066]
1980 5.7456e-06 [ 1.00278401] [-0.0063286]
2000 5.21818e-06 [ 1.00265312] [-0.00603121]
=========
- Placehoder를 사용할 경우
import tensorflow as tf
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]) # 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)
#여기까지는 optimize 시킨 W,b를 찾는 과정임.
#이를 사용하여 predict를 해보면...
print(sess.run(hypothesis, feed_dict={X: [5]}))
print(sess.run(hypothesis, feed_dict={X: [1.5, 5.5]}))
#이러한 답이 나온다.
[ 6.10004997]
[ 2.59992599 6.60006762]