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

실전 — Text classification fine-tuning

~8 min · keras-nlp

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

track 전부를 한 workflow 로

여기가 capstone — track 전체를 돌릴 수 있는 레시피 하나로 압축한 거야. Code 를 지금까지 배운 것의 체크리스트로 읽어봐: preset (lesson 2) 이 pretrained backbone 과 짝 tokenizer (lesson 3) 를 주고, num_classesclassification head (lesson 4) 모양을 잡고, enable_lora 가 fine-tune 을 싸게 만들고 (lesson 6), save 가 ship 가능한 .keras 파일 하나를 써. 새로운 건 하나도 없어 — 조립일 뿐이야.

성패를 가르는 디테일

이 레시피의 몇 가지 선택은 변호할 가치가 있어. plain Adam 말고 AdamW: decoupled weight decay 가 transformer fine-tuning 표준이고 일반화가 조금 더 안정적이야. capstone 에 LoRA: BERT 크기 모델이어도 켜둘 만해 — 평범한 하드웨어에서 재현 가능하게 하고 저장 artifact 를 작게 유지하거든. validation set 은 타협 불가validation_data 없으면 overfitting 이 오는 걸 못 봐. pretrained 모델에 epoch 3 이면 작은 dataset 은 금방 overfit 해. 여기서 제일 흔한 실패는 모델이 아니라 — unbalanced 거나 label 잘못 붙은 dataset 이야. garbage label 넣으면 자신만만한 garbage 가 나와.

여기서부터는 전부 변형

이게 end-to-end 로 돌면 문이 열려. dataset 만 바꾸면 customer-support router, backbone 을 multilingual preset 으로 바꾸면 비영어 텍스트에도 동작, num_classes 만 바꾸면 같은 코드가 topic tagging 이나 content moderation 을 해. capstone 은 결승선이 아니라 — 이후 모든 NLP feature 가 재사용하는 template 이야.

Code

End-to-end — load → LoRA → compile → fit → save·python
import keras
import keras_hub

# Load pretrained classifier
classifier = keras_hub.models.BertClassifier.from_preset(
    "bert_base_en",
    num_classes=3,
)

# Enable LoRA for efficient fine-tuning
classifier.backbone.enable_lora(rank=4)

# Compile
classifier.compile(
    optimizer=keras.optimizers.AdamW(5e-5),
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"],
)

# Train
classifier.fit(
    train_ds,
    validation_data=val_ds,
    epochs=3,
)

# Save the fine-tuned model
classifier.save("my_classifier.keras")

External links

Exercise

domain text classification dataset 골라 또는 annotate (예: 제품 트윗 sentiment). BertClassifier fine-tune. .keras 로 저장. stdin 분류하는 작은 predict.py 빌드.

Progress

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

댓글 0

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

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