Softmax classification
# Lab 6 Softmax Classifier
import tensorflow as tf
tf.set_random_seed(777) # for reproducibility
x_data = [[1, 2, 1, 1],
[2, 1, 3, 2],
[3, 1, 3, 4],
[4, 1, 5, 5],
[1, 7, 5, 5],
[1, 2, 5, 6],
[1, 6, 6, 6],
[1, 7, 7, 7]]
# One-hot Encoding ์ด๋ผ ํ๋ค.
y_data = [[0, 0, 1],
[0, 0, 1],
[0, 0, 1],
[0, 1, 0],
[0, 1, 0],
[0, 1, 0],
[1, 0, 0],
[1, 0, 0]]
X = tf.placeholder("float", [None, 4])
Y = tf.placeholder("float", [None, 3])
nb_classes = 3
W = tf.Variable(tf.random_normal([4, nb_classes]), name='weight')
b = tf.Variable(tf.random_normal([nb_classes]), name='bias')
# tf.nn.softmax computes softmax activations
# softmax = exp(logits) / reduce_sum(exp(logits), dim)
hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)
# Cross entropy cost/loss
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
# Launch graph
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(2001):
_, cost_val = sess.run([optimizer, cost], feed_dict={X: x_data, Y: y_data})
if step % 200 == 0:
print(step, cost_val)
print('--------------')
# Testing & One-hot encoding
a = sess.run(hypothesis, feed_dict={X: [[1, 11, 7, 9]]})
# ์ต๋๊ฐ์ argument๋ฅผ ๋ฆฌํดํด์ค
print(a, sess.run(tf.argmax(a, 1)))
print('--------------')
b = sess.run(hypothesis, feed_dict={X: [[1, 3, 4, 3]]})
print(b, sess.run(tf.argmax(b, 1)))
print('--------------')
c = sess.run(hypothesis, feed_dict={X: [[1, 1, 0, 1]]})
print(c, sess.run(tf.argmax(c, 1)))
print('--------------')
all = sess.run(hypothesis, feed_dict={X: [[1, 11, 7, 9], [1, 3, 4, 3], [1, 1, 0, 1]]})
print(all, sess.run(tf.argmax(all, 1)))
'''
0 6.926112
200 0.6005015
400 0.47295815
600 0.37342924
800 0.28018373
1000 0.23280522
1200 0.21065344
1400 0.19229904
1600 0.17682323
1800 0.16359556
2000 0.15216158
-------------
[[1.3890490e-03 9.9860185e-01 9.0613084e-06]] [1]
-------------
[[0.9311919 0.06290216 0.00590591]] [0]
-------------
[[1.2732815e-08 3.3411323e-04 9.9966586e-01]] [2]
-------------
[[1.3890490e-03 9.9860185e-01 9.0613084e-06]
[9.3119192e-01 6.2902197e-02 5.9059085e-03]
[1.2732815e-08 3.3411323e-04 9.9966586e-01]] [1 0 2]
'''
Fancy Ver. Softmax Classification
์์ ์ฝ๋์์ cost๋ฅผ ์ ์ํ๋๋ฐ ์์ด ์์ด ๋ณต์กํ๋,
๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํด์ฃผ๋ ํจ์๋ฅผ ์ฌ์ฉํด ๋ณด์!
# From
logits = tf.matul(X, W) + b
hypothesis = tf.nn.softmax(logits)
cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(hypothesis), axis=1))
# To
cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels = Y_one_hot)
cost = tf.reduce_mean(cost_i)
label์ one-hot ์ฝ๋ฉ์ผ๋ก ๋์ด ์๋ Y ๋ฅผ ๋ฃ์ด์ผ ํ๋ค๋ ์๋ฏธ์์ Y_one_hot์ผ๋ก ์ ํ๋ค.
์ด๋ ๊ฒ ํด์ฃผ๋ฉด, cost_i ๋ ๊ฐ ํ์ ๋ํ cost๊ฐ๋ค์ ๋ค ๊ฐ๊ณ ์๋ค.
๋ง์ง๋ง์ผ๋ก ์ด ํ์ ๊ดํ ๊ฐ๋ค์ ํ๊ท ๋ด์ฃผ๋ฉด ๊ทธ๊ฒ cost์ด๋ค.
๋๋ฌผ ํน์ง์ ๋ฐ๋ฅธ ์ข ๋ถ๋ฅํ๊ธฐ
๋ฐ์ดํฐ๊ฐ ์ฃผ์ด์ก์ ๋, Y๊ฐ ํด๋์ค์์๋ One-hot ์ฝ๋ฉ์ด ์๋์ด ์๋ค๋ฉด,
์ด๊ฒ๋ถํฐ ๋ง์ ธ์ฃผ๋ ๊ฒ์ด ๋ง๋ค.
Y = tf.placeholder(tf.int32, [None, 1]) # shape = [?, 1]
# ๋ด Y ๋ฐ์ดํฐ, class ๊ฐฏ์๋ฅผ ์คํ
๋ ๋ง๋ค์ด์ค
# ์ฌ๊ธฐ์ ์กฐ์ฌํด์ผ ํ๋๊ฒ, ๋ด๊ฐ one_hot์ ์คํํ๊ณ ๋ ๋ค ๊ฒฐ๊ณผ ๋ญํฌ๋ ํ์ฐจ์ ํฐ ๊ฐ์ ๋๋ ค์ค๋ค.
# ์ฆ, ์ฌ๊ธฐ์ ๋ด๊ฐ Y๋ rank = 2์ด๋ค.
# one_hot์ ๋๋ฆฌ๊ณ ๋ ๋ค rank๋ 3์ ๋ฆฌํดํ๋ค๋ ์๊ธฐ
Y_one_hot = tf.one_hot(Y, nb_classes) # shape = [?, 1, 7]
# Example
# Y = [[0], [2]] -> shape = (?(2), 1)
# Y_one_hot = [[[1 0 0], [0 0 1]]] -> shape = (?(2), 1, 3)
# ๊ทธ๋ฐ๋ฐ, ์ฐ๋ฆฌ๋ shape -> (?, 7) ์ด๋ฐ ์์ผ๋ก ๋์ค๊ธฐ๋ฅผ ๋ฐ๋๋ค.
# ๊ทธ๋ด๋ ์ฌ์ฉํ๋ ๊ฒ์ด reshape ํจ์์ด๋ค.
# ์
๋ ฅ ๋ฃ๊ณ , ๋ด๊ฐ ์ํ๋ shape์ ํํ๋ฅผ ์จ์ฃผ๋ฉด ๋๋ค.
# ์ด ๋, -1์ '์์์' ๋ผ๋ ๋ป์ด๋ค.
Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes])
# Y_one_hot = [[1 0 0], [0 0 1]] -> shape = (?(2), 7)
์คํ ์ฝ๋
# Lab 6 Softmax Classifier
import tensorflow as tf
import numpy as np
tf.set_random_seed(777) # for reproducibility
# Predicting animal type based on various features
xy = np.loadtxt('data-04-zoo.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]
print(x_data.shape, y_data.shape)
'''
(101, 16) (101, 1)
'''
nb_classes = 7 # 0 ~ 6
X = tf.placeholder(tf.float32, [None, 16])
Y = tf.placeholder(tf.int32, [None, 1]) # 0 ~ 6
Y_one_hot = tf.one_hot(Y, nb_classes) # one hot
print("one_hot:", Y_one_hot)
Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes])
print("reshape one_hot:", Y_one_hot)
'''
one_hot: Tensor("one_hot:0", shape=(?, 1, 7), dtype=float32)
reshape one_hot: Tensor("Reshape:0", shape=(?, 7), dtype=float32)
'''
W = tf.Variable(tf.random_normal([16, nb_classes]), name='weight')
b = tf.Variable(tf.random_normal([nb_classes]), name='bias')
# tf.nn.softmax computes softmax activations
# softmax = exp(logits) / reduce_sum(exp(logits), dim)
logits = tf.matmul(X, W) + b
hypothesis = tf.nn.softmax(logits)
# Cross entropy cost/loss
# tf.stop_gradient() ํจ์๋ gradient ๋
ธ๋๊ฐ ์ํ๋ ๋, ์ด ๊ฐ์ ์ฐ์ฐ์ ์ํฅ์ ์ฃผ์ง ์์์
# ์๋ ค์ฃผ๋ ํจ์์ด๋ค.
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,
labels=tf.stop_gradient([Y_one_hot])))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
# ์ด๋ฒกํฐ๋ก ์ฃผ์ด์ง hypothesis์ max๋ฅผ ๊ฐ๋ index๋ฅผ ๋ฐํํ๋ค.
prediction = tf.argmax(hypothesis, 1)
# one-hot coding์ผ๋ก ๋ Y ๊ฐ์ค argmax๋ฅผ ๋ฐํํ๋ฉด, ํด๋น ์ ๋ต์ index๋ฅผ ๋ฐํํ ๊ฒ์ด๋ค.
# ๊ทธ๋ฆฌ๊ณ ๊ทธ ๊ฐ๊ณผ ๋ด๊ฐ ์์ํ index๊ฐ ๊ฐ์ผ๋ฉด 1์ ๊ฐ๊ณ ์๋๋ค.
correct_prediction = tf.equal(prediction, tf.argmax(Y_one_hot, 1))
# cast ํจ์๋, ์๋ ๊ฐ์ผ๋ฉด correct_prediction์ ๋
ธ๋์ด์ง๋ง,
# ์ด ํจ์๋ฅผ ์ฐ๋ฉด ํ
์๋ฅผ ๋ฐํํด์ค๋ค.
# ๋ค์ ํ๋ผ๋ฏธํฐ๋ ๋์ง๋, ์ด ํ
์์ ์๋ฃํ์ ์จ์ค๋ค.
# ์ฐธ๊ณ ๋ก ๋ชจ๋ ๊ฐ์ ํ
์๋ก ์์ง์ธ๋ค. ์ฆ ๋ฒกํฐ๋ ํ๋ ฌ์ด๋ ํ
์์ด๋ค.
# ๋ฐ๋ผ์ castํ ๋ค์ ๋ฐํ๊ฐ๋ ํ๋ ฌ์ด๋ค.
# reduce_mean ํจ์๋ ์ด ํ๋ ฌ์ ํ๊ท ์ ๊ตฌํด์ฃผ๋๋ฐ, ํ๋ผ๋ฏธํฐ์ ๋ฐ๋ผ ํ, ์ด, ์ ์ฒด๋ก ๊ตฌํ ์ ์๋ค.
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# Launch graph
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(2001):
_, cost_val, acc_val = sess.run([optimizer, cost, accuracy], feed_dict={X: x_data, Y: y_data})
if step % 100 == 0:
print("Step: {:5}\tCost: {:.3f}\tAcc: {:.2%}".format(step, cost_val, acc_val))
# Let's see if we can predict
pred = sess.run(prediction, feed_dict={X: x_data})
# y_data: (N,1) = flatten => (N, ) matches pred.shape
# [[1], [2]] -> [1, 2] ์ด๋ฐ์์ผ๋ก!
for p, y in zip(pred, y_data.flatten()):
print("[{}] Prediction: {} True Y: {}".format(p == int(y), p, int(y)))
'''
Step: 0 Loss: 5.106 Acc: 37.62%
Step: 100 Loss: 0.800 Acc: 79.21%
Step: 200 Loss: 0.486 Acc: 88.12%
...
Step: 1800 Loss: 0.060 Acc: 100.00%
Step: 1900 Loss: 0.057 Acc: 100.00%
Step: 2000 Loss: 0.054 Acc: 100.00%
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 3 True Y: 3
...
[True] Prediction: 0 True Y: 0
[True] Prediction: 6 True Y: 6
[True] Prediction: 1 True Y: 1
'''