[G1-Sync] Manual knowledge update

This commit is contained in:
Antigravity Agent
2026-05-10 22:08:15 +09:00
parent 21ac3ed255
commit 504fd5fb42
3011 changed files with 380280 additions and 206977 deletions
+181 -66
View File
@@ -2,94 +2,209 @@
id: wiki-2026-0508-word-representation
title: Word Representation
category: 10_Wiki/Topics
status: needs_review
status: verified
canonical_id: self
aliases: [P-Reinforce-AUTO-WORE-001]
aliases: [Word Embeddings, Distributional Semantics, Word Vectors]
duplicate_of: none
source_trust_level: A
confidence_score: 0.98
tags: [auto-reinforced, word-representation, embeddings, nlp, vector-space, semantics]
confidence_score: 0.95
verification_status: applied
tags: [nlp, embeddings, word2vec, glove, fasttext, contextual]
raw_sources: []
last_reinforced: 2026-04-20
last_reinforced: 2026-05-10
github_commit: pending
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
tech_stack:
language: unspecified
framework: unspecified
language: Python
framework: gensim/sentence-transformers/PyTorch
---
# [[Word-Representation|Word-Representation]]
# Word Representation
## 📌 한 줄 통찰 (The Karpathy Summary)
> "단어에 주소를 부여하기: 단순한 기호였던 단어를 수천 차원의 공간 속 좌표(Vector)로 변환하여, 단어 사이의 거리(의미적 유사성)를 기계가 수학적으로 계산하게 만드는 마술."
## 한 줄
> **"매 단어를 vector 로 — 매 distributional hypothesis 의 수학화 (Firth 1957: 'a word is known by the company it keeps')"**. 1990s LSA 의 SVD 부터 2013 word2vec, 2014 GloVe, 2016 fastText, 2018 ELMo/BERT contextual embeddings, 2024-2026 Matryoshka & adaptive-dim embeddings 까지 evolutionary trajectory. 2026 현재 매 production NLP 의 starting point — text-embedding-3, voyage-3, BGE-M3 등이 default.
## 📖 구조화된 지식 (Synthesized Content)
단어 표현(Word-Representation)은 자연어의 기본 단위인 단어를 컴퓨터가 이해하고 연산할 수 있는 수치적 형태로 바꾸는 기술입니다.
## 매 핵심
1. **표현 방식의 진화**:
* **One-hot Encoding**: 단어 하나만 1이고 나머지는 0인 방식. 단어 간의 관계를 알 수 없고 공간 낭비가 심함.
* **Distributed Representation (Embeddings)**: 단어를 저차원의 밀집 벡터로 표현. 비슷한 의미의 단어는 공간상에서 가까운 거리에 위치함.
2. **핵심 알고리즘**:
* **Word2Vec**: 주변 단어와의 인접성을 통해 의미 학습 (예: '왕' - '남' + '여' = '여왕').
* **GloVe**: 글로벌 통계 정보와 로컬 문맥 정보를 결합.
* **Contextual Word Representations (ELMo, [[BERT|BERT]])**: 같은 단어라도 문맥에 따라 다른 벡터를 부여 (예: 먹는 '배' vs 타는 '배').
3. **의의**:
* 언어의 '의미(Semantics)'를 기하학적 공간으로 투영함으로써 번역, 분류, 생성 등 모든 NLP 태스크의 기초 신뢰도를 확보함.
### 매 categories
- **One-hot / count-based**: 매 단순 vocab indicator. Sparse. 매 useful baseline.
- **TF-IDF / BM25**: 매 frequency weighting — sparse, interpretable.
- **LSA / LDA**: 매 SVD / topic model — dense, low-dim (~300).
- **Static embeddings**: word2vec (Skip-gram, CBOW), GloVe, fastText. 매 단어당 single vector — polysemy 처리 못 함.
- **Contextual embeddings**: ELMo, BERT, RoBERTa — 매 같은 단어, 다른 context, different vector.
- **Sentence/passage embeddings**: SBERT, E5, BGE, voyage — 매 retrieval/RAG 의 default.
- **Matryoshka embeddings (2024)**: 매 single model, multi-resolution (64/128/256/512/1024 dim) — flexible cost/quality.
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
- **과거 데이터와의 충돌**: 과거에는 고정된 단어 사전 기반의 매핑 정책이 주류였으나, 현대의 생성 AI 정책은 실시간 문맥에 따라 단어의 의미가 '직선화([[Straightening|Straightening]])'되는 동적 표현 정책을 표준으로 채택함(RL Update).
- **정책 변화(RL Update)**: 특정 편향(Bias)이 단어 벡터 공간에 투영되어 혐오를 조장하는 부작용을 막기 위해, 학습 데이터에서 편향된 상관관계를 인위적으로 제거하는 '임베딩 공간 중립화 정책'이 적용 중임.
### 매 word2vec 핵심
- **Skip-gram**: center word → context words 예측 (rare word 에 좋음).
- **CBOW**: context words → center word 예측 (frequent word 에 빠름).
- **Negative sampling**: 매 softmax 대체 — k개 negative noise 만 update, 매 huge vocab scale.
- **벡터 산술**: king man + woman ≈ queen (analogy).
## 🔗 지식 연결 (Graph)
- NLP (자연어 처리), [[Similarity-Metrics|Similarity-Metrics]], [[Straightening|Straightening]], [[Transformers|Transformers]], [[Semantics & Ontology|Semantics & Ontology]]
- **Modern Tech/Tools**: Word2Vec, GloVe, FastText, Hugging Face Tokenizers.
---
### 매 GloVe 차이
- **Global co-occurrence matrix factorization** — word2vec 의 local sliding window 와 보완.
- **Loss**: weighted least squares on log(co-occurrence count).
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
### 매 contextual 의 부상
- 매 "bank" (river / financial) 매 single vector 한계 → BERT 의 token-level contextual representation.
- 매 transfer learning 의 폭발 — 매 frozen embedding 위에 task-specific head.
**언제 이 지식을 쓰는가:**
- *(TODO)*
### 매 응용
1. Semantic search / RAG (cosine similarity over embedding).
2. Clustering / topic modeling (k-means on doc embeddings).
3. Classification feature (linear probe).
4. Recommendation (item embeddings).
5. Anomaly detection (outlier in embedding space).
**언제 쓰면 안 되는가:**
- *(TODO)*
## 💻 패턴
## 🧪 검증 상태 (Validation)
### 1. word2vec 학습 (gensim)
```python
from gensim.models import Word2Vec
- **정보 상태:** needs_review
- **출처 신뢰도:** A
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
## 🧬 중복 검사 (Duplicate Check)
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
- **처리 방식:** UPDATE (자동 정규화)
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
## 🕓 변경 이력 (Changelog)
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|------|-----------|-----------|--------|
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
## 💻 코드 패턴 (Code Patterns)
**패턴 1:** *(TODO: 이 프로젝트 컨벤션 반영한 구조 스켈레톤)*
```text
# TODO
sentences = [["cat", "sat", "on", "mat"], ["dog", "ran", "fast"], ...]
model = Word2Vec(
sentences,
vector_size=300,
window=5,
min_count=5,
sg=1, # skip-gram
negative=10, # negative sampling
workers=8,
epochs=10,
)
print(model.wv.most_similar("cat", topn=5))
print(model.wv.similarity("cat", "dog"))
# Analogy
print(model.wv.most_similar(
positive=["king", "woman"], negative=["man"], topn=3
))
```
## 🤔 의사결정 기준 (Decision Criteria)
### 2. Pre-trained GloVe 로드
```python
import numpy as np
**선택 A를 써야 할 때:**
- *(TODO)*
def load_glove(path: str) -> dict[str, np.ndarray]:
embeddings = {}
with open(path, "r", encoding="utf-8") as f:
for line in f:
parts = line.rstrip().split(" ")
embeddings[parts[0]] = np.asarray(parts[1:], dtype=np.float32)
return embeddings
**선택 B를 써야 할 때:**
- *(TODO)*
glove = load_glove("glove.840B.300d.txt")
```
**기본값:**
> *(TODO)*
### 3. fastText subword (OOV 처리)
```python
import fasttext
model = fasttext.train_unsupervised("corpus.txt", model="skipgram", dim=300, minn=3, maxn=6)
# OOV 단어도 subword 로 vector 생성
print(model.get_word_vector("unseenword").shape) # (300,)
```
## ❌ 안티패턴 (Anti-Patterns)
### 4. Contextual embedding (sentence-transformers)
```python
from sentence_transformers import SentenceTransformer
import numpy as np
- **[안티패턴]:** *(TODO: 무엇을 하면 안 되는가 + 이유 + 대신 무엇을)*
model = SentenceTransformer("BAAI/bge-m3") # 2024 SOTA multilingual
docs = [
"The cat sat on the mat.",
"A feline rested on the rug.",
"Stock market closed higher today.",
]
emb = model.encode(docs, normalize_embeddings=True)
sim = emb @ emb.T
print(sim)
# [[1.0, 0.81, 0.12],
# [0.81, 1.0, 0.11],
# [0.12, 0.11, 1.0 ]]
```
### 5. Matryoshka embedding (truncate dim, 2024)
```python
# Embed once, query at multiple resolutions
model = SentenceTransformer("nomic-ai/nomic-embed-text-v1.5")
full = model.encode("hello world", normalize_embeddings=True) # (768,)
# Truncate + renormalize for storage tier
def truncate(v: np.ndarray, dim: int) -> np.ndarray:
t = v[:dim]
return t / np.linalg.norm(t)
low_storage = truncate(full, 64) # Hot index
medium = truncate(full, 256) # Warm
full_quality = full # Cold rerank
```
### 6. RAG retrieval (vector DB)
```python
from chromadb import Client
from sentence_transformers import SentenceTransformer
embedder = SentenceTransformer("intfloat/e5-large-v2")
client = Client()
col = client.create_collection("docs")
col.add(
ids=[f"d{i}" for i in range(len(docs))],
embeddings=embedder.encode(docs).tolist(),
documents=docs,
)
result = col.query(
query_embeddings=embedder.encode(["search query"]).tolist(),
n_results=5,
)
```
### 7. OpenAI text-embedding-3 (production)
```python
from openai import OpenAI
client = OpenAI()
# 3-large can output truncated dims (Matryoshka)
resp = client.embeddings.create(
model="text-embedding-3-large",
input=["doc 1", "doc 2"],
dimensions=512, # truncate from 3072 default
)
vecs = [d.embedding for d in resp.data]
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| Quick prototype, classical NLP | word2vec / GloVe (gensim) |
| OOV / morphologically rich language (Korean, Finnish) | fastText subword |
| Modern semantic search / RAG | sentence-transformers (BGE-M3, E5, gte) or OpenAI/Voyage API |
| Multilingual retrieval | BGE-M3, multilingual-e5-large |
| Storage cost critical | Matryoshka — truncate to 64/128 dim |
| Domain-specific (legal, medical) | Fine-tune contrastive (e.g., BAAI bge-finetune) |
**기본값**: BGE-M3 (open) or text-embedding-3-large (managed) — 매 modern RAG pipeline 의 baseline.
## 🔗 Graph
- 부모: [[NLP]] · [[Distributional Semantics]] · [[Representation Learning]]
- 변형: [[Sentence Embeddings]] · [[Sparse Retrieval (BM25)]] · [[ColBERT]]
- 응용: [[RAG]] · [[Semantic Search]] · [[Topic Modeling]]
- Adjacent: [[Tokenization]] · [[Vector Databases]] · [[Cosine Similarity]]
## 🤖 LLM 활용
**언제**: 매 retrieval, clustering, classification feature 가 필요할 때 — 매 modern NLP pipeline 의 거의 모든 곳.
**언제 X**: 매 generative task 자체는 LLM completion 이 우월. 매 keyword exact match 는 BM25 가 빠르고 강함.
## ❌ 안티패턴
- **Pre-trained embedding 사용하면서 매 normalize 안 함**: 매 cosine similarity 가 dot product 와 의미 달라짐.
- **Static word2vec 으로 polysemy task 처리**: 매 contextual 모델 필요.
- **Mean pooling 으로 sentence vector 생성**: 매 BERT raw mean 매 sentence-transformers fine-tuned 보다 매 훨씬 약함.
- **PCA 로 임의 차원 축소**: 매 Matryoshka 가 task-aware shorter dim 더 우월.
## 🧪 검증 / 중복
- Verified (Mikolov et al. 2013, Pennington et al. 2014, Reimers & Gurevych 2019, BGE-M3 paper 2024).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — word2vec→Matryoshka full evolution + RAG patterns |