AI가 고양이가 개와 비슷하다는 것을 아는 방법: Word Embeddings에 대한 직관적 가이드
Source: Dev.to
단어 임베딩의 직관
아이디어로 수학을 한다고 상상해 보세요. 고전적인 예시:
King - Man + Woman ≈ Queen
이는 GloVe(Global Vectors for Word Representation)와 같은 정적 임베딩의 힘을 보여줍니다. GloVe는 방대한 코퍼스를 스캔하고, 단어들이 서로 근처에 등장하는 빈도를 세어 각 단어에 고정된 수치 벡터를 할당합니다. 이러한 벡터가 “의미”를 포착하기 때문에, 의미적으로 유사한 단어들은 서로 가깝게 배치됩니다.
다의어 문제
정적 모델은 다의어—여러 의미를 가진 단어—에 어려움을 겪습니다.
- I need to go to the bank to deposit some money. (금융 기관)
- We sat on the bank of the river. (강가)
GloVe와 같은 정적 모델에서는 “bank”가 모든 상황을 평균한 하나의 벡터만을 가지므로, 이러한 의미 차이를 구분할 수 없습니다.
동적(맥락적) 임베딩
맥락적 임베딩(예: BERT – Bidirectional Encoder Representations from Transformers)은 전체 문장을 살펴 각 단어 등장마다 고유한 벡터를 생성합니다. BERT가 위의 두 “bank” 문장을 처리하면, 주변 단어(“river”, “deposit”)가 해석을 안내하기 때문에 서로 다른 벡터를 만들어냅니다.
PyTorch를 이용한 간단한 BERT 사용법
import torch
from transformers import BertTokenizer, BertModel
# Load tokenizer and model
tokenizer = BertTokenizer.from_pretrained('./bert_model')
model_bert = BertModel.from_pretrained('./bert_model')
model_bert.eval() # Prevent training
def print_token_embeddings(sentence: str, label: str):
"""
Tokenizes a sentence, runs it through BERT,
and prints the first 5 values of each token's embedding.
"""
inputs = tokenizer(sentence, return_tensors="pt")
with torch.no_grad():
outputs = model_bert(**inputs)
embeddings = outputs.last_hidden_state[0]
tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
print(f"\n--- {label} ---")
for token, vector in zip(tokens, embeddings):
print(f"{token}: {vector[:5].tolist()}")
토론 질문: 오늘 새로운 모델을 처음부터 학습시킨다면, 어떤 특정 어휘나 틈새 주제를 가장 먼저 배우게 하고 싶나요? 댓글로 여러분의 생각을 공유해 주세요.
이 게시물의 모든 일러스트는 DALL·E 3를 사용해 생성되었습니다.
참고 문헌
- Devlin, J. et al. (2018). BERT: Pre‑training of Deep Bidirectional Transformers for Language Understanding.
- Stanford NLP Group. GloVe: Global Vectors for Word Representation.
- Spot Intelligence. GloVe Embeddings Explained.