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

Layer 추가하는 법

~8 min · sequential

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

제일 먼저 손이 가는 layer 네 개

Sequential model 의 표현력은 결국 쌓는 layer 가 결정해. 일상 작업의 대부분은 이 핵심 네 개가 다 짊어져:

  • Dense(units, activation) — fully connected, 일꾼. 모든 입력이 모든 출력에 연결되고, units 가 출력 개수.
  • Dropout(rate) — *학습 때만* 입력 일부를 0 으로 떨궈서 overfitting 막아. inference 땐 no-op 이라 예측할 때 일부러 빼는 거 아니야.
  • Flatten() — 다차원 입력을 1D 로 펼쳐 (28×28 image → 784 벡터). vector 만 알아듣는 Dense 앞에 끼워주는 어댑터.
  • Activation(fn) — 비선형 적용. 단독으로 쓸 일은 드물고, 보통 Dense(64, activation="relu") 처럼 layer 에 접어 넣어.

첫 layer 만 특별해 — shape 가 필요

첫 layer 빼고는 전부 앞 layer 에서 입력 shape 를 *자동 추론* 해. 근데 *첫* layer 는 추론할 앞이 없으니 직접 알려줘야 해. 깔끔한 방법은 keras.Input(shape=(...)) 을 stack 의 첫 항목으로 박는 거. Keras 가 그 shape 를 알기 전엔 weight 를 할당 못 하고, 그래서 입력 선언 없는 model 은 'unbuilt' 상태로 남아 summary() 가 투덜대. 위에서 박아두면 데이터 한 배치 들어오기도 전에 model 이 완성돼 (전체 stack 은 Code 섹션).

Code

shape 주석 달린 4-layer stack·python
model = keras.Sequential([
    keras.Input(shape=(28, 28, 1)),  # 28x28 grayscale images
    layers.Flatten(),                       # → (784,)
    layers.Dense(128, activation="relu"),   # → (128,)
    layers.Dropout(0.2),                   # Regularization
    layers.Dense(10, activation="softmax"), # → (10,) probabilities
])

External links

Exercise

같은 MNIST classifier 를 두 방식으로 짜 — constructor list / model.add(). 둘이 똑같은 model.summary() 내는지 확인.

Progress

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

댓글 0

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

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