48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
import tensorflow as tf
|
|
from tensorflow.keras import layers, models
|
|
import numpy as np
|
|
import time
|
|
|
|
# 모델 평가 시작 시간 기록
|
|
start_time = time.time()
|
|
|
|
# 데이터셋 로드 및 전처리
|
|
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
|
|
|
|
# 데이터 정규화 및 차원 확장
|
|
x_train = x_train / 255.0
|
|
x_test = x_test / 255.0
|
|
x_train = np.expand_dims(x_train, -1)
|
|
x_test = np.expand_dims(x_test, -1)
|
|
|
|
# 간단한 CNN 모델 정의
|
|
model = models.Sequential([
|
|
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
|
|
layers.MaxPooling2D((2, 2)),
|
|
layers.Conv2D(64, (3, 3), activation='relu'),
|
|
layers.MaxPooling2D((2, 2)),
|
|
layers.Flatten(),
|
|
layers.Dense(64, activation='relu'),
|
|
layers.Dense(10, activation='softmax')
|
|
])
|
|
|
|
# 모델 컴파일
|
|
model.compile(optimizer='adam',
|
|
loss='sparse_categorical_crossentropy',
|
|
metrics=['accuracy'])
|
|
|
|
# 모델 요약
|
|
model.summary()
|
|
|
|
# 모델 훈련
|
|
model.fit(x_train, y_train, epochs=500, batch_size=256, validation_split=0.2)
|
|
|
|
# 모델 평가
|
|
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
|
|
print(f"Test accuracy: {test_acc:.2f}")
|
|
|
|
# 모델 평가 종료 시간 기록 및 출력
|
|
end_time = time.time()
|
|
print(f"Evaluation time: {end_time - start_time:.2f} seconds")
|
|
|