Cost Function.
- How fit the line to our (Training) Data?
Cost = ( H ( x1 ) - y1 )^2 + ( H ( x2 ) - y2 )^2 + ( H ( x3 ) - y3 )^2
-> 제곱하는 이유 : 음수, 양수를 모두 양수로 통일시키기 위하여
Linear Regression?
예측값과 실제값의 오차를 구하여 가장 적은 오차를 찾음
즉 가장 작은 값 (가장 오차가 적은 값) 을 나오게 하는 W와 b를 구하는 과정
in Tenserflow
1. Build graph using Tensorflow operations
2. sess.run(op, feed_dict={x:x_data})
3. update variables in the graph (and return values)
How to minimize cost?
최솟값을 구하는 알고리즘
Gradient descent algorithem : 경사를 따라 내려가는 알고리즘
- Minimize cost function
- used many minimization problems
- For given cost func. , cost(W, b), it will find W, b to minimize cost
- It can be applied to more general func. : cost(w1, w2, ...)
How it works? / How would you find the lowest point?
start any value ex(0, 0)
Keeping changing W and b a liittle bit to try and reduce cost(W, b)
경사도는 어떻게? -> 미분을 이용함
Convex Fucntion : 어느점에서 시작하던 결과값이 같음
H(x) = Wx
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]) # hypothesis = XW + b 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]}) # 20번에 한번씩 출력 if step % 20 == 0: print(step, cost_val, W_val, b_val) | cs |
https://github.com/BadSchool/Study/blob/master/tensorflow/tf_1.py
=====================================================================
=====================================================================
'Programming > Tensorflow' 카테고리의 다른 글
[Tensorflow] Binary Classfication (0) | 2017.07.30 |
---|---|
[Tensorflow] Multi-Variable linear regression (use matrix) (0) | 2017.07.30 |
[Tensorflow] 준비사항 2 (0) | 2017.07.30 |
[Tensorflow] 준비사항 1 (0) | 2017.07.30 |
[Tensorflow] Intro (0) | 2017.07.30 |