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

Custom object 등록

~8 min · serialize

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

Subclassed Layer / Model 을 .keras 로 저장하면 deserialize 시 *어떤 클래스인지* 알아야 함. @keras.saving.register_keras_serializable() 데코레이터로 등록 — 이름 → 클래스 매핑 자동.

get_config / from_config 도 구현 권장. config 가 layer 의 __init__ 인자. def get_config(self): cfg = super().get_config(); cfg.update({'units': self.units}); return cfg — 그 후 from_config 가 그 dict 으로 재 인스턴스화.

Code

@keras.saving.register_keras_serializable(package="my_package")
class <span class="dc">MyCustomLayer</span>(keras.layers.Layer):
    def __init__(self, units, **kwargs):
        super().__init__(**kwargs)
        self.units = units

    def get_config(self):
        config = super().get_config()
        config.update({{"units": self.units}})
        return config

# Now model.save() / load_model() works with custom objects

External links

Exercise

앞 custom Transformer block 가져와. @keras.saving.register_keras_serializable() + get_config 메서드 추가. 그 layer 사용 모델 저장 / 재로드. 출력 정확히 일치 확인.

Progress

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

댓글 0

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

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