2-6 훈민정음의 자음 classification TensorFlow Softmax 예제
훈민정음의 4개의 자음을 학습(train)시켜 보자. 4개의 자음에 대한 이미지 구성은 3X3 매트릭스로 가능하다. 더욱 많은 자음과 모음을 분류(calssification)하려면 적어도 5X5 또는 그 이상으로 해상도를 높여야 할 것이다. 라벨에 해당하는 One Hot Code 는 그림을 보면 쉽게 이해가 될 것이다. 라벨 0에 해당하는 첫 번째 “ㄱ”에 대한 입력 데이터를 작성해 보자.
검은색은 0 흰색은 1로 코딩한다.
“ㄱ”⟶ [ 0, 0, 0, 1, 1, 0, 1, 1, 0 ],
[ 0, 0, 1, 1, 0, 1, 1, 0, 1 ].
[ 1, 0, 0, 1, 1, 0, 1, 1, 0 ],
[ 1, 1, 1, 0, 0, 1, 1, 0, 1 ]
“ㄴ“⟶ [ 0, 1, 1, 0, 1, 1, 0, 0, 0 ],
[ 0, 1, 1, 0, 1, 1, 0, 0, 1 ],
[ 1, 1, 1, 0, 1, 1, 0, 0, 0 ],
[ 1, 1, 1, 1, 0, 1, 1, 0, 0 ]
“ㄷ”⟶ [ 0, 0, 0, 0, 1, 1, 0, 0, 0 ],
[ 0, 0, 1, 0, 1, 1, 0, 0, 1 ],
[ 1, 0, 0, 1, 0, 1, 1, 0, 0 ],
[ 0, 0, 1, 0, 1, 1, 0, 0, 0 ]
“ㅁ”⟶ [ 0, 0, 0, 0, 1, 0, 0, 0, 0 ],
[ 1, 0, 0, 0, 1, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 1, 0, 1, 0, 0 ],
[ 0, 0, 0, 0, 1, 0, 0, 0, 1 ]
이 데이터들은 TensorFlow 코드에서 x_data로 입력하고 one hot code는 y_data로 입력한다. 마지막 컬럼의 Test 용 데이터는 Session에서 사용하기로 한다.
“ㄱ”⟶ [ 0, 0, 0, 1, 1, 0, 1, 0, 1 ]
“ㄴ“⟶ [ 0, 1, 1, 0, 1, 1, 0, 0, 1 ]
“ㄷ”⟶ [ 0, 0, 0, 0, 1, 1, 0, 0, 1 ]
“ㅁ”⟶ [ 0, 0, 1, 0, 1, 0, 0, 0, 1 ]
X의 샘플수는 4개이지만 None으로 하고 각 샘플의 데이터 수는 9 이다.
one code를 저장하는 Y는 4개이지만 None으로 하고 각 각각의 데이터 수는 4 이다.
클라스의 수는 라벨의 수에 해당하는 4이다.
웨이트 W는 X의 데이타수 9와 클라스의 수 4에 맞춰 배열을 선언한다.
바이아스 b는 클라스의 수 4로 두자.
아래 그래프는 cost 함수의 감소 패턴이다.
다음의 결과는 테스트용 데이터에 대한 softmax 명령에 의한 계산 결과로서 one hot code의 1 위치에 대응하는 지점의 가장 확률값이 높은 hypothesis 값을 보여준다. 그 뒤의 브라켓 속의 값을 살펴보면 학습결과에 맞춰 정확히 인식했음을 알 수 있다.
주의할 점은 iteration 횟수가 지나치게 적으면 잘못 인식할 수도 있다. iteration 횟수 100일 때에 오답이 나옴을 확인해보도록 하자.
matplotlib를 이용해 학습용 이미지를 출력해 보자.
아래의 코드를 복사해서 실행할 경우 혹 indentation 이 잘못되어 에러가 검출되면 2018년 8월 10일의 AS 내용을 참조하기 바란다. 실제 편집과정에서 paste 하는 순간에 indentation 이 사라지는군요. 오호통재라 누굴 원망하랴! indentation 위치 AS 꼭 보시고 수정작업하세요.
#softmax_classifier_9data_hunmin_02.py
#Softmax Classifier
from matplotlib import pyplot as plt
import numpy as np
import tensorflow as tf
tf.set_random_seed(777) # for reproducibility
def gen_image(arr):
t_d = np.reshape(arr, (3, 3))
two_d = (np.reshape(arr, (3, 3)) * 255).astype(np.uint8)
print(two_d)
plt.imshow(two_d, interpolation='nearest')
plt.savefig('batch.png')
return plt
x_data = [[ 0, 0, 0, 1, 1, 0, 1, 1, 0 ],
[ 0, 0, 1, 1, 0, 1, 1, 0, 1 ],
[ 1, 0, 0, 1, 1, 0, 1, 1, 0 ],
[ 1, 1, 1, 0, 0, 1, 1, 0, 1 ],
[ 0, 1, 1, 0, 1, 1, 0, 0, 0 ],
[ 0, 1, 1, 0, 1, 1, 0, 0, 1 ],
[ 1, 1, 1, 0, 1, 1, 0, 0, 0 ],
[ 1, 1, 1, 1, 0, 1, 1, 0, 0 ],
[ 0, 0, 0, 0, 1, 1, 0, 0, 0 ],
[ 0, 0, 1, 0, 1, 1, 0, 0, 1 ],
[ 1, 0, 0, 1, 0, 1, 1, 0, 0 ],
[ 0, 0, 1, 0, 1, 1, 0, 0, 0 ],
[ 0, 0, 0, 0, 1, 0, 0, 0, 0 ],
[ 1, 0, 0, 0, 1, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 1, 0, 1, 0, 0 ],
[ 0, 0, 0, 0, 1, 0, 0, 0, 1 ]
]
y_data = [ [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0],
[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0],
[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0],
[0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1]
]
X = tf.placeholder("float", [None, 9])
Y = tf.placeholder("float", [None, 4])
nb_classes = 4
W = tf.Variable(tf.random_normal([9, 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:
for idx in range(16):
output = x_data[idx]
gen_image(output).show()
sess.run(tf.global_variables_initializer())
for step in range(2001):
sess.run(optimizer, feed_dict={X: x_data, Y: y_data})
if step % 200 == 0:
print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}))
print('--------------')
# Testing & One-hot encoding
a = sess.run(hypothesis, feed_dict={X: [[ 0, 0, 0, 1, 1, 0, 1, 0, 1 ]]})
print(a, sess.run(tf.argmax(a, 1)))
print('--------------')
bb = sess.run(hypothesis, feed_dict={X: [[ 0, 1, 1, 0, 1, 1, 0, 0, 1 ]]})
print(bb, sess.run(tf.argmax(bb, 1)))
print('--------------')
c = sess.run(hypothesis, feed_dict={X: [[ 0, 0, 0, 0, 1, 1, 0, 0, 1 ]]})
print(c, sess.run(tf.argmax(c, 1)))
print('--------------')
d = sess.run(hypothesis, feed_dict={X: [[ 0, 0, 1, 0, 1, 0, 0, 0, 1 ]]})
print(d, sess.run(tf.argmax(d, 1)))
print('--------------')
all = sess.run(hypothesis, feed_dict={
X: [[ 0, 0, 0, 1, 1, 0, 1, 0, 1 ],[ 0, 1, 1, 0, 1, 1, 0, 0, 1 ],
[ 0, 0, 0, 0, 1, 1, 0, 0, 1 ],[ 0, 0, 1, 0, 1, 0, 0, 0, 1 ]]})
print(all, sess.run(tf.argmax(all, 1)))
pairplay 가 kr-dev 컨텐츠를 응원합니다! :)