티스토리 뷰
Logistic classification
Tensorflow
-예습 복습에 따른 시험 합격 불합격 예측
x_data=[예습횟수,복습횟수]
y_data=[불합격or합격]
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf # y_data는 0과 1의 값만 가진다. x_data = [[1, 2], [2, 3], [3, 1], [4, 3], [5, 3], [6, 2]] y_data = [[0], [0], [0], [1], [1], [1]] # placeholder : Shape 주의! x_data [6, 2] / y_data [6, 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 : 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)) # Minimize : Gradient Descent 사용 train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost) # 정확도 측정 (hypothesis > 0.5 기준) # tf.cast() : true/false를 1.0/0.0 으로 반환 predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) # 세션 시작 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) # 10000 0.158035 # 정확도 측정 h, c, a = sess.run([hypothesis, predicted, accuracy], feed_dict={X:x_data, Y:y_data}) print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a) # Hypothesis: [[0.03441257] # [0.16354376] # [0.32198268] # [0.77366924] # [0.93461043] # [0.97851723]] # Correct(Y): [[0.] # [0.] # [0.] # [1.] # [1.] # [1.]] # Accuracy: 1.0 | cs |
-당뇨 데이터를 활용한 당뇨 예측
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 37 38 39 40 41 42 43 44 | import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf 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 : Shape 주의! 총 8개의 x_data와 1개의 y_data 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 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) # 정확도 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)) # 세션 시작 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) # 10000 0.480384 # 정확도 77% _, _, a = sess.run([hypothesis, predicted, accuracy], feed_dict={X: x_data, Y: y_data}) print("Accuracy: ", a) # Accuracy: 0.769433 | cs |
'AI > 딥러닝(sung kim)' 카테고리의 다른 글
softmax and cost function (0) | 2018.06.12 |
---|---|
Multinomial 개념 (0) | 2018.06.10 |
Logistic(regression) classification : cost function & gradient decent (0) | 2018.06.06 |
Logistic(regression) classification (1) | 2018.06.05 |
multi-variable linear regression을 tensorflow에서 구현하기 (0) | 2018.05.22 |
댓글