수치입력 다중클래스 분류모델 레시지(다층퍼셉트론 신경망 모델)
# 1. 사용할 패키지 불러오기
import numpy as np
import random
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import to_categorical
%matplotlib inline
# 2. 데이터 생성하기
x_train = np.random.random((1000, 12))
y_train = np.random.randint(2, size = (1000, 1))
y_train = to_categorical(y_train, num_classes = 10)
x_test = np.random.random((100, 12))
y_test = np.random.randint(2, size = (100, 1))
y_test = to_categorical(y_test, num_classes = 10)
# 3. 모델 구성하기
model = Sequential()
model.add(Dense(input_dim = 12, output_dim = 64, activation = "relu"))
model.add(Dense(input_dim = 64, output_dim = 10, activation = "softmax"))
# 4. 모델 학습과정 설정하기
model.compile(optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics = ['accuracy'])
# 5. 모델 학습시키기
hist = model.fit(x_train, y_train, epochs = 1000, batch_size = 64)
# 6. 학습과정 살펴보기
fig, loss_ax = plt.subplots()
acc_ax = loss_ax.twinx()
loss_ax.set_ylim([0.0, 3.0])
acc_ax.set_ylim([0.0, 1.0])
loss_ax.plot(hist.history["loss"], "yellow", label = "train loss")
acc_ax.plot(hist.history["acc"], "blue", label = "train accuracy")
loss_ax.set_xlabel("epoch")
loss_ax.set_ylabel("loss")
acc_ax.set_ylabel("accuracy")
loss_ax.legend(loc = "upper left")
acc_ax.legend(loc = "lower left")
plt.show()
# 7. 모델 평가하기
loss_and_metrics = model.evaluate(x_test, y_test, batch_size = 32)
print("loss_and_metrics : " + str(loss_and_metrics))
[출처] 블록과 함께하는 파이썬 딥러닝 케라스, 김태영 지음, DigitalBooks, p206~207