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

PyTorch Backend

~8 min · backend

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

연구·prototype 의 home

PyTorch backend (PyTorch 2.1.0+) 는 연구 세계가 이미 사는 framework 에 Keras 를 바로 꽂아. Hugging Face Transformers·torchvision·torchaudio, 그리고 신규 pretrained model 대부분이 PyTorch 우선으로 나와. eager-by-default 라 plain Python 도구로 디버깅 — breakpoint 찍고 tensor 들여다보고, graph 의식 없음.

Keras model 이 곧 torch.nn.Module

interop 을 여는 핵심 디테일: 이 backend 에선 Keras layer·model 이 진짜 torch.nn.Module 인스턴스가 돼 (Code block). wrapper 가 아니라 실제 타입이야. 그래서 Keras model 을 손수 짠 PyTorch train loop 안에 넣거나, 부모 nn.Module 에 등록하거나, parameter 를 torch.optim optimizer 에 넘길 수 있어. compile/fit 의 편의는 유지하면서 필요한 순간 raw torch.autograd 로 내려가는 거지.

주의 하나: KERAS_BACKEND=torch 면 model 이 torch tensor 를 받고 내놔. NumPy / TF tensor 던지면 type error. cross-backend 코드는 keras.ops 통해서 — 다음 lesson 들에서 다뤄.

Code

Keras model 이 진짜 torch.nn.Module·python
os.environ["KERAS_BACKEND"] = "torch"
import keras
from keras import layers

model = keras.Sequential([
    layers.Dense(128, activation="relu"),
    layers.Dense(10, activation="softmax"),
])

# Keras model IS a torch.nn.Module
print(isinstance(model, torch.nn.Module))  # True

External links

Exercise

MNIST 스크립트를 KERAS_BACKEND=torch 로 바꿔. 같은 fit/compile 경로가 그대로 도는지 확인. 그다음 batch 에 print(type(x)) 박아 봐 — torch.Tensor 보일 거야.

Progress

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

댓글 0

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

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