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

Cross-backend 저장

~8 min · serialize

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

한 engine 에서 학습, 다른 engine 에서 로드

이게 Keras 3 의 진짜 magic — backend 하나에서 저장한 모델을 완전히 다른 backend 에서 로드. JAX 로 학습해서 빠른 JIT 컴파일 챙기고, .keras 저장한 다음, TensorFlow 에서 다시 열어서 TF Serving 으로 넘겨. 파일은 누가 썼는지 신경 안 써.

왜 되는 거냐면

.keras 는 backend-specific operation 을 하나도 저장 안 해. layer config 랑 weight 를 그냥 backend-agnostic array 로 기록할 뿐 — traced JAX 그래프나 frozen TF 함수 같은 게 안 박혀. 그래서 다른 backend 가 이 파일을 로드하면 config 로 같은 layer 를 다시 짓고, 그 연산을 자기 keras.ops 구현으로 dispatch 해. 숫자상 똑같은 모델, 밑에 깔린 engine 만 다른 거지.

휴대성이 깨지는 지점

이 보장은 Keras 안에 머무는 한 유지돼. layer 가 keras.ops 를 넘어서 backend native API 로 손 뻗으면 — 생 tf.signal.fft 호출이나 손으로 짠 PyTorch op — 그 연산은 다른 backend 에 대응물이 없어서 거기서 cross-backend 로드가 터져. 규칙은 단순해: Keras layer 랑 keras.ops 로만 모델 만들면 휴대성은 공짜, native op 로 내려가면 engine 하나에 스스로 못 박은 거야.

Code

JAX 에서 저장, TensorFlow 에서 재로드, serving 용 export·python
# Train on JAX (fast JIT compilation)
os.environ["KERAS_BACKEND"] = "jax"
model.fit(x_train, y_train, epochs=10)
model.save("trained_on_jax.keras")

# Later: load on TensorFlow for deployment
os.environ["KERAS_BACKEND"] = "tensorflow"
model = keras.models.load_model("trained_on_jax.keras")
model.export("tf_serving_model", format="tf_saved_model")

External links

Exercise

MNIST 를 KERAS_BACKEND=tensorflow 로 학습 후 mnist.keras 저장. KERAS_BACKEND=torch 로 새 shell 에서 로드 + test set predict. 원래 학습의 accuracy 와 일치 확인.

Progress

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

댓글 0

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

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