- ์ถ๋ก ๊ธฐ๋ฐ์ ๊ธฐ๋ฒ ⇒ ๋จ์ด๋ฅผ ๋ฒกํฐ๋ก ๋ณํ์์ผ์ ํ์ตํ๋ ๊ฒ
- ์ด ๋ํ ๋ถํฌ๊ฐ์ค์ ๊ธฐ๋ฐ์ผ๋ก(๋จ์ด์ ์๋ฏธ๋ ์ฃผ๋ณ ๋จ์ด์ ์ํด ํ์ฑ๋๋ค)
ํต๊ณ ๊ธฐ๋ฐ ๊ธฐ๋ฒ์ ๋ฌธ์ ์
- ํต๊ณ ๊ธฐ๋ฐ์ ๊ฒฝ์ฐ, ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ํ๋ฒ์ ํ์ต์ํค๊ฒ ๋จ.
- ๋ฐ๋ผ์ ์๊ฐ์ด ๊ต์ฅํ ์ค๋ ๊ฑธ๋ฆผ
- ์ด์ ๋ฐ๋ผ word2vec(์ถ๋ก ๊ธฐ๋ฐ๊ธฐ๋ฒ)์ ๊ฒฝ์ฐ, ๋ฏธ๋๋ฐฐ์น๋ฅผ ํ์ฉํด ํ์ตํจ
์ถ๋ก ๊ธฐ๋ฐ ๊ธฐ๋ฒ

- ์ถ๋ก ๋ฌธ์ ๋ฅผ ๋ฐ๋ณตํด ๋จ์ด์ ์ถํ ํจํด์ ํ์ตํ๋.

- ์ ๊ฒฝ๋ง์ ํ์ฉํด ๋จ์ด๋ฅผ ์ฒ๋ฆฌ
- ๋งฅ๋ฝ → ๋ชจ๋ธ → ํ๋ฅ ๋ถํฌ

- ์ ๊ฒฝ๋ง์ ๊ฒฝ์ฐ, ๋จ์ด๋ฅผ ๊ทธ๋๋ก ์ฒ๋ฆฌํ์ง ๋ชปํจ
- ๋ฐ๋ผ์ ‘๊ณ ์ ๊ธธ์ด์ ๋ฒกํฐ’๋ก ๋ณํํด์ผ ํจ → ์ํซ ๋ฒกํฐ๋ก ๋ณํ.
- ๊ฒฐ๊ตญ์๋ c์์ 1์ธ ์ ๋ค์ ๊ฐ์ค์น์ ํ ๋ฒกํฐ๋ง ์ถ๋ ฅ๋จ (๋๋จธ์ง๋ 0์ด๋ฏ๋ก)

- ๊ตณ์ด ํ๋ ฌ๊ณฑ์ ์ํํ๋ ๊ฒ์ ์๊ฐ ๋ญ๋น.
- ๊ทธ๋์ embedded ๊ณ์ธต์ ์ฌ์ฉํ๊ฒ ๋จ.
word2vec
- CBOW ๋ชจ๋ธ
- skip- gram ๋ชจ๋ธ
- ์ถ๋ก ๊ธฐ๋ฐ ๊ธฐ๋ฒ
- ๋จ์ 2์ธต ์ ๊ฒฝ๋ง
- ๊ฐ์ค์น ์ฌํ์ต์ด ๊ฐ๋ฅ. ๋ฐ๋ผ์ ์ถ๊ฐ๋ ๋จ์ด๋ฅผ ํจ์จ์ ์ผ๋ก ํ์ต ๊ฐ๋ฅ
CBOW ๋ชจ๋ธ
- ๋งฅ๋ฝ์ผ๋ก๋ถํฐ ํ๊น์ ์ถ์ธกํ๋ ์ฉ๋์ ์ ๊ฒฝ๋ง
- ex) ๋งฅ๋ฝ: you goodbye, ํ๊น: say

- ๊ฐ์ค์น์ ๊ฒฝ์ฐ ๋์ผ ๊ฐ์ค์น๋ฅผ ๋ฃ์์ ⇒
- Win: ํ๋ฒกํฐ(๊ฐ ํ์ ๊ฐ ๋จ์ด์ ๋ถ์ฐ ํํ)
- Wout: ์ด๋ฒกํฐ(๋จ์ด์ ์๋ฏธ๊ฐ ์ธ์ฝ๋ฉ๋ ๋ฒกํฐ ์ ์ฅ)
- ๋งฅ๋ฝ์ด 2๊ฐ ์ด๋ฏ๋ก (you, goodbye) MatMul ๊ณ์ธต(์ ๋ ฅ์ธต) ๋ ๋๊ฐ๋ก ๋ง๋ฌ
- 0.5: ๋๊ฐ์ ํ๊ท ๊ฐ
- ์ฌ๊ธฐ์ ํต์ฌ์ ์๋์ธต์ ๋ด๋ฐ ์๋ฅผ ์ ๋ ฅ์ธต์ ๋ด๋ฐ ์๋ณด๋ค ์ ๊ฒ ํ๋ ๊ฒ!
- ๊ทธ๋์ผ ๋จ์ด ์์ธก์ ํ์ํ ์ ๋ณด๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ๋ด๊ฒ ๋๋ค.
๋ชจ๋ธ์ ํ์ต

def preprocess(text): text = text.lower() #์๋ฌธ์๋ก text = text.replace('.', ' .') #ํน์๋ฌธ์ ๋ถ๋ฆฌ? words = text.split(' ') #๋์ด์ฐ๊ธฐ๋ก ๋๊ธฐ #๋จ์ด id ๋ณํ word_to_id = {} id_to_word = {} for word in words: if word not in word_to_id: new_id = len(word_to_id) word_to_id[word] = new_id id_to_word[new_id] = word corpus = np.array([word_to_id[w] for w in words]) return corpus, word_to_id, id_to_word
text = 'You say goodbye and I say hello' corpus, word_to_id, id_to_word = preprocess(text) print(corpus) print(id_to_word)


def create_contexts_target(corpus, window_size=1): target = corpus[window_size:-window_size] contexts = [] for idx in range(window_size, len(corpus)-window_size): cs = [] for t in range(-window_size, window_size + 1): if t == 0: continue cs.append(corpus[idx + t]) contexts.append(cs) return np.array(contexts), np.array(target)
contexts, target = create_contexts_target(corpus, window_size= 1) print(contexts) print(target)


def convert_one_hot(corpus, vocab_size): N = corpus.shape[0] if corpus.ndim == 1: one_hot = np.zeros((N, vocab_size), dtype=np.int32) for idx, word_id in enumerate(corpus): one_hot[idx, word_id] = 1 elif corpus.ndim == 2: C = corpus.shape[1] one_hot = np.zeros((N, C, vocab_size), dtype=np.int32) for idx_0, word_ids in enumerate(corpus): for idx_1, word_id in enumerate(word_ids): one_hot[idx_0, idx_1, word_id] = 1 return one_hot
text = "You say goodbye and I say hello" corpus,word_to_id, id_to_word = preprocess(text) contexts, target = create_contexts_target(corpus, window_size=1) vocab_size = len(word_to_id) target = convert_one_hot(target, vocab_size) contexts = convert_one_hot(contexts,vocab_size)
class SimpleCBOW: def __init__(self, vocab_size, hidden_size): V, H = vocab_size, hidden_size # ๊ฐ์ค์น ์ด๊ธฐํ W_in = 0.01 * np.random.randn(V, H).astype('f') W_out = 0.01 * np.random.randn(H, V).astype('f') # ๊ณ์ธต ์์ฑ self.in_layer0 = MatMul(W_in) self.in_layer1 = MatMul(W_in) self.out_layer = MatMul(W_out) self.loss_layer = SoftmaxWithLoss() # ๋ชจ๋ ๊ฐ์ค์น์ ๊ธฐ์ธ๊ธฐ๋ฅผ ๋ฆฌ์คํธ์ ๋ชจ์๋ค. layers = [self.in_layer0, self.in_layer1, self.out_layer] self.params, self.grads = [], [] for layer in layers: self.params += layer.params self.grads += layer.grads # ์ธ์คํด์ค ๋ณ์์ ๋จ์ด์ ๋ถ์ฐ ํํ์ ์ ์ฅํ๋ค. self.word_vecs = W_in
์ธ์๋ก๋ ์ดํ ์(vocab_size)์ ์๋์ธต์ ๋ด๋ฐ ์(hidden_size)๋ฅผ ๋ฐ๋๋ค.
๊ฐ์ค์น๋ 2๊ฐ(W_in, W_out)๋ฅผ ์์ฑํ๊ณ ๋ฌด์์ ๊ฐ์ผ๋ก ์ด๊ธฐํํ๋ค.
ํ์ํ ๊ณ์ธต(์ ๋ ฅ์ธก2๊ฐ, ์ถ๋ ฅ์ธก 1๊ฐ)์ ์์ฑํ๋ค.
Softmax with Loss ๊ณ์ธต์ ์์ฑํ๋ค.
์ ๋ ฅ ์ธก์ ๋งฅ๋ฝ์ ์ฒ๋ฆฌํ๋ MatMul๊ณ์ธต์ ๋งฅ๋ฝ์์ ์ฌ์ฉํ๋ ๋จ์ด์ ์๋งํผ ๋ง๋ค๊ณ ์ด๋ค์ ๋ชจ๋ ๊ฐ์ ๊ฐ์ค์น๋ฅผ ์ด์ฉ.
์ ๊ฒฝ๋ง์์ ์ฌ์ฉ๋๋ ๋งค๊ฐ๋ณ์์ ๊ธฐ์ธ๊ธฐ๋ฅผ params, grads๋ฆฌ์คํธ์ ๊ฐ๊ฐ ๋ชจ์๋๋ค.
์ ๊ฒฝ๋ง์ ์์ ํ์ธ forward()๋ฉ์๋๋ฅผ ๊ตฌํํ๋ค. ์ธ์๋ก ๋งฅ๋ฝ๊ณผ ํ๊น์ ๋ฐ์ ์์ค์ ๋ฐํํ๋ค.
def forward(self, contexts, target): h0 = self.in_layer0.forward(contexts[:, 0]) h1 = self.in_layer1.forward(contexts[:, 1]) h = (h0 + h1) * 0.5 score = self.out_layer.forward(h) loss = self.loss_layer.forward(score, target) return loss
contexts๋ ์ ์์์ (6,2,7)์ด ๋๋๋ฐ
0๋ฒ์งธ ์ฐจ์์ ์์ ์๋ ๋ฏธ๋๋ฐฐ์น์ ์,
1๋ฒ์งธ ์ฐจ์์ ์์ ์๋ ๋งฅ๋ฝ ์๋์ฐ ํฌ๊ธฐ
2๋ฒ์งธ ์ฐจ์์ ์ ํซ ๋ฒกํฐ
target์ ์ ์์์ (6,7)๊ณผ ๊ฐ์ ํ์์ด ๋๋ค.

def backward(self, dout=1): ds = self.loss_layer.backward(dout) da = self.out_layer.backward(ds) da *= 0.5 self.in_layer1.backward(da) self.in_layer0.backward(da) return None
skip- gram ๋ชจ๋ธ
- ํ๊น์ผ๋ก๋ถํฐ ์ฃผ๋ณ ๋จ์ด(๋งฅ๋ฝ)์ ์ถ์ธกํ๋ ๊ฒ์ ์๋ฏธํจ.

- ์ ๋ ฅ์ธต์ ํ๊ฐ
- ์ถ๋ ฅ์ธต์ ๋๊ฐ (๋งฅ๋ฝ์ด 2๊ฐ)


- ๋์ ๋น๊ตํด๋ณด๋ฉด, skip-gram์ด ์ฑ๋ฅ์ด ๋ ๋ฐ์ด๋๊ณ , cbow๋ ํ์ต ์๋๊ฐ ๋น ๋ฆ.
- skip์ ๊ฒฝ์ฐ ์์ค๊ฐ์ ๋งฅ๋ฝ ์๋งํผ ๊ตฌํด์ผ ํ๊ธฐ์ ์๋๋ cbow๋ณด๋ค ๋๋ฆผ
- ํ์ง๋ง ํ๊น์ ๊ธฐ์ค์ผ๋ก ๋งฅ๋ฝ์ ๋ฝ์๋ธ๋ค๋ ๊ฒ ์์ฒด๊ฐ ์ด๋ ค์ด ์ผ ⇒ skip ๋ชจ๋ธ์ด ๋ ๋ฐ์ด๋๋ค๋ ์ฅ์ ์ด ์์
→ ์ํฉ์ ๋ฐ๋ผ ์ฌ์ฉํ๊ธธ.
- ์ถ๋ก ๊ธฐ๋ฐ๊ธฐ๋ฒ+ ํต๊ณ๊ธฐ๋ฐ๊ธฐ๋ฒ : glove ๊ธฐ๋ฒ๋ ์กด์ฌํจ.
- ๋ง๋ญ์น ์ ์ฒด์ ํต๊ณ ์ ๋ณด๋ฅผ ์์ค ํจ์์ ๋์ ํด ๋ฏธ๋๋ฐฐ์น ํ์ต์ ํ๋ ๊ฒ.