다항 분류
Fashion MNIST
불러오기
import tensorflow as tf
(x_train, y_train), (x_test, y_test) = \
tf.keras.datasets.fashion_mnist.load_data()
from PIL import Image
Image.fromarray(x_train[0]).resize((200, 200))
전처리
x_train = x_train / 127.5 - 1
x_test = x_test / 127.5 - 1
- 이미지 데이터는 0-255로 밝기를 표현
- 신경망에서 사용하는 시그모이드 등의 함수는 지나치게 큰 값이 들어오면 경사가 0에 가까워져서 학습이 잘 되지 않음
- $[-1, +1]$ 범위로 재조정
모형
from tensorflow.keras.layers import *
model = tf.keras.Sequential([
Flatten(),
Dense(10, activation='softmax')
])
소프트맥스 함수
tf.nn.softmax([-1. , 0.5, 2. ])
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0.03911257, 0.17529039, 0.785597 ], dtype=float32)>
시그모이드 vs. 소프트맥스
시그모이드
x = 1.0
tf.nn.sigmoid(x)
<tf.Tensor: shape=(), dtype=float32, numpy=0.7310586>
소프트맥스
tf.nn.softmax([0, x])
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.26894143, 0.7310586 ], dtype=float32)>
훈련
설정
model.compile(
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
훈련
model.fit(x_train, y_train, epochs=100, validation_split=0.1,
callbacks=[tf.keras.callbacks.EarlyStopping(patience=2)])
Epoch 1/100 1688/1688 [==============================] - 2s 946us/step - loss: 0.5530 - accuracy: 0.8067 - val_loss: 0.4958 - val_accuracy: 0.8222 Epoch 2/100 1688/1688 [==============================] - 1s 839us/step - loss: 0.4627 - accuracy: 0.8386 - val_loss: 0.4651 - val_accuracy: 0.8325 Epoch 3/100 1688/1688 [==============================] - 1s 788us/step - loss: 0.4448 - accuracy: 0.8443 - val_loss: 0.4562 - val_accuracy: 0.8380 Epoch 4/100 1688/1688 [==============================] - 1s 801us/step - loss: 0.4351 - accuracy: 0.8481 - val_loss: 0.4623 - val_accuracy: 0.8368 Epoch 5/100 1688/1688 [==============================] - 1s 710us/step - loss: 0.4277 - accuracy: 0.8501 - val_loss: 0.4601 - val_accuracy: 0.8345
<keras.callbacks.History at 0x232dd3ee0b0>
평가
정확도
model.evaluate(x_test, y_test)
313/313 [==============================] - 0s 781us/step - loss: 0.4857 - accuracy: 0.8243
[0.48574209213256836, 0.8242999911308289]
예측
prob = model.predict(x_test)
313/313 [==============================] - 0s 547us/step
가장 확률이 높은 카테고리
y_pred = prob.argmax(axis=1)
혼동행렬
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_pred)
array([[658, 4, 17, 67, 4, 0, 242, 0, 8, 0], [ 1, 963, 4, 22, 3, 0, 6, 0, 1, 0], [ 8, 6, 696, 18, 120, 0, 143, 0, 9, 0], [ 10, 22, 7, 860, 10, 1, 85, 0, 5, 0], [ 0, 3, 108, 61, 687, 0, 133, 0, 8, 0], [ 0, 0, 0, 1, 0, 943, 2, 35, 4, 15], [ 69, 2, 109, 52, 85, 0, 665, 0, 18, 0], [ 0, 0, 0, 0, 0, 60, 0, 917, 0, 23], [ 2, 1, 7, 8, 2, 6, 35, 7, 932, 0], [ 0, 0, 0, 0, 0, 27, 0, 50, 1, 922]], dtype=int64)