티스토리 뷰
Logistic Regression¶
In [1]:
from PIL import Image
Image.open('Logistic Regression.png')
Out[1]:
Training Data¶
In [5]:
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]]
# placeholders for a tensor that will be always fed.
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 using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W) + b))
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)
# cost/loss function
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)
# Accuracy computation
# True if hypothesis>0.5 else False
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
# float 32에 casting 하면 true=1, false=0
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted,Y), dtype=tf.float32))
# 예측값과 y가 같으면 true, 틀리면 false 거기에 casting하면 1,0
# Launch graph
with tf.Session() as sess:
# Initialize Tensorflow variables
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)
# Accuracy report
h, c, a = sess.run([hypothesis, predicted, accuracy],
feed_dict={X: x_data, Y: y_data})
print("\nHipothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a)
Classifying diabetes¶
feature들과 target값(당뇨병=1,아니면=0)
In [6]:
Image.open('diabetes.png')
Out[6]:
In [ ]:
import numpy as np
xy = np.loadtxt('data-03-diabetes.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]
# placeholders for a tensor that will be always fed.
X = tf.placeholder(tf.float32, shape=[None, 8])
Y = tf.placeholder(tf.float32, shape=[None, 1])
W = tf.Variable(tf.random_normal([8, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
#Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W)))
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)
# cost/loss function
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)
# Accuracy computation
# True if hypothesis>0.5 else False
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())
feed = {X: x_data, Y: y_data}
for step in range(10001):
sess.run(train, feed_dict=feed)
if step % 200 == 0:
print(step, sess.run(cost, feed_dict=feed))
# Accuracy report
h, c, a =sess.run([hypothesis, predicted, accuracy], feed_dict=feed)
print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a)
In [ ]:
# 결과 값 일부만 출력
0 1.3086263
200 0.8495514
400 0.7393282
600 0.69743496
800 0.6690898
...
9200 0.47940743
9400 0.4790678
9600 0.47874758
9800 0.4784453
10000 0.4781597
Hypothesis:
[[0.40194005]
[0.93241286]
[0.24879721]
[0.940664 ]
[0.10599464]
...
[0.69642776]
[0.7324742 ]
[0.8581585 ]
[0.6740077 ]
[0.9035657 ]]
Correct (Y):
[[0.]
[1.]
[0.]
[1.]
[0.]
...
[1.]
[1.]
[1.]
[1.]
[1.]]
Accuracy: 0.7720685
Exercise
- CSV reading using tf.decode_csv
- Try other classification data from Kaggle
'beginner > 파이썬 딥러닝 기초' 카테고리의 다른 글
Softmax Regression 이론 (0) | 2019.05.05 |
---|---|
Logistic Classification 이론 (0) | 2019.05.05 |
Loading data from file (0) | 2019.05.04 |
Multi-variable linear regression (0) | 2019.05.03 |
Linear Regression의 cost 최소화의 TensorFlow 구현 (0) | 2019.05.03 |