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

Tokenizer

~8 min · keras-nlp

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

tokenizer 가 곁다리가 아닌 이유

model 은 text 를 절대 안 봐 — integer 만 봐. tokenizer 가 그 다리야: 문자열을 sub-word 조각으로 쪼개고, 각 조각을 model 이 학습한 고정 vocabulary 의 ID 로 매핑해. 마지막 구절이 전부야. vocab 이 weight 에 박혀있어서 tokenizer 와 model 은 부부 사이 — 한쪽만 바꾸면 ID 가 엉뚱한 embedding 을 가리키고, 에러 한 줄 없이 그럴듯한 쓰레기가 나와.

알고리즘 셋, 하는 일은 하나

family 마다 쪼개는 방식이 달라: WordPiece (BERT), BytePair / BPE (GPT 계열), SentencePiece (T5, Llama, Gemma). 다 같은 문제를 풀어 — vocab 에 한 번도 없던 단어까지 더 작은 조각으로 fallback 해서 어떤 문자열이든 표현 — 그냥 쪼개는 규칙이 다를 뿐. modern KerasHub 에선 이게 pure-Python 으로 돌아서 TensorFlow 의존성 없어. torch / JAX 프로젝트에 tokenizer 때문에 TF 스택 끌어올 일 없어졌어.

실전 결론 (Code 참고): tokenizer 를 손으로 만들 일은 거의 없어. Preprocessor.from_preset() 로드하면 짝 tokenizer 가 그 안에 따라와. raw vocabulary= 로 직접 빌드하는 건 새 vocab 을 from scratch 로 학습하는 드문 경우용이야.

Code

raw vocab 으로 빌드 vs preset 의 짝 tokenizer 로드·python
# Tokenizers are included with model presets
tokenizer = keras_hub.tokenizers.WordPieceTokenizer(
    vocabulary=vocab_data,
)

# Or use the tokenizer that comes with a preset
preprocessor = keras_hub.models.BertPreprocessor.from_preset(
    "bert_base_en",
    sequence_length=128,
)

External links

Exercise

BERT 의 tokenizer 가져와. 'Hello, world! 안녕!' tokenize. token ID + decoded 문자열 출력. 한국어 처리 (BERT-base-en 은 byte 단위로 쪼갤 가능성) 메모.

Progress

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

댓글 0

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

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