다항 분류
Fashion MNIST
불러오기
import tensorflow as tf
(x_train, y_train), (x_test, y_test) = \
tf.keras.datasets.fashion_mnist.load_data()
전처리
x_train = x_train / 255
x_test = x_test / 255
- 이미지 데이터는 0-255로 밝기를 표현
- 신경망에서 사용하는 시그모이드 등의 함수는 지나치게 큰 값이 들어오면 경사가 0에 가까워져서 학습이 잘 되지 않음
- 255로 나눠서 0-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,
optimizer=tf.keras.optimizers.Adam(),
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 [==============================] - 1s 716us/step - loss: 0.6092 - accuracy: 0.7953 - val_loss: 0.4744 - val_accuracy: 0.8382 Epoch 2/100 1688/1688 [==============================] - 1s 612us/step - loss: 0.4664 - accuracy: 0.8416 - val_loss: 0.4627 - val_accuracy: 0.8413 Epoch 3/100 1688/1688 [==============================] - 1s 625us/step - loss: 0.4391 - accuracy: 0.8499 - val_loss: 0.4335 - val_accuracy: 0.8493 Epoch 4/100 1688/1688 [==============================] - 1s 625us/step - loss: 0.4256 - accuracy: 0.8532 - val_loss: 0.4350 - val_accuracy: 0.8473 Epoch 5/100 1688/1688 [==============================] - 1s 642us/step - loss: 0.4164 - accuracy: 0.8581 - val_loss: 0.4098 - val_accuracy: 0.8592 Epoch 6/100 1688/1688 [==============================] - 1s 624us/step - loss: 0.4096 - accuracy: 0.8583 - val_loss: 0.4167 - val_accuracy: 0.8560 Epoch 7/100 1688/1688 [==============================] - 1s 646us/step - loss: 0.4040 - accuracy: 0.8604 - val_loss: 0.4218 - val_accuracy: 0.8518
<keras.callbacks.History at 0x1f4167de2f0>
평가
정확도
model.evaluate(x_test, y_test)
313/313 [==============================] - 0s 810us/step - loss: 0.4535 - accuracy: 0.8431
[0.4534875452518463, 0.8431000113487244]
예측
prob = model.predict(x_test)
313/313 [==============================] - 0s 693us/step
가장 확률이 높은 카테고리
y_pred = prob.argmax(axis=1)
혼동행렬
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_pred)
array([[827, 1, 14, 72, 5, 0, 69, 0, 12, 0], [ 3, 952, 2, 35, 5, 0, 1, 0, 2, 0], [ 29, 5, 782, 19, 102, 1, 57, 0, 5, 0], [ 22, 7, 12, 914, 15, 0, 26, 0, 4, 0], [ 0, 0, 156, 68, 697, 0, 71, 0, 8, 0], [ 0, 0, 0, 1, 0, 937, 0, 46, 2, 14], [153, 4, 143, 71, 93, 0, 515, 0, 21, 0], [ 0, 0, 0, 0, 0, 43, 0, 938, 0, 19], [ 8, 1, 10, 14, 2, 6, 13, 5, 941, 0], [ 0, 0, 0, 0, 0, 20, 0, 51, 1, 928]], dtype=int64)