📢 학습목표
- 손글씨 데이터를 분류하는 딥러닝 모델링을 진행해보자
- 다중분류 딥러닝 모델링을 설계하자
- 이미지 데이터 기본 정보에 대해서 확인하자
라이브러리 불러오기
# 기본 라이브러리 불러오기
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
데이터 불러오기
# 데이터 불러오기
from tensorflow.keras.datasets import mnist
# 훈련용 데이터, 테스트용 데이터 구분해서 저장해둠 -> 그대로 변수에 담아주기
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train[0] ndarray (28, 28) hide data
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
...
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
18, 18, 18, 126, 136, 175, 26, 166, 255, 247, 127, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 30, 36, 94, 154, 170,
253, 253, 253, 253, 253, 225, 172, 253, 242, 195, 64, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 49, 238, 253, 253, 253, 253,
253, 253, 253, 253, 251, 93, 82, np.unique(y_train)
# array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=uint8)MLP 모델 설계
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Input, Dense, Flatten
# 모델 설계
model = Sequential()
# input layer
model.add(Input(shape=(28,28)))
# hidden layer
model.add(Flatten()) # 2차원 사진 데이터 입력받아 1차원으로 변환
model.add(Dense(units=16, activation = 'relu'))
model.add(Dense(units=8, activation = 'relu'))
model.add(Dense(units=16, activation = 'relu'))
# output layer
model.add(Dense(units=10, activation = 'softmax'))
units : 노드 수 = class 종류 개수activation : 활성화 함수는 보통 ‘softmax’ 사용학습 방법 및 평가 방법 설계
model.compile(loss='sparse_categorical_crossentropy',
optimizer='SGD',
metrics=['accuracy'])
loss : sparse_categorical_crossentropy 사용학습
model.fit(X_train, y_train, validation_split=0.2, epochs=20, batch_size=64)
시각화
# 시각화
plt.figure(figsize=(5,3))
plt.plot(model.history.history['loss'], label='loss')
plt.plot(model.history.history['val_loss'], label='val_loss')
plt.legend()