์์ ๊ธ, Thesaurus(์์๋ฌ์ค), Co-occurence Matrix(๋์๋ฐ์ ํ๋ ฌ)๋ถ๋ถ์์ ํต๊ณ ๊ธฐ๋ฐ ๊ธฐ๋ฒ์ ๋ฐํ์ฌ ์ค๋ช ํ์ต๋๋ค.
- Thesaurus(์์๋ฌ์ค), Co-occurence Matrix(๋์๋ฐ์ ํ๋ ฌ) ๊ธ์ ๋๋ค. ์ง๊ธ ๋ด์ฉ๊ณผ ์ฐ๊ฒฐ๋๋ ๊ธ์ด๋๊น ํ๋ฒ ์ฝ์ด๋ณด์ธ์.
[NLP] Thesaurus(์์๋ฌ์ค), Co-occurence Matrix(๋์๋ฐ์ ํ๋ ฌ)
์ค๋๋ง์ NLP ๊ด๋ จ ๊ธ์ ์ฐ๋ค์.. ์๊ฐ ๋๋๋๋ก ์ด์ฌํ ์ฐ๊ณ ์ฌ๋ ค ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. Thesaursus - ์์๋ฌ์ค์์๋ฌ์ค(Thesaurus)๋ ๋จ์ด์ ๊ทธ ์๋ฏธ๋ฅผ ์ฐ๊ฒฐ์์ผ์ฃผ๋ ๋๊ตฌ์ ๋๋ค.์ฃผ๋ก ํน์ ๋จ์ด์ ์๋ฏธ
daehyun-bigbread.tistory.com
Pointwise Mutual Information (PMI) - ์ ๋ณ ์ํธ์ ๋ณด๋
- ๋์๋ฐ์ ํ๋ ฌ์ ์์๋ ๋ ๋จ์ด๊ฐ ๋์์ ๋ฐ์ํ ํ์๋ฅผ ๋ํ๋ ๋๋ค.
- ๊ทผ๋ฐ '๋ฐ์' ํ์๋ผ๋ ํํ์ ์ข์ ํน์ง์ ์๋๋๋ค. ๋ง์ด ๋์ค๋ ๋จ์ด(๊ณ ๋น๋ ๋จ์ด)๋ฅผ ์๊ฐํด๋ณด๋ฉด ์ ์ ์์ต๋๋ค.
- ๋จ์ํ ๋ฑ์ฅ ํ์๋ก ๊ด๋ จ์ฑ์ ํ๊ฐํ๋ค๋ฉด, ๋ง์ด ๋์ค๋ ๋จ์ด๋ค์ ์ฐ๊ด์ฑ์ด ๋๋ค๊ณ ์๊ฐํ ์ ์๊ฒ ์ฃ . ์ค์ ๋ก ๋ณด๋ฉด ๊ด๋ จ์ด ์๋ ๋จ์ด์ผ์๋ ์์ต๋๋ค.
- ๊ทธ๋์ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์ Pointwise Mutual Information(PMI), ์ ๋ณ ์ํธ์ ๋ณด๋ ์ด๋ผ๋ ์ฒ๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- Pointwise Mutual Information(PMI)๋ ํ๋ฅ ๋ณ์ x์ y์ ๋ํด ๋ค์์์ผ๋ก ์ ์๋ฉ๋๋ค.
- ์ ๋ปํฉ๋๋ค. ๋ ๐ฅ๊ฐ ์ผ์ด๋ ํ๋ฅ , ๋ ๐ฆ๊ฐ ์ผ์ด๋ ํ๋ฅ , ๐(๐ฅ,๐ฆ)๋ ๐ฅ์ ๐ฆ๊ฐ ๋์์ ์ผ์ด๋ ํ๋ฅ
- ์ด PMI ๊ฐ์ด ๋์์๋ก ๊ด๋ จ์ฑ์ด ๋๋ค๋ ์๋ฏธ์ ๋๋ค.
- ์ด ์์ ์ ์ฉํ๋ฉด ๐(๐ฅ)๋ ๋จ์ด ๐ฅ๊ฐ Corpus(๋ง๋ญ์น)์ ๋ฑ์ฅํ ํ๋ฅ ์ ๊ฐ๋ฆฌํต๋๋ค.
- ์์ปจ๋ 10,000๊ฐ์ ๋จ์ด๋ก ์ด๋ฃจ์ด์ง ๋ง๋ญ์น์์ "the"๊ฐ 100๋ฒ ๋ฑ์ฅํ๋ค๋ฉด?
- ๐("๐กโ๐") = 100/10000 = 0.01์ด ๋ฉ๋๋ค.
- ๋ํ ๐(๐ฅ,๐ฆ)๋ ๋จ์ด ๐ฅ์ ๐ฆ๊ฐ ๋์๋ฐ์ํ ํ๋ฅ ์ด๋ฏ๋ก, ๋ง์ฐฌ๊ฐ์ง๋ก "the"์ "car"๊ฐ 10๋ฒ ๋์๋ฐ์ ํ๋ค๋ฉด?
- ๐("๐กโ๐","๐๐๐")= 10/10000 =0.001 ์ด ๋๋ ๊ฒ์ด์ฃ .
- ๊ทธ๋ผ Co-occurence Matrix (๋์๋ฐ์ ํ๋ ฌ - ๊ฐ ์์๋ ๋์๋ฐ์ํ ๋จ์ด์ ํ์)์ ์ฌ์ฉํ์ฌ ์์ ๋ฅผ ๋ค์ ์จ๋ด ์๋ค.
- ๋ Co-occurence Matrix (๋์๋ฐ์ ํ๋ ฌ), ๐ถ(๐ฅ,๐ฆ)๋ ๋จ์ด ๐ฅ์ ๐ฆ๊ฐ ๋์๋ฐ์ํ๋ ํ์, ๐ถ(๐ฅ)์ ๐ถ(๐ฆ)๋ ๊ฐ๊ฐ ๋จ์ด ๐ฅ์ ๐ฆ์ ๋ฑ์ฅ ํ์์ ๋๋ค.
- ์ด๋ Corpus(๋ง๋ญ์น)์ ํฌํจ๋ ๋จ์ด ์๋ฅผ ๐์ด๋ผ ํ๋ฉด, ์๋ ๋ค์๊ณผ ๊ฐ์ด ๋ณํฉ๋๋ค.
- ์ด๋ ๊ฒ Pointwise Mutual Information(PMI)๋ผ๋ ์ฒ๋๋ฅผ ์๊ฒ๋์์ต๋๋ค.
- ๊ทผ๋ฐ, ๋ฌธ์ ๊ฐ Co-occurence(๋์ ๋ฐ์ ํ์)๊ฐ 0์ด๋ฉด logโก20=−∞ ๊ฐ ๋ฉ๋๋ค.
- ์ด ๋ฌธ์ ๋ฅผ ํผํ๊ธฐ ์ํด ์ค์ ๋ก ๊ตฌํํ ๋๋ ์์ ์ํธ์ ๋ณด๋(Positive PMI, PPMI)์ ์ฌ์ฉํฉ๋๋ค.
- ์ด ์์ ๋ฐ๋ผ PMI๊ฐ ์์์ผ ๋๋ 0์ผ๋ก ์ทจ๊ธํฉ๋๋ค.
- ์ด์ ๋จ์ด ์ฌ์ด์ ๊ด๋ จ์ฑ์ 0 ์ด์์ ์ค์๋ก ๋ํ๋ผ ์ ์์ต๋๋ค.
- ๊ทธ๋ฌ๋ฉด Co-occurence Matrix(๋์๋ฐ์ ํ๋ ฌ)์ PPMI ํ๋ ฌ๋ก ๋ณํํ๋ ํจ์๋ฅผ ๊ตฌํํด๋ด ์๋ค.
def ppmi(C, verbose=False, eps = 1e-8):
'''PPMI(์ ๋ณ ์ํธ์ ๋ณด๋) ์์ฑ
:param C: ๋์๋ฐ์ ํ๋ ฌ
:param verbose: ์งํ ์ํฉ์ ์ถ๋ ฅํ ์ง ์ฌ๋ถ
:return:
'''
M = np.zeros_like(C, dtype=np.float32)
N = np.sum(C)
S = np.sum(C, axis=0)
total = C.shape[0] * C.shape[1]
cnt = 0
for i in range(C.shape[0]):
for j in range(C.shape[1]):
pmi = np.log2(C[i, j] * N / (S[j]*S[i]) + eps)
M[i, j] = max(0, pmi)
if verbose:
cnt += 1
if cnt % (total//100 + 1) == 0:
print('%.1f%% ์๋ฃ' % (100*cnt/total))
return M
- ์ฌ๊ธฐ์์ ์ธ์ C๋ Co-occurence Matrix(๋์๋ฐ์ ํ๋ ฌ), verbose๋ ์งํ์ํฉ ์ถ๋ ฅ์ ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ๋ ํ๋๊ทธ์ ๋๋ค.
- ํฐ Corpus๋ฅผ ๋ค๋ฃฐ ๋ verbose=True๋ก ์ค์ ํ๋ฉด ์ค๊ฐ์ค๊ฐ ์งํ ์ํฉ์ ์๋ ค์ฃผ์ฃ .
- ์ฐธ๊ณ ๋ก, ์ด ์ฝ๋๋ Co-occurence Matrix(๋์๋ฐ์ ํ๋ ฌ)์ ๋ํด์๋ง PPMI ํ๋ ฌ์ ๊ตฌํ ์ ์๋๋ก ํ๊ณ ์ ๋จ์ํ๊ฒ ๊ตฌํํ์ต๋๋ค.
- ๊ตฌ์ฒด์ ์ผ๋ก ๋งํ๋ฉด, ๋จ์ด ๐ฅ์ ๐ฆ๊ฐ ๋์์ ๋ฐ์ํ๋ ํ์๋ฅผ ๐ถ(๐ฅ,๐ฆ)๋ผ ํ์ ๋?
- ์์ ์์์ฒ๋ผ ๋๋๋ก. ์ฆ, ๊ทผ์ฌ๊ฐ์ ๊ตฌํ๋๋ก ๊ตฌํํ์ต๋๋ค.
- ๊ทธ๋ฌ๋ฉด Co-occurence Matrix(๋์๋ฐ์ ํ๋ ฌ)์ PPMI ํ๋ ฌ๋ก ๋ณํํด ๋ณด๊ฒ ์ต๋๋ค.
import sys
sys.path.append('..')
import numpy as np
from common.util import preprocess, create_co_matrix, cos_similarity, ppmi
text = 'You say goodbye and I say hello.'
corpus, word_to_id, id_to_word = preprocess(text)
vocab_size = len(word_to_id)
C = create_co_matrix(corpus, vocab_size)
W = ppmi(C)
np.set_printoptions(precision=3) # ์ ํจ ์๋ฆฟ์๋ฅผ ์ธ ์๋ฆฌ๋ก ํ์
print('๋์๋ฐ์ ํ๋ ฌ')
print(C)
print('-'*50)
print('PPMI')
print(W)
- ์ฝ๋์ ์คํ ๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ์ต๋๋ค.
๋์๋ฐ์ ํ๋ ฌ
[[0 1 0 0 0 0 0]
[1 0 1 0 1 1 0]
[0 1 0 1 0 0 1]
[0 0 1 0 1 0 0]
[0 1 0 1 0 1 0]
[0 1 0 0 1 0 1]
[0 0 0 0 0 1 0]]
PPMI
[[0. 1.807 0. 0. 0. 0. 0. ]
[1.807 0. 0.807 0. 0.807 0.807 0. ]
[0. 0.807 0. 1.807 0. 0. 1.807]
[0. 0. 1.807 0. 1.807 0. 0. ]
[0. 0.807 0. 1.807 0. 0.807 0. ]
[0. 0.807 0. 0. 0.807 0. 2.807]
[0. 0. 0. 0. 0. 2.807 0. ]]
- ์ด๊ฒ์ผ๋ก Co-occurence Matrix(๋์๋ฐ์ ํ๋ ฌ)์ PPMI ํ๋ ฌ๋ก ๋ณํํ๋ ๋ฒ์ ์์๋ดค์ต๋๋ค.
- ์ด๋ PPMI ํ๋ ฌ์ ๊ฐ ์์๋ 0 ์ด์์ ์ค์์ ๋๋ค. ์ด์ ๋ ์ข์ ์ฒ๋๋ก ์ด๋ค์ง Matrix(๋ ์ข์ ๋จ์ด Vector)์ ์์ ์ฅ์์ต๋๋ค.
- ๊ทธ๋ฌ๋ PPMI ํ๋ ฌ์๋ ์ฌ์ ํ ํฐ ๋ฌธ์ ๊ฐ ์์ต๋๋ค!
- Corpus(๋ง๋ญ์น)์ ์ดํ ์๊ฐ ์ฆ๊ฐํจ์ ๋ฐ๋ผ ๊ฐ ๋จ์ด Vector์ Dimension(์ฐจ์) ์๋ ์ฆ๊ฐํ๋ค๋ ๋ฌธ์ ์ฃ .
- ์๋ฅผ ๋ค์ด Corpus(๋ง๋ญ์น)์ ์ดํ ์๊ฐ 10๋ง ๊ฐ๋ผ๋ฉด ๊ทธ ๋ฒกํฐ์ ์ฐจ์ ์๋ ๋๊ฐ์ด 10๋ง์ด ๋ฉ๋๋ค.
- 10๋ง ์ฐจ์์ ๋ฒกํฐ๋ฅผ ๋ค๋ฃฌ๋ค๋ ๊ฒ์ ๊ทธ๋ค์ง ํ์ค์ ์ด์ง ์์ต๋๋ค.
- ๋ํ, ์ด ํ๋ ฌ์ ๋ด์ฉ์ ๋ค์ฌ๋ค๋ณด๋ฉด ์์ ๋๋ถ๋ถ์ด 0์ธ ๊ฒ์ ์ ์ ์์ต๋๋ค.
- Vector์ ์์ ๋๋ถ๋ถ์ด ์ค์ํ์ง ์๋ค๋ ๋ป์ด์ฃ . ๋ค๋ฅด๊ฒ ํํํ๋ฉด ๊ฐ ์์์ '์ค์๋'๊ฐ ๋ฎ๋ค๋ ๋ป์ ๋๋ค.
- ๋์ฑ์ด ์ด๋ฐ Vector๋ ๋ ธ์ด์ฆ์ ์ฝํ๊ณ ๊ฒฌ๊ณ ํ์ง ๋ชปํ๋ ์ฝ์ ๋ ์์ง์.
- ์ด ๋ฌธ์ ์ ๋์ฒํ๊ณ ์ ์์ฃผ ์ํํ๋ ๊ธฐ๋ฒ์ด ๋ฐ๋ก ๋ฒกํฐ์ Dimension Reduction(์ฐจ์ ๊ฐ์)์ ๋๋ค.
Dimension Reduction - ์ฐจ์ ๊ฐ์
์ฐจ์ ๊ฐ์(dimensionality reduction)๋ ๋ฌธ์ ๊ทธ๋๋ก ๋ฒกํฐ์ ์ฐจ์์ ์ค์ด๋ ๋ฐฉ๋ฒ์ ๋งํฉ๋๋ค.
- ๊ทธ๋ฌ๋ ๋จ์ํ ์ค์ด๊ธฐ๋ง ํ๋ ๊ฒ ์๋๋ผ, '์ค์ํ ์ ๋ณด'๋ ์ต๋ํ ์ ์งํ๋ฉด์ ์ค์ด๋ ๊ฒ ํต์ฌ์ ๋๋ค.
- ์ง๊ด์ ์ธ ์๋ก ์๋์ ๊ทธ๋ํ๋ฅผ ๋ณด๋ฉด, ๋ฐ์ดํฐ์ ๋ถํฌ๋ฅผ ๊ณ ๋ คํด์ ์ค์ํ '์ถ'์ ์ฐพ๋ ์ผ์ ์ํํฉ๋๋ค.
- ์ผ์ชฝ์ ๋ฐ์ดํฐ์ ๋ค์ 2์ฐจ์ ์ขํ๊ณ์ ํ์ํ ๋ชจ์ต์ ๋๋ค.
- ๊ทธ๋ฆฌ๊ณ ์ค๋ฅธ์ชฝ์ ์๋ก์ด ์ถ์ ๋์ ํ์ฌ ๋๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ขํ์ถ ํ๋๋ง์ผ๋ก ํ์ํ์ต๋๋ค(์๋ก์ด ์ถ์ ์ฐพ์ ๋๋ ๋ฐ์ดํฐ๊ฐ ๋๊ฒ ๋ถํฌ๋๋๋ก ๊ณ ๋ คํด์ผ ํฉ๋๋ค).
- ์ด๋ ๊ฐ ๋ฐ์ดํฐ์ ์ ๊ฐ์ ์๋ก์ด ์ถ์ผ๋ก ์ฌ์๋ ๊ฐ์ผ๋ก ๋ณํ๋ฉ๋๋ค.
- ์ฌ๊ธฐ์ ์ค์ํ ๊ฒ์ ๊ฐ์ฅ ์ ํฉํ ์ถ์ ์ฐพ์๋ด๋ ์ผ๋ก, 1์ฐจ์ ๊ฐ๋ง์ผ๋ก๋ ๋ฐ์ดํฐ์ ๋ณธ์ง์ ์ธ ์ฐจ์ด๋ฅผ ๊ตฌ๋ณํ ์ ์์ด์ผ ํฉ๋๋ค.
- ์ด์ ๊ฐ์ ์์ ์ ๋ค์ฐจ์ ๋ฐ์ดํฐ์ ๋ํด์๋ ์ํํ ์ ์์ต๋๋ค.
- ์ฐจ์์ ๊ฐ์์ํค๋ ๋ฐฉ๋ฒ์ ์ฌ๋ฌ ๊ฐ์ง์ ๋๋ค๋ง, ์ด๋ฒ์๋ ํน์๊ฐ๋ถํด(Singular Value Decomposition, SVD)๋ฅผ ์ด์ฉํ๊ฒ ์ต๋๋ค.
- ํน์๊ฐ๋ถํด(Singular Value Decomposition, SVD)๋ ์์์ ํ๋ ฌ์ ์ธ ํ๋ ฌ์ ๊ณฑ์ผ๋ก ๋ถํดํ๋ฉฐ, ์์์ผ๋ก๋ ๋ค์๊ณผ ๊ฐ์ด ๋ํ๋ ๋๋ค.
- ๊ฐ์ด SVD๋ ์์์ ํ๋ ฌ ๐๋ฅผ ๐,๐,๐ ๋ผ๋ ์ธ ํ๋ ฌ์ ๊ณฑ์ผ๋ก ๋ถํดํฉ๋๋ค.
- ์ฌ๊ธฐ์ ๐์ ๐๐๋ ์ง๊ตํ๋ ฌ(orthogonal matrix)์ด๊ณ , ๊ทธ ์ด๋ฒกํฐ๋ ์๋ก ์ง๊ตํฉ๋๋ค.
- ๋ํ ๐๋ ๋๊ฐํ๋ ฌ(diagonal matrix)(๋๊ฐ์ฑ๋ถ ์ธ์๋ ๋ชจ๋ 0์ธ ํ๋ ฌ)์ ๋๋ค.
- ์ด ์์์ ์๊ฐ์ ์ผ๋ก ํํํ ๊ฒ์ด ์๋์ ๊ทธ๋ฆผ์ ๋๋ค.
- ๐๋ ์ง๊ตํ๋ ฌ์ ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ด ์ง๊ตํ๋ ฌ์ ์ด๋ ํ ๊ณต๊ฐ์ ์ถ(๊ธฐ์ )์ ํ์ฑํฉ๋๋ค.
- ์ง๊ธ ์ฐ๋ฆฌ์ Corpus(๋ง๋ญ์น)์์๋ ์ด ๐ ํ๋ ฌ์ '๋จ์ด ๊ณต๊ฐ'์ผ๋ก ์ทจ๊ธํ ์ ์์ฃ .
- ๋ํ ๐๋ ๋๊ฐํ๋ ฌ๋ก, ๊ทธ ๋๊ฐ์ฑ๋ถ์๋ 'ํน์๊ฐ'(singular value)์ด ํฐ ์์๋ก ๋์ด๋์ด ์์ต๋๋ค.
- ํน์๊ฐ์ด๋, ์ฝ๊ฒ ๋งํด 'ํด๋น ์ถ์ ์ค์๋'๋ผ๊ณ ๊ฐ์ฃผํ ์ ์์ต๋๋ค.
- ๊ทธ๋์ ๊ฐ์ด ์ค์๋๊ฐ ๋ฎ์ ์์(ํน์๊ฐ์ด ์์ ์์)๋ฅผ ๊น์๋ด๋ ๋ฐฉ๋ฒ์ ์๊ฐํ ์ ์์ต๋๋ค.
- ํ๋ ฌ ๐์์ ํน์๊ฐ์ด ์๋ค๋ฉด ์ค์๋๊ฐ ๋ฎ๋ค๋ ๋ป์ด๋ฏ๋ก, ํ๋ ฌ ๐์์๋ ์ฌ๋ถ์ ์ดVector๋ฅผ ๊น์๋ด์ด ์๋์ ํ๋ ฌ์ ๊ทผ์ฌํ ์ ์์ต๋๋ค.
- ์ด๋ฅผ ์ฐ๋ฆฌ ๋ฌธ์ ๋ก ๊ฐ์ ธ์์ '๋จ์ด์ PPMI ํ๋ ฌ'์ ์ ์ฉํด๋ณผ๊น์?
- ๊ทธ๋ฌ๋ฉด ํ๋ ฌ ๐ ์ ๊ฐ ํ์๋ ํด๋น ๋จ์ด ID์ ๋จ์ด ๋ฒกํฐ๊ฐ ์ ์ฅ๋์ด ์์ผ๋ฉฐ,
- ๊ทธ ๋จ์ด ๋ฒกํฐ๊ฐ ํ๋ ฌ ๐′๋ผ๋ ์ฐจ์ ๊ฐ์๋ ๋ฒกํฐ๋ก ํํ๋๋ ๊ฒ์ ๋๋ค.
๋จ์ด์ ๋์๋ฐ์ ํ๋ ฌ์ ์ ๋ฐฉํ๋ ฌ์ด์ง๋ง, ์ดํด ํ๊ธฐ ์ฝ๊ฒ ์ง์ฌ๊ฐํ์ผ๋ก ๊ทธ๋ ธ์ต๋๋ค.
๋ํ ์ฌ๊ธฐ์์๋ SVD๋ฅผ ์ง๊ด์ ์ด๊ณ ๊ฐ๋ตํ๊ฒ๋ง ์ค๋ช ํ์ต๋๋ค.
SVD (ํน์๊ฐ๋ถํด)์ ์ํ Dimension Reduction(์ฐจ์ ๊ฐ์)
์ด์ SVD๋ฅผ ํ์ด์ฌ ์ฝ๋๋ก ์ดํด๋ด ์๋ค.
- ์ด์ SVD๋ฅผ ํ์ด์ฌ ์ฝ๋๋ก ์ดํด๋ด ์๋ค.
- SVD๋ ๋ํ์ด์ linalg ๋ชจ๋์ด ์ ๊ณตํ๋ svd ๋ฉ์๋๋ก ์คํํ ์ ์์ต๋๋ค.
- ์ฐธ๊ณ ๋ก, "linalg"๋ ์ ํ๋์(linear algebra)์ ์ฝ์ด์ ๋๋ค.
- ๊ทธ๋ผ, ๋์๋ฐ์ ํ๋ ฌ์ ๋ง๋ค์ด PPMI ํ๋ ฌ๋ก ๋ณํํ ๋ค์ SVD๋ฅผ ์ ์ฉํด๋ด ์๋ค.
import sys
sys.path.append('..')
import numpy as np
import matplotlib.pyplot as plt
from common.util import preprocess, create_co_matrix, ppmi
text = 'You say goodbye and I say hello.'
corpus, word_to_id, id_to_word = preprocess(text)
vocab_size = len(id_to_word)
C = create_co_matrix(corpus, vocab_size, window_size=1)
W = ppmi(C)
# SVD
U, S, V = np.linalg.svd(W)
print(C[0]) # ๋์๋ฐ์ ํ๋ ฌ
# [0 1 0 0 0 0 0]
print(W[0]) # PPMI ํ๋ ฌ
# [0. 1.807 0. 0. 0. 0. 0. ]
print(U[0]) # SVD
# [ 3.409e-01 -1.110e-16 -1.205e-16 -4.441e-16 0.000e+00 -9.323e-01
# 2.226e-16]
print(U[0, :2])
# [ 3.409e-01 -1.110e-16]
- ์ด ๊ฒฐ๊ณผ์์ ๋ณด๋ฏ ์๋๋ ํฌ์ Vector์ธ ๐[0]๊ฐ SVD์ ์ํด์ ๋ฐ์ง Vector ๐[0]๋ก ๋ณํ๋์์ต๋๋ค.
- ๊ทธ๋ฆฌ๊ณ ์ด ๋ฐ์ง Vector์ Dimension(์ฐจ์)์ Reduction(๊ฐ์)์ํค๋ ค๋ฉด, ์์ปจ๋ 2-Dimension Vector(2์ฐจ์ ๋ฒกํฐ) ์ค์ด๋ ค๋ฉด ๋จ์ํ ์ฒ์์ ๋ ์์๋ฅผ ๊บผ๋ด๋ฉด ๋ฉ๋๋ค.
print(U[0, :2])
# [ 3.409e-01 -1.110e-16]
- ๊ทธ๋ฌ๋ฉด ๊ฐ ๋จ์ด๋ฅผ 2์ฐจ์ ๋ฒกํฐ๋ก ํํํ ํ ๊ทธ๋ํ๋ก ๊ทธ๋ ค๋ด ์๋ค.
for word, word_id in word_to_id.items():
plt.annotate(word, (U[word_id, 0], U[word_id, 1]))
plt.scatter(U[:, 0], U[:, 1], alpha=0.5)
plt.show()
- ๋ณด๋ฉด "goodbye"์ "hello", "you"์ "i"๊ฐ ์ ๋ฒ ๊ฐ๊น์ด ์์์ ์ ์ ์์ต๋๋ค.
- ์ฐ๋ฆฌ์ ์๊ฐ๊ณผ ๋น๊ต์ ๋น์ทํ์ฃ . ํ์ง๋ง ์ง๊ธ ์ฌ์ฉํ Corpus(๋ง๋ญ์น)๊ฐ ์์ฃผ ์์์ ์ด ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋๋ก ๋ฐ์๋ค์ด๊ธฐ์๋ ์ข ๊ทธ๋ ์ต๋๋ค.
- ๊ทธ๋์ PTB Dataset์ด๋ผ๋ ๋ ํฐ Corpus(๋ง๋ญ์น)๋ฅผ ์ฌ์ฉํด์ ํด๋ณด๊ฒ ์ต๋๋ค.
PTB Dataset
์ด๋ฒ์๋ ์ ๋นํ ํฐ? Corpus(๋ง๋ญ์น)์ธ ํ ํธ๋ฆฌ๋ฑ ํฌ(Penn Treebank, PTB) Dataset์ ํ์ฉํด ๋ณด๊ฒ ์ต๋๋ค.
PTB ๋ง๋ญ์น๋ ์ฃผ์ด์ง ๊ธฐ๋ฒ์ ํ์ง์ ์ธก์ ํ๋ ๋ฒค์น๋งํฌ๋ก ์์ฃผ ์ด์ฉ๋ฉ๋๋ค.
- ์ด PTB ๋ง๋ญ์น๋ ํ ์คํธ ํ์ผ๋ก ์ ๊ณต๋๋ฉฐ, ์๋์ PTB ๋ฌธ์ฅ์ ๋ช ๊ฐ์ง ์ ์ฒ๋ฆฌ๋ฅผ ํด๋์์ต๋๋ค.
- ์์ปจ๋ ํฌ์ํ ๋จ์ด๋ฅผ <unk>๋ผ๋ ํน์ ๋ฌธ์๋ก ์นํํ๋ค๊ฑฐ๋ <unk>๋ "unknown"์ ์ฝ์ด, ๊ตฌ์ฒด์ ์ธ ์ซ์๋ฅผ "N"์ผ๋ก ๋์ฒดํ๋ ๋ฑ์ ์์ ์ด ์ ์ฉ๋์์ต๋๋ค.
1 consumers may want to move their telephones a little closer to the tv set
2 <unk> <unk> watching abc 's monday night football can now vote during <unk> for the greatest play in N years from among four or five <unk> <unk>
3 two weeks ago viewers of several nbc <unk> consumer segments started calling a N number for advice on various <unk> issues
4 and the new syndicated reality show hard copy records viewers ' opinions for possible airing on the next day 's show
5 interactive telephone technology has taken a new leap in <unk> and television programmers are racing to exploit the possibilities
6 eventually viewers may grow <unk> with the technology and <unk> the cost
- ์์์ ๋ณด๋ฏ PTB ๋ง๋ญ์น์์๋ ํ ๋ฌธ์ฅ์ด ํ๋์ ์ค๋ก ์ ์ฅ๋์ด ์์ต๋๋ค.
- ์ด ์ฑ ์์๋ ๊ฐ ๋ฌธ์ฅ์ ์ฐ๊ฒฐํ 'ํ๋์ ํฐ ์๊ณ์ด ๋ฐ์ดํฐ'๋ก ์ทจ๊ธํฉ๋๋ค.
- ์ด๋ ๊ฐ ๋ฌธ์ฅ ๋์ <eos>๋ผ๋ ํน์ ๋ฌธ์๋ฅผ ์ฝ์ ํฉ๋๋ค.
- ๋ค์์ ptb.py๋ฅผ ์ฌ์ฉํ๋ ์์ ๋๋ค.
import sys
sys.path.append('..')
from dataset import ptb
corpus, word_to_id, id_to_word = ptb.load_data('train')
print('๋ง๋ญ์น ํฌ๊ธฐ:', len(corpus))
print('corpus[:30]:', corpus[:30])
print()
print('id_to_word[0]:', id_to_word[0])
print('id_to_word[1]:', id_to_word[1])
print('id_to_word[2]:', id_to_word[2])
print()
print("word_to_id['car']:", word_to_id['car'])
print("word_to_id['happy']:", word_to_id['happy'])
print("word_to_id['lexus']:", word_to_id['lexus'])
- ์๋๋ ์คํ ๊ฒฐ๊ณผ์ ๋๋ค.
corpus size: 929589
corpus[:30]: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]
id_to_word[0]: aer
id_to_word[1]: banknote
id_to_word[2]: berlitz
word_to_id['car']: 3856
word_to_id['happy']: 4428
word_to_id['lexus']: 7426
- ๋ง๋ญ์น๋ฅผ ๋ค๋ฃจ๋ ๋ฐฉ๋ฒ์ ์ง๊ธ๊น์ง์ ๊ฐ์ต๋๋ค.
- corpus์๋ ๋จ์ด ID ๋ชฉ๋ก์ด ์ ์ฅ๋ฉ๋๋ค.
- id_to_word๋ ๋จ์ด ID์์ ๋จ์ด๋ก ๋ณํํ๋ ๋์ ๋๋ฆฌ์ด๊ณ , word_to_id๋ ๋จ์ด์์ ๋จ์ด ID๋ก ๋ณํํ๋ ๋์ ๋๋ฆฌ์ ๋๋ค.
PTB Dataset ํ๊ฐ
PTB ๋ฐ์ดํฐ์ ์ ํต๊ณ ๊ธฐ๋ฐ ๊ธฐ๋ฒ์ ์ ์ฉํด๋ด ์๋ค.
- ์ด๋ฒ์๋ ํฐ ํ๋ ฌ์ SVD๋ฅผ ์ ์ฉํด์ผ ํ๋ฏ๋ก ๊ณ ์ SVD๋ฅผ ์ด์ฉํ ๊ฒ์ ์ถ์ฒํฉ๋๋ค.
import sys
sys.path.append('..')
import numpy as np
from common.util import most_similar, create_co_matrix, ppmi
from dataset import ptb
window_size = 2
wordvec_size = 100
corpus, word_to_id, id_to_word = ptb.load_data('train')
vocab_size = len(word_to_id)
print('๋์๋ฐ์ ์ ๊ณ์ฐ ...')
C = create_co_matrix(corpus, vocab_size, window_size)
print('PPMI ๊ณ์ฐ ...')
W = ppmi(C, verbose=True)
print('SVD ๊ณ์ฐ ...')
try:
from sklearn.utils.extmath import randomized_svd
U, S, V = randomized_svd(W, n_components=wordvec_size, n_iter=5, random_state=None)
except ImportError:
U, S, V = np.linalg.svd(W)
word_vecs = U[:, :wordvec_size]
querys = ['you', 'year', 'car', 'toyota']
for query in querys:
most_similar(query, word_to_id, id_to_word, word_vecs, top=5)
- SVD๋ฅผ ์ํํ๋ ๋ฐ sklearn์ randomized_svd() Method๋ฅผ ์ด์ฉํ์ต๋๋ค.
- ์ด Method๋ ๋ฌด์์ ์๋ฅผ ์ฌ์ฉํ Truncated SVD๋ก, ํน์๊ฐ์ด ํฐ ๊ฒ๋ค๋ง ๊ณ์ฐํ์ฌ ๊ธฐ๋ณธ์ ์ธ SVD๋ณด๋ค ํจ์ฌ ๋น ๋ฆ ๋๋ค.
- ๋๋จธ์ง ๋ถ๋ถ์ ์์ ์์ ๋ง๋ญ์น๋ฅผ ์ฌ์ฉํ ์ฝ๋์ ๊ฑฐ์ ๊ฐ์ต๋๋ค.
- ์คํ ๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ์ต๋๋ค.
[query] you
i: 0.702039909619
we: 0.699448543998
ve: 0.554828709147
do: 0.534370693098
else: 0.512044146526
[query] year
month: 0.731561990308
quarter: 0.658233992457
last: 0.622425716735
earlier: 0.607752074689
next: 0.601592506413
[query] car
luxury: 0.620933665528
auto: 0.615559874277
cars: 0.569818364381
vehicle: 0.498166879744
corsica: 0.472616831915
[query] toyota
motor: 0.738666107068
nissan: 0.677577542584
motors: 0.647163210589
honda: 0.628862379043
lexus: 0.604740429865
- ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด, ์ฐ์ 'you'๋ผ๋ ๊ฒ์์ด์์๋ ์ธ์นญ๋๋ช ์ฌ์ธ 'i'์ 'we'๊ฐ ์์๋ฅผ ์ฐจ์งํ์์ ์ ์ ์์ต๋๋ค.
- ์์ญ ๋ฌธ์ฅ์์๋ ๊ด์ฉ์ ์ผ๋ก ์์ฃผ ๊ฐ์ด ๋์ค๋ ๋จ์ด๋ค์ด๊ธฐ ๋๋ฌธ์ด์ฃ .
- ๋, 'year'์ ์ฐ๊ด์ด๋ก๋ 'month'์ 'quarter'๊ฐ, 'car'์ ์ฐ๊ด์ด๋ก๋ 'auto'์ 'vehicle' ๋ฑ์ด ๋ฝํ์ต๋๋ค.
- ๊ทธ๋ฆฌ๊ณ 'toyota'์ ๊ด๋ จ๋ ๋จ์ด๋ก๋ 'nissan', 'honda', 'lexus' ๋ฑ ์๋์ฐจ ์ ์กฐ์ ์ฒด๋ ๋ธ๋๋๊ฐ ๋ฝํ ๊ฒ๋ ํ์ธํ ์ ์์ต๋๋ค.
- ์ด์ฒ๋ผ ๋จ์ด์ ์๋ฏธ ํน์ ๋ฌธ๋ฒ์ ์ธ ๊ด์ ์์ ๋น์ทํ ๋จ์ด๋ค์ด ๊ฐ๊น์ด ๋ฒกํฐ๋ก ๋ํ๋ฌ์ต๋๋ค.
- ์๊ฐํ๊ฑฐ๋ ๋น์ทํ ๊ฒฐ๊ณผ๋ผ๊ณ ํ ์ ์๊ฒ ๋ค์.
Summary
Co-Occurance Matrix (๋์๋ฐ์ ํ๋ ฌ)์ PPMI ํ๋ ฌ๋ก ๋ณํํ๊ณ ๋ค์ Dimension(์ฐจ์)์ Reduction(๊ฐ์)์ํด์ผ๋ก์จ, ๊ฑฐ๋ํ 'ํฌ์๋ฒกํฐ'๋ฅผ ์์ '๋ฐ์ง๋ฒกํฐ'๋ก ๋ณํํ ์ ์๋ค.
๋จ์ด์ Vector ๊ณต๊ฐ์์๋ ์๋ฏธ๊ฐ ๊ฐ๊น์ด ๋จ์ด๋ ๊ทธ ๊ฑฐ๋ฆฌ๋ ๊ฐ๊น์ธ ๊ฒ์ผ๋ก ๊ธฐ๋๋๋ค.
'๐ NLP (์์ฐ์ด์ฒ๋ฆฌ) > ๐ Natural Language Processing' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[NLP] BPTT (Backpropagation Through Time) (0) | 2024.05.23 |
---|---|
[NLP] ์ถ๋ก ๊ธฐ๋ฐ ๊ธฐ๋ฒ & Neural Network (์ ๊ฒฝ๋ง) (0) | 2024.05.22 |
[NLP] Thesaurus(์์๋ฌ์ค), Co-occurence Matrix(๋์๋ฐ์ ํ๋ ฌ) (0) | 2024.05.18 |
[NLP] Transformer Model - ํธ๋์คํฌ๋จธ ๋ชจ๋ธ ์์๋ณด๊ธฐ (0) | 2024.03.07 |
[NLP] ํฉ์ฑ๊ณฑ, ์ํ์ ๊ฒฝ๋ง, Encoder, Decoder์์ ์ํํ๋ Self-Attention (0) | 2024.03.01 |