X: 유저가 학습한 시간
Y: 유저가 받은 점수
-> X 데이터에 없는 유저(7시간학습)가 받는 점수를 예측하는것이 목표.
가설을 세우자.
H(x)=Wx + b라는 형태의 직선을 통해.
오른쪽 파란선(hypothesis)를 기준으로 밖에 있는 범위의 값(x 표시되어있는 3개)에 대해서 방정식으로 값을 유도 -> Cost function
cost = 1/m * (H(x)-y)^2 (양수가 되어야 하므로.)
hypothesis와 cost function을 정리
우리의 목표 -> cost 값을 최소로 하는것 -> 예측하는 근사치와 좀 더 가까워지는 경향.
# Lab 2 Linear Regression
#tf 라이브러리 입력, 난수 생성
import tensorflow as tf
tf.set_random_seed(777) # for reproducibility
# X and Y data
x_train = [1, 2, 3]
y_train = [1, 2, 3]
# Try to find values for W and b to compute y_data = x_data * W + b
# We know that W should be 1 and b should be 0
# But let TensorFlow figure it out
W = tf.Variable(tf.random_normal([1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
# Our hypothesis XW+b
hypothesis = x_train * W + b
# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - y_train))
# Minimize
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
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())
# Fit the line
for step in range(2001):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(cost), sess.run(W), sess.run(b))
# Learns best fit W:[ 1.], b:[ 0.]
위 코드 정리하자면,
1. X_data, Y_data로 w,b 값을 도출해내어야 한다.
2. X,Y 그래프를 이용하여 3개 data를 표시한다
3. cost function이 minimize되게끔 w,b를 랜덤으로 정한다.
4. w,b값은 w->1, b->0으로 수렴한다.
5. 1차함수의 형태가 random -> y=x 꼴로 바뀐다.
'코딩이것저것' 카테고리의 다른 글
CNN_multi-variable linear regression (0) | 2017.07.31 |
---|---|
CNN_Linear Regression의 cost 최소화 알고리즘의 원리 설명 (0) | 2017.07.31 |
openCV_영상파일에서 얼굴 감지하기 (0) | 2017.07.28 |
openCV_웹캠영상에서 얼굴 인식하기 (0) | 2017.07.28 |
openCV_이미지에서 얼굴 인식하기 (0) | 2017.07.28 |