from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
## 데이터 전처리
from tensorflow.keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
## 모델 생성
model_dnn = tf.keras.Sequential([
tf.keras.layers.Input(shape=(X_train.shape[1],)),
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model_dnn = Sequential()
model_dnn.add(Dense(16, activation='relu', input_shape=(18,)))
model_dnn.add(Dense(128, activation='relu'))
model_dnn.add(Dense(1, activation='sigmoid'))
## 데이터 reshape 및 정규화
train_images = train_images.reshape((60000, 28, 28, 1)) / 255.0
test_images = test_images.reshape((10000, 28, 28, 1)) / 255.0
## 모델 요약
model_dnn.summary()
## 모델 컴파일
model_dnn.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
## 모델 학습
model_dnn.fit(X_train, y_train, epochs=100, batch_size=1, verbose=1)
# 모델 학습과정 그래프로 나타내기
plt.plot(history.history['accuracy'], label='accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0, 1])
plt.legend(loc='lower right')
plt.show()
## 모델 예측
preds = model.predict(x_test)
preds[0]
####
probability_model = tf.keras.Sequential([model,
tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
predictions[0] # 첫번째 이미지으 예측값
np.argmax(predictions[0]) # 가장 높은 신뢰도 값을 가진 레이블
### 예측을 모두 그래프로 받는 함수
def plot_image(i, predictions_array, true_label, img):
true_label, img = true_label[i], img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.imshow(img, cmap=plt.cm.binary)
predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'blue'
else:
color = 'red'
plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
100*np.max(predictions_array),
class_names[true_label]),
color=color)
def plot_value_array(i, predictions_array, true_label):
true_label = true_label[i]
plt.grid(False)
plt.xticks(range(10))
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array, color="#777777")
plt.ylim([0, 1])
predicted_label = np.argmax(predictions_array)
thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue')
### 예측 확인
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i], test_labels)
plt.show()
## 모델 평가
model_dnn.evaluate(X_test, y_test)
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print('\\nTest accuracy:', test_acc)
## 모델 저장
model_dnn.save('model_dnn.h5')