티스토리 뷰
Softmax classification
Tensorflow
- softmax_cross_entropy_with_logits를 사용하여 Softmax classification구현
- 동물의 특징에 따라 7가지로 분류 예제
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 59 60 61 | import tensorflow as tf import numpy as np # 동물 데이터 불러오기 xy = np.loadtxt('data-04-zoo.csv', delimiter=',', dtype=np.float32) x_data = xy[:, 0:-1] y_data = xy[:, [-1]] # Y data가 0 ~ 6으로 7가지 nb_classes = 7 # placeholder X = tf.placeholder(tf.float32, shape=[None, 16]) Y = tf.placeholder(tf.int32, shape=[None, 1]) # shape = (?, 1) # Y data를, one-hot으로 변경 : shape = (?, 1, 7) Y_one_hot = tf.one_hot(Y, nb_classes) # one_hot을 통과하면 차원이 늘어나므로, reshape로 줄여주기 : shape = (?, 7) Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes]) W = tf.Variable(tf.random_normal([16, nb_classes]), name="weight") b = tf.Variable(tf.random_normal([nb_classes]), name="bias") # Hypothesis : softmax function 사용 # softmax = exp(logits) / reduce_sum(exp(logits)) logits = tf.matmul(X, W) + b hypothesis = tf.nn.softmax(logits) # cost/loss function : cross entropy cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y_one_hot) cost = tf.reduce_mean(cost_i) # Minimize : Gradient Descent 사용 train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) # argmax() : [0.1, 0.3, 0.5]의 argmax는 1로 가장 큰 값의 index 출력 prediction = tf.argmax(hypothesis, 1) correct_prediction = tf.equal(prediction, tf.argmax(Y_one_hot, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 세션 시작 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for step in range(2001): sess.run(train, feed_dict={X: x_data, Y: y_data}) if step % 100 == 0: loss, acc = sess.run([cost, accuracy], feed_dict={X: x_data, Y: y_data}) print(step, sess.run([cost, accuracy], feed_dict={X: x_data, Y: y_data})) # 2000 [0.053728174, 1.0] # Predict Test pred = sess.run(prediction, feed_dict={X: x_data}) # y_data.flatten() : 다차원 배열을 1차원 배열로 쭉 펴준다. # zip : pred와 y_data.flatten() 2개의 배열을 하나로 묶어서 p, y로 넘겨줌 for p, y in zip(pred, y_data.flatten()): print("[{}] Prediction: {} True Y: {}".format(p == int(y), p, int(y))) # [True] Prediction: 6 True Y: 6 # [True] Prediction: 1 True Y: 1 | cs |
-----------------------------------------
이 글은 모두를 위한 딥러닝을 보고 정리한 글입니다.
'AI > 딥러닝(sung kim)' 카테고리의 다른 글
learning rate, standardization, normalization (0) | 2018.07.12 |
---|---|
과적합(Overfitting) (0) | 2018.07.12 |
TensorFlow로 Softmax Classification의 구현하기 (0) | 2018.06.14 |
softmax and cost function (0) | 2018.06.12 |
Multinomial 개념 (0) | 2018.06.10 |
댓글