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

Preprocessing layer

~9 min · layers

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

전처리를 script 말고 layer 로

ML 프로덕션에서 가장 흔한 버그가 train/serve skew 야. 학습 파이프라인은 픽셀을 255 로 나누고 텍스트를 소문자화하는데, serving 코드가 그 한 단계를 빼먹으면 — model 이 학습 때랑 미묘하게 다른 입력에 조용히 헛소리를 뱉어. Keras 3 의 답은 전처리를 model graph 안에 사는 layer 로 만드는 것 — model 저장하면 전처리도 같이 저장되고, 따로 sync 맞출 두 번째 script 가 아예 없어.

카탈로그

숫자: Rescaling(1./255) 가 픽셀을 [0, 1] 로, Normalization() 이 feature 를 zero-mean / unit-variance 로. 텍스트: TextVectorization 이 raw 문자열을 정수 sequence 로. 범주: CategoryEncoding / StringLookup 이 범주·문자열을 model 이 먹을 tensor 로. 같은 패밀리에 image augmentation (RandomFlip, RandomRotation) 도 있어.

adapt() 단계

Rescaling 같은 stateless layer 는 아무것도 필요 없어 — 계수가 고정이거든. 근데 Normalization 이랑 TextVectorization 은 data 에서 통계를 먼저 배워: layer.adapt(train_data) 를 한 번 호출하면 layer 가 mean/variance 를 계산하거나 vocab 을 구축해. adapt 후엔 일반 layer 처럼 동작하고, 그 학습된 통계를 배포 환경까지 자동으로 들고 가.

Code

Preprocessing layer — 숫자 / 텍스트 / 범주·python
# Numeric preprocessing
layers.Rescaling(1.0/255)             # Normalize pixel values
layers.Normalization()                  # Standardize features (.adapt() first)

# Text preprocessing
layers.TextVectorization(
    max_tokens=10000,
    output_mode="int",
    output_sequence_length=200,
)

# Category encoding
layers.CategoryEncoding(num_tokens=10)
layers.StringLookup(vocabulary=["cat", "dog", "bird"])

External links

Exercise

첫 두 layer 가 Rescaling(1./255) 와 RandomFlip('horizontal') 인 CNN 짜. CIFAR-10 학습. model 저장하고 새 프로세스에서 다시 로드, raw uint8 [0,255] 픽셀로 predict() — 전처리 재구현 없이 rescaling 이 자동 적용돼서 멀쩡한 예측 나오는지 확인. 그 round-trip 이 이 lesson 의 핵심.
Hint
RandomFlip 은 학습 시에만 켜져서 inference 예측엔 영향 없어 — 근데 Rescaling 은 두 모드 다 돌아, 그게 바로 train/serve skew 에서 널 구해주는 부분이야.

Progress

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

댓글 0

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

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