### Hypothesis and cost function
H(x) = Wx + b
1 2 3 4 5 6 7 | 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(W,b)
1 | cost = tf.reduce_mean(tf.square(hypothesis - y_train)) |
Variable은 텐서플로 내에서 사용되는 변수.
일반적인 컴퓨터 언어에서의 변수와는 조금 다른 의미이다.
텐서플로가 자체적으로 값을 변경한다.
내가 만들고자 하는 표본과 결과값을 구하기 위해 어떤 가설을 사용할지와 코스트를 줄이는 것이 관건인 것 같다.
GradientDescent
1 2 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) train = optimizer.minimize(cost) |
Run
1 2 3 4 5 6 7 | 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)) |
>>
1 2 3 4 5 6 7 8 9 10 | 0 3.4877102 [1.9187387] [-0.12721722] 20 0.057189822 [1.2554799] [-0.39401332] 40 0.023739621 [1.1844296] [-0.40146947] 60 0.021305015 [1.17014] [-0.38507512] 80 0.019347252 [1.1616088] [-0.3672136] ... 1920 2.7551075e-06 [1.0019279] [-0.00438223] 1940 2.5023674e-06 [1.0018374] [-0.00417636] 1960 2.2726144e-06 [1.001751] [-0.00398015] 1980 2.0640389e-06 [1.0016687] [-0.00379318] 2000 1.8748888e-06 [1.0015904] [-0.00361498] |
처음엔 코스트와 W, b의 값이 크지만 갈수록 값이 작아지는 것을 볼 수 있다.
Placeholder
1 2 3 4 5 6 7 8 9 | X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32) for step in range(2001): cost_val, W_val, b_val, _ = \ sess.run([cost, W, b, train], feed_dict={X: [1,2,3], Y: [1,2,3]}) if step % 20 == 0: print(step, cost_val, W_val, b_val) |
>> 결과는 위와 동일함.
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 | 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]) Y = tf.placeholder(tf.float32, shape=[None]) # our hyphothesis XW+b hypothesis = X * W + b # cost/loss function cost = tf.reduce_mean(tf.square(hypothesis - Y)) #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 with new traning data 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) |
>> 요로케 해도 동일
1 2 3 4 | print(sess.run(hypothesis, feed_dict={X: [5]})) print(sess.run(hypothesis, feed_dict={X: [2.5]})) print(sess.run(hypothesis, feed_dict={X: [1.5, 3.5]})) |
>>
1 2 3 | [6.0996594] [3.600272] [2.6005168 4.600027 ] |
### 정리
결국 모델(그래프)을 어떻게 만드느냐.
그리고 어떤 데이터로 학습시키느냐가 가장 중요하다.
그 다음 단계가 코스트를 얼마나 최소화 하느냐.
일단 여기까지만 알아두자.
### 참고
https://www.youtube.com/watch?v=mQGwjrStQgg&list=PLlMkM4tgfjnLSOjrEJN31gZATbcj_MpUm&index=5
해당 영상을 기반으로 작성
'AI - ML' 카테고리의 다른 글
ML lec 5-1: Logistic Classification의 가설 함수 정의 (0) | 2019.03.18 |
---|---|
ML lab 04-2: TensorFlow로 파일에서 데이타 읽어오기 (new) (0) | 2019.03.18 |
ML lec 02 - Linear Regression의 Hypothesis 와 cost 설명 (0) | 2019.02.18 |
ML lab 01 - TensorFlow의 설치및 기본적인 operations (new) (0) | 2019.02.18 |
ML lec 01 - 기본적인 Machine Learning 의 용어와 개념 설명 (0) | 2019.02.15 |