출처 : 모두를위한 머신러닝 (http://hunkim.github.io/ml/)
One hot incoding 이란¶
One hot 인코딩은 특정 상수값으로 표현된 Y 를 Matrix 형태로 바꿔주는 방법이다.¶
위의 데이터를 보면 원본 파일에서는 각 직업에 따라 1~5 사이의 값으로 그 직업을 대표했다면, one hot encoding 을 거치고 난 후엔 1x5 행렬에서 한 값만 1이고 나머지는 0을 갖는 행렬로 표시된다.
아래 예제에서 Tensorflow 에서 어떻게 구동되는지 살펴본다.
예제: 동물의 종 파악하는 알고리즘¶
파일 위치: https://raw.githubusercontent.com/hunkim/DeepLearningZeroToAll/master/data-04-zoo.csv
데이터파일의 각 column 이 나타내는 의미:
- animal name: (deleted),,,,,,,,,,,,,,,,
- hair Boolean",,,,,,,,,,,,,,,,
- feathers Boolean",,,,,,,,,,,,,,,,
- eggs Boolean",,,,,,,,,,,,,,,,
- milk Boolean",,,,,,,,,,,,,,,,
- airborne Boolean",,,,,,,,,,,,,,,,
- aquatic Boolean",,,,,,,,,,,,,,,,
- predator Boolean",,,,,,,,,,,,,,,,
- toothed Boolean",,,,,,,,,,,,,,,,
- backbone Boolean",,,,,,,,,,,,,,,,
- breathes Boolean",,,,,,,,,,,,,,,,
- venomous Boolean",,,,,,,,,,,,,,,,
- fins Boolean",,,,,,,,,,,,,,,,
- legs Numeric (set of values: {0",2,4,5,6,8}),,,,,,,,,,,
- tail Boolean",,,,,,,,,,,,,,,,
- domestic Boolean",,,,,,,,,,,,,,,,
- catsize Boolean",,,,,,,,,,,,,,,,
- type Numeric (integer values in range [0",6]),,,,,,,,,,,,,,,
데이터 로드¶
- 데이터를 읽어들이고 가장 마지막 column 을 y 로 설정한다.
- 동물의 종의 종류가 7개 (0~6)
- X 의 placeholder 의 shape 은 16개의 column 이 존재하고, row 의 갯수는 알수없으므로 [None, 16] 으로 한다.
- Y 의 placeholder 는 0~6 사이의 값을 하나 갖기 때문에 [None, 1] 로 한다.
In [1]:
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('./static/data-04-zoo.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]
print(x_data.shape, y_data.shape)
nb_classes = 7 # 0 ~ 6
X = tf.placeholder(tf.float32, [None, 16])
Y = tf.placeholder(tf.int32, [None, 1]) # 0 ~ 6
One hot 방법¶
이 예제에서는 0~6으로 표현된 종의 종류를 Matrix 형태로 바꿔주는 방법이다.
0 : [0, 0, 0, 0, 0, 0, 0]
1 : [0, 0, 0, 0, 0, 0, 1]
2 : [0, 0, 0, 0, 0, 1, 0]
3 : [0, 0, 0, 0, 1, 0, 0]
... 생략그런데 tensorflow 에 내장되어있는 one_hot 을 사용할 경우 dimension 이 증가한다.
- 이 증가한 dimension 은 reshape 모듈을 이용하여 원래의 dimension 으로 바꿔준다.
In [2]:
Y_one_hot = tf.one_hot(Y, nb_classes) # one hot
print("one_hot", Y_one_hot) # one_hot은 N 차원을 받으면 N+1 차원을 return 함
Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes])
print("reshape", Y_one_hot)
W, b 의 shape 결정¶
- X W = Y 가 되야 하므로 (None x 16) * (16 x 7) = (None x 7)
In [3]:
W = tf.Variable(tf.random_normal([16, nb_classes]), name='weight')
b = tf.Variable(tf.random_normal([nb_classes]), name='bias')
with_logits 을 이용한 cost 계산¶
In [4]:
# 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
cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=logits,
labels=Y_one_hot)
cost = tf.reduce_mean(cost_i)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
Session 돌리기¶
In [5]:
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))
# Launch graph
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(2000):
sess.run(optimizer, 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: {:5}\tLoss: {:.3f}\tAcc: {:.2%}".format(
step, loss, acc))
# 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
for p, y in zip(pred, y_data.flatten()):
print("[{}] Prediction: {} True Y: {}".format(p == int(y), p, int(y)))
전체 코드¶
In [ ]:
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('./static/data-04-zoo.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]
print(x_data.shape, y_data.shape)
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) # one_hot은 N 차원을 받으면 N+1 차원을 return 함
Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes])
print("reshape", Y_one_hot)
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
cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=logits,
labels=Y_one_hot)
cost = tf.reduce_mean(cost_i)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
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))
# Launch graph
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(2000):
sess.run(optimizer, 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: {:5}\tLoss: {:.3f}\tAcc: {:.2%}".format(
step, loss, acc))
# 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
for p, y in zip(pred, y_data.flatten()):
print("[{}] Prediction: {} True Y: {}".format(p == int(y), p, int(y)))
'Dev > 딥러닝' 카테고리의 다른 글
07-2. Tensor Flow 에서 Data preprocessing (normalization) 구현 (0) | 2018.08.22 |
---|---|
07-1. Tensor Flow 에서 Learning rate 이란 (0) | 2018.08.22 |
06-1. Tensor Flow 로 3종류 이상 Classfication (Soft max Classifier) (0) | 2018.08.20 |
05. Tensor Flow 로 Classification 예제 (binary) (0) | 2018.08.17 |
04-2. Tensor Flow 에서 csv 파일 읽기 및 queue 사용법 (0) | 2018.08.17 |
댓글