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

TextVectorization과 Embedding

~12 min · text, tokenization, embedding

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

단어 → 숫자, 제대로

텍스트를 신경망에 넣기 전에 raw 문자열을 dense 숫자 벡터로 변환. 두 단계: TextVectorization으로 토큰화 + 정수 인코딩, 그 다음 Embedding layer로 정수를 dense 벡터에 매핑.

TextVectorization은 model graph 안에 사는 preprocessing layer. 즉 토큰화 로직이 model과 함께 저장되고 외부 스크립트 없이 inference에서 작동 — production 배포에 큰 장점.

Embedding layer는 각 정수 토큰 ID를 dense 벡터로 매핑. 이 벡터는 training 중에 학습돼 — 의미상 유사한 단어가 embedding 공간에서 가까이 위치. mask_zero=True는 다음 layer들에 padding 토큰 (ID 0) 무시하라고 지시.

고전적 속성: 잘 훈련된 embedding은 의미 관계 인코딩. 유명한 예 king - man + woman ≈ queen이 성립하는 이유는 embedding 공간의 방향이 의미에 대응하기 때문. 2026년 production NLP는 처음부터 학습 대신 KerasHub pretrained embedding (BERT, GPT-2, Gemma)에서 시작.

Code

TextVectorization + Embedding·python
import tensorflow as tf
from tensorflow.keras import layers

# Build vocab from training texts
vectorize = layers.TextVectorization(
    max_tokens=10000,
    output_mode='int',
    output_sequence_length=200,
    standardize='lower_and_strip_punctuation',
    split='whitespace',
)
train_texts = ["I love TensorFlow", "Deep learning is fun"]
vectorize.adapt(train_texts)
print(vectorize.get_vocabulary()[:5])    # ['', '[UNK]', 'is', ...]

# Embedding maps int IDs → dense 128-d vectors
embedding = layers.Embedding(
    input_dim=10000 + 2,    # vocab size + [PAD] + [UNK]
    output_dim=128,
    mask_zero=True,         # mask padding for variable-length sequences
)

# Full text classification pipeline
model = tf.keras.Sequential([
    vectorize,
    embedding,
    layers.Bidirectional(layers.LSTM(64, return_sequences=True)),
    layers.Bidirectional(layers.LSTM(32)),
    layers.Dense(64, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(1, activation='sigmoid'),
])

Progress

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

댓글 0

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

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