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]]
X = tf.placeholder(tf.float32, shape = [None, 2])
Y = tf.placeholder(tf.float32, shape = [None, 1])
W = tf.Variable(tf.random_normal([2,1]), name = 'weight')
b = 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
====================================================================
====================================================================
'Programming > Tensorflow' 카테고리의 다른 글
[Tensorflow] Application & Tips (0) | 2017.08.02 |
---|---|
[Tensorflow] Multi-Variable linear regression (use matrix) (0) | 2017.07.30 |
[Tensorflow] Linear Regression (0) | 2017.07.30 |
[Tensorflow] 준비사항 2 (0) | 2017.07.30 |
[Tensorflow] 준비사항 1 (0) | 2017.07.30 |