In [1]:
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]]
# placeholder for a tensor will be fed
X = tf.placeholder(tf.float32, shape=[None, 2]) # n개 일때 None
Y = tf.placeholder(tf.float32, shape=[None, 1])
# X: nx2, Y: nx1 --> X * W = Y + b --> W 는 2x1, b는 1 (b는 항상 나가는값 갯수)
W = tf.Variable(tf.random_normal([2, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
# sigmoid : tf.div(1., 1. + tf.exp(tf.matmul(X, W) + b))
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)
cost = -tf.reduce_mean(Y * tf.log(hypothesis) +
(1 - Y) * tf.log(1 - hypothesis))
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)
# True if hypothesis >0.5, else False (dype을 float 로 cast 하면 1. or 0. 이 된다.)
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))
# Launch
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 % 500 == 0:
print(step, cost_val)
h, c, a = sess.run([hypothesis, predicted, accuracy],
feed_dict={X: x_data, Y: y_data})
print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuarcy: ", a)
Classifying diabetes¶
In [7]:
import numpy as np
import tensorflow as tf
# Load Data
xy = np.loadtxt('./static/data-03-diabetes.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]
# placeholder for a tensor will be fed
X = tf.placeholder(tf.float32, shape=[None, 8]) # n개 일때 None
Y = tf.placeholder(tf.float32, shape=[None, 1])
# X: nx2, Y: nx1 --> X * W = Y + b --> W 는 2x1, b는 1 (b는 항상 나가는값 갯수)
W = tf.Variable(tf.random_normal([8, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
# sigmoid : tf.div(1., 1. + tf.exp(tf.matmul(X, W) + b))
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)
cost = -tf.reduce_mean(Y * tf.log(hypothesis) +
(1 - Y) * tf.log(1 - hypothesis))
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)
# True if hypothesis >0.5, else False (dype을 float 로 하면 1. or 0.)
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))
# Launch
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 % 1000 == 0:
print(step, cost_val)
h, c, a = sess.run([hypothesis, predicted, accuracy],
feed_dict={X: x_data, Y: y_data})
print("\nAccuarcy: ", a)
'Dev > 딥러닝' 카테고리의 다른 글
06-2. Tensorflow 에서 One hot 인코딩을 이용한 classification (0) | 2018.08.22 |
---|---|
06-1. Tensor Flow 로 3종류 이상 Classfication (Soft max Classifier) (0) | 2018.08.20 |
04-2. Tensor Flow 에서 csv 파일 읽기 및 queue 사용법 (0) | 2018.08.17 |
04-1. Tensor Flow 변수가 여러개일때 Linear regression (0) | 2018.08.17 |
03. TensorFlow Linear Regression 의 cost 최소화 구현 (0) | 2018.08.17 |
댓글