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

두 세계의 좋은 점만 가져가는 패턴

이 track 전체가 향해 온 한 수: custom layer 를 subclass 한 다음, Functional API 로 엮어. layer 는 안쪽에 필요한 Python 로직을 다 품고 있지만, 바깥에서 보면 tensor 받아 tensor 돌려주는 callable 일 뿐이야 — DenseConv2D 랑 구분이 안 돼. 그래서 Functional graph 안에서 built-in 처럼 그냥 불러 쓰면 돼 (Code block 참고).

seam 이 왜 매끄럽고, 왜 공짜인가

이게 깔끔하게 합쳐지는 이유는 layer 계약이야. 모든 Keras layer 는 built-in 이든 custom 이든 같은 인터페이스를 노출해: layer(tensor) -> tensor. Functional API 는 layer *안에서* 무슨 일이 일어나는지 신경 안 써 — 나온 tensor 를 다음 call 로 배선할 뿐이야. 그래서 skip connection 과 Python 로직을 가진 ResidualBlock 이 graph 에 끼워져도 주변 Functional 구조는 여전히 introspect 가능해: model.summary() 가 뼈대를 그려주고, custom block 의 내부만 불투명해져. lesson 1 에서 본 introspection 비용을 *그럴 만한 block 에만* 정확히 가두는 거야.

반대 방향도 돼: subclass Model__init__() 에서 Functional sub-model 을 만들어 call() 에서 부를 수 있어. 실전에서 흔한 모양은 pretrained Functional backbone (ResNet, vision transformer) 을 custom routing / loss 처리하는 얇은 subclass head 가 감싸는 형태야.

Code

Functional model 안에서 쓰는 subclass ResidualBlock·python
# Custom layer
class ResidualBlock(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

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

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