본문 바로가기
C.W.K.
Stream
Lesson 08 of 08 · published

실전 — 같은 모델을 세 백엔드에서 돌리기

~8 min · backend

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

한 스크립트로 약속을 검증해

이제 다중 백엔드가 구호가 아니라 실제로 동작하는지 확인해보자. 코드 블록의 모델 정의, compilefit, 저장 호출은 100% 백엔드 독립적이라 TensorFlow, PyTorch, JAX 가운데 어느 이름도 나오지 않아. 실행 엔진은 파일 밖에서 KERAS_BACKEND 환경 변수로 선택하고, 같은 소스가 세 백엔드에서 거의 같은 정확도로 학습돼.

.keras 파일이 백엔드 사이의 계약이야

마지막의 model.save("my_model.keras")가 학습과 배포를 이어줘. .keras 형식은 모델 구조, 가중치, 옵티마이저 상태를 특정 백엔드 연산 없이 저장해. 속도를 위해 JAX로 학습한 다음 TensorFlow에서 불러와 배포해도 재학습하거나 별도 형식으로 미리 변환할 필요가 없어. 한 번 저장한 파일을 어느 백엔드에서나 읽을 수 있어.

세 환경에서 실제로 달라지는 값은 에포크당 걸린 시간과 최대 메모리처럼 직접 측정해야 하는 성능이야. 어떤 백엔드가 알맞은지는 인터넷의 평균 비교가 아니라 아빠 모델과 하드웨어에서 얻은 숫자가 결정해.

Code

백엔드와 무관한 한 스크립트로 모델 생성·학습·저장·python
import keras
from keras import layers

# This model definition is 100% backend-agnostic
def build_model():
    inputs = keras.Input(shape=(784,))
    x = layers.Dense(256, activation="relu")(inputs)
    x = layers.Dropout(0.3)(x)
    x = layers.Dense(128, activation="relu")(x)
    x = layers.Dropout(0.3)(x)
    outputs = layers.Dense(10, activation="softmax")(x)
    return keras.Model(inputs, outputs)

model = build_model()
model.compile(
    optimizer="adam",
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"],
)

# Train on any backend — same result
model.fit(x_train, y_train, epochs=10, validation_split=0.2)

# Save — cross-backend compatible!
model.save("my_model.keras")
# Load this on ANY backend — TF, PyTorch, JAX

External links

Exercise

같은 MNIST CNN을 TensorFlow, PyTorch, JAX에서 각각 실행해. 에포크당 시간, 최대 메모리, 최종 정확도를 표로 저장하고 다음 프로젝트에서 백엔드를 고를 때 참고해.

Progress

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

댓글 0

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

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