MNIST
모듈 임포트
import tensorflow as tf
from tensorflow.keras.datasets.mnist import load_data
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Input, Flatten
from tensorflow.keras.utils import to_categorical, plot_model
import numpy as np
import tensorflow as tf
from keras.models import load_model
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
데이터 로드 및 전처리
- MNIST 데이터셋을 로드
- Train Data 중, 30%를 검증 데이터(validation data)로 사용
#고정된 난수 생성을 위해 tf.random.set_seed(111)를 설정합니다.
tf.random.set_seed(111)
(x_train_full, y_train_full), (x_test, y_test) =load_data(path='mnist.npz')
x_train, x_val, y_train, y_val = train_test_split(x_train_full, y_train_full,
test_size=0.3,
random_state=111)
#mnist.npz 파일로부터 훈련, 테스트 데이터셋을 불러옵니다.
num_x_train=(x_train.shape[0])
num_x_val=(x_val.shape[0])
num_x_test=(x_test.shape[0])
num_sample=5
random_idxs=np.random.randint(60000, size=num_sample)
plt.figure(figsize=(15,3))
for i , idx in enumerate(random_idxs):
img=x_train_full[idx,:]
label= y_train_full[idx]
plt.subplot(1,len(random_idxs),i+1)
plt.imshow(img)
plt.title('Index:{},Label:{}'.format(idx,label))
#훈련, 검증, 테스트 데이터셋의 픽셀 값을 255로 나누어 정규화합니다.
x_train=x_train/255.
x_val = x_val/255.
x_test=x_test/255.
모델 구성(Sequential)
#신경망 구성
model = Sequential([Input(shape=(28,28),name='input'),
Flatten(input_shape=[28,28], name='flatten'),
Dense(100,activation='relu', name='dense1'),
Dense(64, activation='relu', name='dense2'),
Dense(32, activation='relu', name='dense3'),
Dense(10, activation='softmax', name='output')])
#원핫인코딩
y_train = to_categorical(y_train)
y_val = to_categorical(y_val)
y_test = to_categorical(y_test)
모델 컴파일 및 학습
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=50, batch_size=128, validation_data=(x_val, y_val))
history_dict=history.history
loss= history_dict['loss']
val_loss= history_dict['val_loss']
epochs=range(1,len(loss)+1)
fig=plt.figure(figsize=(12,5))
ax1=fig.add_subplot(1,2,1)
ax1.plot(epochs, loss, color='b', label='train_loss')
ax1.plot(epochs, val_loss, color='r', label='val_loss')
ax1.set_xlabel('Epochs')
ax1.set_ylabel('Loss')
ax1.grid()
ax1.legend()
accuracy=history_dict['accuracy']
val_accuracy=history_dict['val_accuracy']
ax2=fig.add_subplot(1,2,2)
ax2.plot(epochs, accuracy, color='b', label='train_accuracy')
ax2.plot(epochs, val_accuracy, color='r', label='val_accuracy')
ax2.set_xlabel('Epochs')
ax2.set_ylabel('accuracy')
ax2.grid()
ax2.legend()
모델 평가 및 예측
model.evaluate(x_test, y_test)
pred_ys=model.predict(x_test)
print(pred_ys.shape)
np.set_printoptions(precision=7)
print(pred_ys[0])
arg_pred_y = np.argmax(pred_ys, axis=1)
plt.imshow(x_test[11])
plt.title('Predicted label:{}'.format(arg_pred_y[11]))
plt.show()
from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns
sns.set(style='white')
plt.figure(figsize=(8,8))
cm = confusion_matrix(np.argmax(y_test, axis=1), np.argmax(pred_ys, axis=-1))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.xlabel('Predicted Label')
plt.ylabel('True Lable')
plt.show()