Programming/Tensorflow2017. 7. 30. 18:30

ex)

Spam Detection : Spam or Ham

Facebook feed : Show or Hide

Credit Card Fraudulent Trasaction detection : legitimate / fraud

 

Linear Regression 문제점

합격임에도 불구하고 불합격 판정을 줄 수 있다.

 

- We know Y is 0 or 1

- H(x) = Wx + b

but Hypothesis can give vaules large than 1 or less than 0 (너무 큰 값을 받아들이지 못함)

 

Solution!

Used sigmoid Func.

 

Cost Func. 문제점

기존의 func는 변경된 H(x) 함수에 적용할 수 없음

 

Solution!

연립방정식으로 두가지 경우를 생각 (y=0, y=1)

 

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
27
28
29
30
31
32
33
34
35
36
import tensorflow as tf
 
x_data = [[1,2], [2,3], [3,1], [4,3], [5,3], [6,2]]
y_data = [[0], [0], [0], [1], [1], [1]]
 
= tf.placeholder(tf.float32, shape = [None, 2])
= tf.placeholder(tf.float32, shape = [None, 1])
 
= tf.Variable(tf.random_normal([2,1]), name = 'weight')
= tf.Variable(tf.random_normal([1]), name = 'bias')
 
# Hypothesis Func.
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)
 
# Cost Func.
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))
 
# Minimize Func.
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)
 
predicted = tf.cast(hypothesis > 0.5, dtype = tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype = tf.float32))
 
 
# Launch Graph
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
 
    for step in range(10001):
        cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data})
        if step % 200 == 0:
            print(step, cost_val)
 
 
    h, c, a = sess.run([hypothesis, predicted, accuracy], feed_dict = {X: x_data, Y: y_data})
    print("\nHypthesis: ", h, "\nCorrect (Y) ", c, "\Accuracy: ", a)
cs

 https://github.com/BadSchool/Study/blob/master/tensorflow/tf_5-2.py

====================================================================

 

 

====================================================================

Posted by BadSchool
Programming/Tensorflow2017. 7. 30. 18:12

Recap

- Hypothesis(가설. 어떻게 예측할 것 인지)

- Cost function(cost 계산 방법)

- Gradient descent algorithm(cost 값 최적화)

 

변수들이 많아지면 복잡해 지므로 Matrix를 사용하여 일괄 계산

 

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
27
28
#Matrix 사용 함
import tensorflow as tf
 
x_data = [[73.80.75.], [93.,88.,93], [89.,91.,90], [96.,98.,100.], [73.,66.,70]]
y_data = [[152.], [185.], [180.], [196.], [142.]]
 
= tf.placeholder(tf.float32, shape=[None, 3])
= tf.placeholder(tf.float32, shape=[None, 1])
 
= tf.Variable(tf.random_normal([3,1]), name='weight')
= tf.Variable(tf.random_normal([1]),name='bias')
 
hypothesis = tf.matmul(X,W) + b
 
cost = tf.reduce_mean(tf.square(hypothesis - Y))
 
optimizer = tf.train.GradientDescentOptimizer(learning_rate = 1e-5)
train = optimizer.minimize(cost)
 
sess = tf.Session()
 
sess.run(tf.global_variables_initializer())
for step in range(2001):
    cost_val, hy_val, _ = sess.run([cost, hypothesis, train],
        feed_dict={x1: x1_data, x2: x2_data, x3: x3_data, Y: y_data})
    if step % 10 == 0:
        print(step, "Cost: ", cost_val, "\nPrediction:\n",hy_val)
        
cs
https://github.com/BadSchool/Study/blob/master/tensorflow/tf_4-1.py

 

==================================================================

 

 

 

==================================================================

'Programming > Tensorflow' 카테고리의 다른 글

[Tensorflow] Application & Tips  (0) 2017.08.02
[Tensorflow] Binary Classfication  (0) 2017.07.30
[Tensorflow] Linear Regression  (0) 2017.07.30
[Tensorflow] 준비사항 2  (0) 2017.07.30
[Tensorflow] 준비사항 1  (0) 2017.07.30
Posted by BadSchool
Programming/Tensorflow2017. 7. 30. 18:00

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
= tf.Variable(tf.random_normal([1]), name='weight')
= tf.Variable(tf.random_normal([1]), name='bias')
= tf.placeholder(tf.float32, shape=[None])
= 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 : [12345], Y : [2.13.14.15.16.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
Posted by BadSchool