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

call() 메서드

~8 min · subclass

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

call() 이 계약

Layer 가 *하는 일* 은 전부 call() 안에 살아. 입력 tensor 받아서 출력 tensor 돌려주고, 시그니처엔 Keras 가 해당될 때 자동으로 넘겨주는 두 인자가 있어:

  • training — train 과 inference 사이를 가르는 boolean. Dropout 은 training=True 일 때만 unit 을 떨구고, BatchNormalization 은 train 중엔 현재 batch 통계를, inference 중엔 누적된 moving 통계를 써. layer 가 둘 중 하나라도 감싸면 이 flag 를 받아서 아래로 넘겨야 해.
  • mask — 어느 timestep 이 진짜고 어느 게 padding 인지 표시하는 optional boolean tensor. attention / recurrent layer 가 padding 자리를 무시할 수 있게 해줘.

Python 자유가 빛을 발하는 자리

call() 은 그냥 메서드라서 안에 진짜 제어 흐름을 넣을 수 있어 — if training: 분기, loop, shape 의존 계산. 이게 Functional API 대신 subclass 하는 이유 그 자체야 (Code block 참고). 그 자유를 portable 하게 유지하는 단 하나의 규율: tensor 연산은 keras.ops 로 하고, backend 의 native op 은 절대 쓰지 마. keras.ops.relu(x) 로 쓰면 같은 layer 가 TensorFlow / PyTorch / JAX 에서 안 바뀌고 돌아가. dynamic shape 도 마찬가지 — keras.ops.shape(x)[0] 처럼 portable form 으로.

Code

training 일 때만 분기하는 call()·python
class ConditionalLayer(keras.layers.Layer):
    def call(self, inputs, training=False):
        # Use keras.ops for backend-agnostic code!
        x = keras.ops.relu(inputs)
        if training:
            x = keras.ops.nn.dropout(x, rate=0.5)
        return x

External links

Exercise

학습 중에만 Dropout(0.5) 적용하는 Layer subclass 짜. layer(x, training=True) vs layer(x, training=False) 호출 — 출력이 다르게 / input 패턴 그대로 나오는지 확인.

Progress

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

댓글 0

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

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