C.W.K.
Stream
Lesson 05 of 07 · published

MNIST classifier 빌드

~8 min · sequential

Level 0Keras 도제
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

전체 파이프라인이 한 화면에

고전 deep-learning hello-world: MNIST 손글씨 0–9 분류. 가치 있는 건 데이터셋이 아니라, Keras 전체 workflow 가 한 화면에 들어온다는 점 — 앞으로 짤 모든 프로젝트가 이 여섯 단계를 같은 순서로 재사용해 (전체 스크립트는 Code 섹션).

  1. Loadkeras.datasets.mnist.load_data() 가 train/test 를 NumPy array 로 바로 줘. 다운로드 배관 신경 안 써도 돼.
  2. Normalize — pixel 을 255 로 나눠 [0, 1] 로. 빼먹으면 학습이 느리거나 불안정해 — 제일 흔한 누락이야.
  3. Build — 앞 lesson 의 Sequential stack: Flatten → Dense(relu) → Dropout → Dense(softmax).
  4. Compile — optimizer (adam), loss (sparse_categorical_crossentropy, label 이 그냥 정수라서), 지켜볼 metric 연결.
  5. Trainfit()validation_split 줘서 암기 말고 일반화를 봐.
  6. Evaluate — 학습에 안 쓴 test set 으로 채점.

5 epoch 면 test accuracy 97-98%. 4단계 loss 선택이 미묘한 함정 — sparse_categorical_crossentropy3 같은 정수 label 을 기대하고, categorical_crossentropy[0,0,0,1,0,...] 같은 one-hot 을 기대해. 잘못 고르면 Keras 가 shape 에러 — exercise 에서 일부러 터뜨려 볼 바로 그 실패 모드야.

Code

MNIST classifier 전체: load → normalize → build → compile → fit → evaluate·python
import keras
from keras import layers

# 1. Load data
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# 2. Normalize pixel values to [0, 1]
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0

# 3. Build model
model = keras.Sequential([
    keras.Input(shape=(28, 28)),
    layers.Flatten(),
    layers.Dense(128, activation="relu"),
    layers.Dropout(0.2),
    layers.Dense(10, activation="softmax"),
])

# 4. Compile
model.compile(
    optimizer="adam",
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"],
)

# 5. Train
model.fit(x_train, y_train, epochs=5, validation_split=0.1)

# 6. Evaluate
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Test accuracy: {test_acc:.4f}")  # ~97.5%

External links

Exercise

위 MNIST classifier 구현해서 test accuracy 97% 이상. 이제 일부러 망가뜨려 — 'categorical_crossentropy' (잘못) 로 바꿔서 에러 봐. 실패 모드 안에 lesson 이 있어.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.