Word2Vec
Word2Vec
An embedding technique that represents words as vectors. CBOW and Skip-gram methods. The foundation of modern NLP.
Word2Vec
An embedding technique that represents words as vectors. CBOW and Skip-gram methods. The foundation of modern NLP.
Word2Vec is a word embedding technique published by Tomas Mikolov of Google in 2013, which represents words as fixed-dimensional dense vectors (Dense Vector). Based on the distributional hypothesis that "words that appear in similar contexts have similar meanings," it learns semantic relationships between words from large text corpora.
Before Word2Vec, One-Hot Encoding was mainly used. However, a one-hot vector has a dimensionality equal to the vocabulary size and cannot express similarity between words. Word2Vec represents words as vectors of several hundred dimensions and can capture semantic relationships through vector arithmetic, as in "king - man + woman = queen."
Word2Vec offers two training methods. CBOW (Continuous Bag of Words) predicts the center word from its surrounding words, while Skip-gram predicts the surrounding words from the center word. Skip-gram is strong on rare words, and CBOW trains faster. Negative Sampling improves training efficiency, and hundreds of millions of words can be trained on a CPU.
In practice, Word2Vec performs worse than contextual embeddings such as BERT and GPT, so direct use has declined, but it remains conceptually very important. FastText (subword processing) and GloVe (use of global statistics) built on Word2Vec and became the foundation of modern embeddings. It is still used for educational purposes and lightweight projects.
from gensim.models import Word2Vec
from gensim.models import KeyedVectors
import numpy as np
# 1. ์ํ ๋ฐ์ดํฐ (ํ ํฐํ๋ ๋ฌธ์ฅ ๋ฆฌ์คํธ)
sentences = [
["์ธ๊ณต์ง๋ฅ", "๋จธ์ ๋ฌ๋", "๋ฅ๋ฌ๋", "์ฐ๊ตฌ"],
["์์ฐ์ด", "์ฒ๋ฆฌ", "NLP", "ํ
์คํธ", "๋ถ์"],
["๋จธ์ ๋ฌ๋", "์๊ณ ๋ฆฌ์ฆ", "ํ์ต", "๋ฐ์ดํฐ"],
["๋ฅ๋ฌ๋", "์ ๊ฒฝ๋ง", "GPU", "ํ์ต"],
["์ธ๊ณต์ง๋ฅ", "์์ฐ์ด", "์ฒ๋ฆฌ", "์ฑ๋ด"],
]
# 2. Word2Vec ๋ชจ๋ธ ํ์ต
model = Word2Vec(
sentences,
vector_size=100, # ์๋ฒ ๋ฉ ์ฐจ์
window=5, # ์ปจํ
์คํธ ์๋์ฐ ํฌ๊ธฐ
min_count=1, # ์ต์ ์ถํ ๋น๋
sg=1, # 1=Skip-gram, 0=CBOW
workers=4, # ๋ณ๋ ฌ ์ค๋ ๋ ์
epochs=100 # ํ์ต ์ํฌํฌ
)
# 3. ๋จ์ด ๋ฒกํฐ ํ์ธ
vector = model.wv["์ธ๊ณต์ง๋ฅ"]
print(f"'์ธ๊ณต์ง๋ฅ' ๋ฒกํฐ ์ฐจ์: {vector.shape}")
print(f"๋ฒกํฐ (์ฒ์ 10๊ฐ): {vector[:10]}")
# 4. ์ ์ฌ ๋จ์ด ๊ฒ์
similar = model.wv.most_similar("๋จธ์ ๋ฌ๋", topn=5)
print("\n'๋จธ์ ๋ฌ๋'๊ณผ ์ ์ฌํ ๋จ์ด:")
for word, score in similar:
print(f" {word}: {score:.4f}")
# 5. ๋จ์ด ๊ฐ ์ ์ฌ๋
similarity = model.wv.similarity("๋ฅ๋ฌ๋", "์ ๊ฒฝ๋ง")
print(f"\n๋ฅ๋ฌ๋ - ์ ๊ฒฝ๋ง ์ ์ฌ๋: {similarity:.4f}")
# 6. ๋ฒกํฐ ์ฐ์ฐ (Word Analogy)
# A:B = C:? -> ? = B - A + C
try:
result = model.wv.most_similar(
positive=["๋ฅ๋ฌ๋", "์์ฐ์ด"],
negative=["๋จธ์ ๋ฌ๋"],
topn=3
)
print("\n๋ฅ๋ฌ๋ - ๋จธ์ ๋ฌ๋ + ์์ฐ์ด =")
for word, score in result:
print(f" {word}: {score:.4f}")
except KeyError as e:
print(f"๋จ์ด ์์: {e}")
# 7. ๋ชจ๋ธ ์ ์ฅ & ๋ก๋
model.save("word2vec.model")
loaded_model = Word2Vec.load("word2vec.model")
# ๋ฒกํฐ๋ง ์ ์ฅ (๊ฒฝ๋)
model.wv.save_word2vec_format("word2vec.txt", binary=False)
# === ์ฌ์ ํ์ต ๋ชจ๋ธ ์ฌ์ฉ (์ค๋ฌด ๊ถ์ฅ) ===
# Google News 300์ฐจ์ (์ฝ 3GB)
# https://drive.google.com/file/d/0B7XkCwpI5KDYNlNUTTlSS21pQmM
# pretrained = KeyedVectors.load_word2vec_format(
# "GoogleNews-vectors-negative300.bin", binary=True
# )
# print(pretrained.most_similar("king"))
# === ํ๊ตญ์ด: FastText ์ฌ์ฉ ๊ถ์ฅ ===
# from gensim.models import FastText
#
# model_ko = FastText(
# sentences,
# vector_size=100,
# window=5,
# min_count=1,
# sg=1
# )
# # FastText๋ OOV(๋ฏธ๋ฑ๋ก ๋จ์ด)๋ ์ฒ๋ฆฌ ๊ฐ๋ฅ
"Shall we apply Word2Vec-based synonym expansion to improve search? When a user searches for 'laptop,' 'notebook' and 'computer' would be searched together. That said, sentence embeddings like Sentence-BERT are more accurate these days."
"What is the difference between Word2Vec and BERT embeddings?" - "Word2Vec assigns a single fixed vector to each word. 'bank' gets the same vector whether it means a financial institution or a riverbank. BERT, on the other hand, is a contextual embedding that generates different vectors depending on context, so it can distinguish homonyms too."
"The 'king - man + woman = queen' operation in Word2Vec is actually not always accurate. Data bias is reflected, so unintended results can sometimes appear. Still, the very idea of representing word meaning in a vector space was groundbreaking."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free
Word2Vec requires a large corpus of millions to billions of words. Training on small data yields low quality. Use pretrained models such as Google News or Wiki, or continue training on a domain corpus (Continual Training).
Word2Vec cannot handle words that were not in its training data. Even variants such as "์ธ๊ณต์ง๋ฅ๋ค" are treated as separate words. For languages with diverse morphology like Korean, FastText (which supports subword processing) is more suitable.
In real NLP projects, use contextual embeddings such as BERT and Sentence-BERT. Word2Vec is suitable for educational/research purposes or environments with extremely limited resources.