텐서플로 및 케라스 모듈 설정

import numpy as np
from matplotlib import pyplot as plt
from tensorflow.keras.datasets import mnist
from tensorflow.keras import models, layers
from tensorflow.keras.utils import to_categorical

데이터 설정

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
check_image = test_images[24]
checking_img = check_image.reshape((1, 28*28))
train_images = train_images.reshape((60000, 28*28))
train_images = train_images.astype('float32')/255
test_images = test_images.reshape((10000, 28*28))
test_images = test_images.astype('float32')/255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

모델 구성

model = models.Sequential()
model.add(layers.Dense(512, activation='relu', input_shape=(28*28, )))
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))

모델 컴파일

model.compile(optimizer = 'rmsprop',
              loss = 'categorical_crossentropy',
              metrics = ['accuracy'])

모델 학습

model.fit(train_images, train_labels, epochs=5, batch_size=128)

모델 평가

test_loss, test_acc = model.evaluate(test_images, test_labels)

모델 예측

output = model.predict(checking_img)
반응형

+ Recent posts