다층 퍼셉트론 신경망 모델 만들어보기
# 1. 라이브러리 로딩하기
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.utils.vis_utils import model_to_dot
from IPython.display import SVG
%matplotlib inline
# 2. seed number 고정하기
np.random.seed(5)
# 3. pima Indians Diabetes data 불러오기
pima = np.loadtxt("d:/keras/pima_indians_diabetes.txt", delimiter = ",")
# 4. 데이터셋 생성하기
x_train = pima[:700, 0:8]
y_train = pima[:700, 8]
x_test = pima[700:, 0:8]
y_test = pima[700:, 8]
# 5. 모델 구성하기
model = Sequential()
model.add(Dense(input_dim = 8, output_dim = 12, activation = "relu"))
model.add(Dense(input_dim = 12, output_dim = 8, activation = "relu"))
model.add(Dense(input_dim = 8, output_dim = 1, activation = "sigmoid"))
# 6. 모델 출력하기
SVG(model_to_dot(model, show_shapes = True).create(prog = "dot", format = "svg"))
# 7. 모델 학습과정 설정하기
model.compile(loss = "binary_crossentropy", optimizer = "adam", metrics = ["accuracy"])
# 8. 모델 학습시키기
model.fit(x_train, y_train, epochs = 1500, batch_size = 64)
# 9. 모델 평가하기
scores = model.evaluate(x_test, y_test)
print("%s: %.2f%%" %(model.metrics_names[1], scores[1]*100))
[출처] 블록과 함께하는 파이썬 딥러닝 케라스, 김태영 지음, DigitalBooks, p104~111