케라스 강좌는 아래의 주소가 정말 섬세하고 막강합니다.
https://tykimos.github.io/lecture/
파이썬의 딥러닝 라이브러리의 최근 동향입니다.
기본적인 용어에 익숙해 져야 합니다.
훈련셋, 검증셋, 시험셋
수능에 비유하면
모의고사 5회분: 훈련셋
작년 수능 믄제: 시험셋
학생 3명: 모델 3개
훈련셋을 몇 번 반복해서 학습할 것인가를 정하는 것이 에포크(epochs)라고 합니다.
학습이 아직 덜 된 상태에서 학습을 더 하면 성능이 높아질 가능성이 있습니다. 이를 언더피팅(underfitting)이라고 합니다.
배치사이즈와 에포크
케라스에서 만든 모델을 학습할 때 fit()함수를 사용합니다.
model.fit(x, y, batch_size=32, epochs=10)
x: 입력 데이터
y: 라벨값
batch_size: 몇 개의 샘플로 가중치를 갱신할 것인지 지정
epochs: 학습 반복 횟수
히스토리 기능 사용하기
매 에포크 마다 훈련 손실값(loss)
매 에포크 마다 훈련 정확도(acc)
매 에포크 마다 검증 손실값(val_loss)
매 에포크 마다 검증 정확도(val_acc)
from keras.utils import np_utils
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Activation
import keras
import numpy as np
np.random.seed(3)
# 1. 데이터셋 준비하기
# 훈련셋과 시험셋 로딩
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
# 훈련셋과 검증셋 분리
X_val = X_train[50000:]
Y_val = Y_train[50000:]
X_train = X_train[:50000]
Y_train = Y_train[:50000]
X_train = X_train.reshape(50000, 784).astype('float32') / 255.0
X_val = X_val.reshape(10000, 784).astype('float32') / 255.0
X_test = X_test.reshape(10000, 784).astype('float32') / 255.0
# 훈련셋, 검증셋 고르기
train_rand_idxs = np.random.choice(50000, 700)
val_rand_idxs = np.random.choice(10000, 300)
X_train = X_train[train_rand_idxs]
Y_train = Y_train[train_rand_idxs]
X_val = X_val[val_rand_idxs]
Y_val = Y_val[val_rand_idxs]
# 라벨링 전환
Y_train = np_utils.to_categorical(Y_train)
Y_val = np_utils.to_categorical(Y_val)
Y_test = np_utils.to_categorical(Y_test)
# 2. 모델 구성하기
model = Sequential()
model.add(Dense(units=2, input_dim=28*28, activation='relu'))
model.add(Dense(units=10, activation='softmax'))
# 3. 모델 엮기
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
# 4. 모델 학습시키기
tb_hist = keras.callbacks.TensorBoard(log_dir='./graph', histogram_freq=0, write_graph=True, write_images=True)
model.fit(X_train, Y_train, epochs=1000, batch_size=10, validation_data=(X_val, Y_val), callbacks=[tb_hist])
위의 코드를 실행해서 텐서보드로 확인해 봅니다.
tensorboard --logdir=c:\work\graph