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

keras.Model subclass

~8 min · subclass

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

메서드 두 개, 역할 두 개

Subclass model 은 keras.Model 을 상속하는 Python class 인데, 일을 정확히 두 메서드로 나눠. __init__() 은 *부품 목록* — model 이 가질 layer 를 전부 인스턴스화해서 self 에 붙여. call() 은 *조립* — 입력 tensor 를 받아서 그 layer 들을 통과시켜 출력을 만들어. 그 다음은 Keras 가 가져가 — call() 만 정의해두면 인스턴스가 built-in model 처럼 동작해서 compile(), fit(), evaluate(), save() 다 그대로 돌아가 (Code block 참고).

이 분리가 왜 중요해

이 분리는 취향 문제가 아니야. __init__() 에서 만든 layer 는 model = MyModel() 쓰는 순간 *한 번* 생성되고, 그 weight 가 그때부터 추적돼 — Keras 가 뭘 최적화할지 아는 게 이래서야. call() 은 *매* forward 마다, batch 당 한 번 돌아. 실수로 layer 생성을 call() 안에 넣으면 매 step 마다 학습 안 된 새 weight 가 조용히 만들어지고, tensor 연산을 __init__() 에 넣으면 아직 연산할 tensor 자체가 없어. call() 이 받는 training 인자는 Dropout / BatchNormalization 이 train 모드와 inference 모드를 가르는 스위치 — 신경 쓰는 sub-layer 마다 빠짐없이 넘겨줘.

Code

최소 keras.Model subclass·python
import keras
from keras import layers

class MyModel(keras.Model):
    def __init__(self):
        super().__init__()
        self.dense1 = layers.Dense(128, activation="relu")
        self.dropout = layers.Dropout(0.3)
        self.dense2 = layers.Dense(10, activation="softmax")

    def call(self, inputs, training=False):
        x = self.dense1(inputs)
        x = self.dropout(x, training=training)
        return self.dense2(x)

model = MyModel()
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy")
model.fit(x_train, y_train, epochs=5)

External links

Exercise

MNIST classifier 를 keras.Model subclass 로 재구현. 첫 call() 전후 model.summary() 비교 — 차이 메모.

Progress

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

댓글 0

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

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