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

Functional 과 Subclass 섞기

~8 min · subclass

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

Functional model 안에 Subclass layer 넣어도 OK. 정 반대로 Subclass model 의 __init__ 에서 Functional submodel 생성도 OK. 한 model 안에 둘이 섞여 있어도 Keras 가 알아서 처리.

실전 예: backbone (pretrained ResNet, Functional) + custom head (Subclassed). 또는 Subclassed Transformer block 을 Functional model 의 layer 로 사용. 둘의 강점이 보완 — Functional 의 introspection + Subclass 의 자유.

Code

# Custom layer
class <span class="dc">ResidualBlock</span>(keras.layers.Layer):
    def __init__(self, filters, **kwargs):
        super().__init__(**kwargs)
        self.dense1 = layers.Dense(filters, activation="relu")
        self.dense2 = layers.Dense(filters)
        self.add_layer = layers.Add()
        self.activation = layers.Activation("relu")

    def call(self, inputs):
        x = self.dense1(inputs)
        x = self.dense2(x)
        x = self.add_layer([x, inputs])
        return self.activation(x)

# Use custom layer in Functional API
inputs = keras.Input(shape=(64,))
x = layers.Dense(64, activation="relu")(inputs)
x = ResidualBlock(64)(x)  # Custom layer used like any built-in
x = ResidualBlock(64)(x)
outputs = layers.Dense(10, activation="softmax")(x)
model = keras.Model(inputs, outputs)

External links

Exercise

하이브리드 빌드 — Functional model 안에 Subclassed layer 한 개 (예: custom MultiHeadAttention) 포함. model.summary() 가 둘 다 명확히 보이는지 확인.

Progress

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

댓글 0

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

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