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

Sequential API — 선형 스택

~11 min · sequential, dense, dropout

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

실제 일을 하는 가장 간단한 model

Sequential API는 layer 목록이고, 각자 다음 layer로 넘겨줘. 표준 feedforward와 convolutional 네트워크엔 이게 맞는 선택 — 그리고 실전 classification 작업의 60%는 이걸로 충분해.

Input shape는 항상 명시적으로 선언해. 첫 항목에 keras.Input(shape=(...)) 넣으면 model이 즉시 완전히 build되고 model.summary() 호출해서 파라미터 수 바로 볼 수 있어. 없으면 첫 forward에서 lazily build되니 에러 감지가 늦어져.

Sequential 쓰면 안 되는 때: 분기 필요 (skip connection, residual block), 공유 layer (siamese network), 다중 입력 (image + metadata), 다중 출력 (multi-task head). 그땐 Functional API로 넘어가.

Code

Sequential — two patterns·python
import tensorflow as tf
from tensorflow import keras
from keras import layers

# Pattern 1: list to constructor (recommended)
model = keras.Sequential([
    keras.Input(shape=(784,)),
    layers.Dense(256, activation='relu'),
    layers.Dropout(0.3),
    layers.Dense(128, activation='relu'),
    layers.BatchNormalization(),
    layers.Dense(10),    # logits, no softmax (use from_logits=True in loss)
], name="mnist_classifier")

# Pattern 2: incremental .add()
model2 = keras.Sequential(name="mnist_v2")
model2.add(keras.Input(shape=(784,)))
model2.add(layers.Dense(256, activation='relu'))
model2.add(layers.Dense(10))

model.summary()
print(model.layers)               # list of Layer objects
print(model.get_layer('dense'))    # by name
model2.pop()                       # remove last layer

Exercise

MNIST용 Sequential MLP 만들기: Input(784) → Dense(256, relu) → Dropout(0.3) → Dense(128, relu) → Dense(10). model.summary() 출력하고 파라미터 수가 약 235K 근처인지 확인.

Progress

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

댓글 0

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

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