from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
(X_train, Y_train), (X_validation, Y_validation) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 784).astype('float64') / 255
X_validation = X_validation.reshape(X_validation.shape[0], 784).astype('float64') / 255
Y_train = np_utils.to_categorical(Y_train, 10)
Y_validation = np_utils.to_categorical(Y_validation, 10)
model = Sequential()
model.add(Dense(512, input_dim=784, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
hist = model.fit(X_train, Y_train, validation_data=(X_validation, Y_validation),
epochs=30, batch_size=500)
print('\nAccuracy: {:.4f}'.format(model.evaluate(X_validation, Y_validation)[1]))
import matplotlib.pyplot as plt
fig, loss_ax = plt.subplots()
acc_ax = loss_ax.twinx()
loss_ax.plot(hist.history['loss'], 'y', label='train loss')
loss_ax.plot(hist.history['val_loss'], 'r', label='val loss')
loss_ax.set_xlabel('epoch')
loss_ax.set_ylabel('loss')
loss_ax.legend(loc='upper left')
acc_ax.plot(hist.history['acc'], 'b', label='train acc')
acc_ax.plot(hist.history['val_acc'], 'g', label='val acc')
acc_ax.set_ylabel('accuracy')
acc_ax.legend(loc='upper left')
plt.show()
'AI' 카테고리의 다른 글
딥러닝 개론. 시그모이드 함수와 zigzag 문제 (0) | 2021.11.19 |
---|---|
선행대수-인공지능을 위한- 스탠포드-강의록 등 (0) | 2021.09.20 |
fmincon 연습 (0) | 2021.09.19 |
회귀분석 연습 프로그램 (0) | 2021.09.18 |
prolog 연습 (0) | 2021.09.04 |