Skip to content

Commit acdc112

Browse files
committed
Word2Vec 오류 수정
1 parent 9e00a8c commit acdc112

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

04 - Neural Network Basic/03 - Word2Vec.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,25 @@
2727
"강아지 고양이 좋다"]
2828

2929
# 문장을 전부 합친 후 공백으로 단어들을 나누고 고유한 단어들로 리스트를 만듭니다.
30+
word_sequence = " ".join(sentences).split()
3031
word_list = " ".join(sentences).split()
3132
word_list = list(set(word_list))
3233
# 문자열로 분석하는 것 보다, 숫자로 분석하는 것이 훨씬 용이하므로
3334
# 리스트에서 문자들의 인덱스를 뽑아서 사용하기 위해,
3435
# 이를 표현하기 위한 연관 배열과, 단어 리스트에서 단어를 참조 할 수 있는 인덱스 배열을 만듭합니다.
3536
word_dict = {w: i for i, w in enumerate(word_list)}
36-
word_index = [word_dict[word] for word in word_list]
3737

3838
# 윈도우 사이즈를 1 로 하는 skip-gram 모델을 만듭니다.
3939
# 예) 나 게임 만화 애니 좋다
4040
# -> ([나, 만화], 게임), ([게임, 애니], 만화), ([만화, 좋다], 애니)
4141
# -> (게임, 나), (게임, 만화), (만화, 게임), (만화, 애니), (애니, 만화), (애니, 좋다)
4242
skip_grams = []
4343

44-
for i in range(1, len(word_index) - 1):
44+
for i in range(1, len(word_sequence) - 1):
4545
# (context, target) : ([target index - 1, target index + 1], target)
46-
target = word_index[i]
47-
context = [word_index[i - 1], word_index[i + 1]]
46+
# 스킵그램을 만든 후, 저장은 단어의 고유 번호(index)로 저장합니다
47+
target = word_dict[word_sequence[i]]
48+
context = [word_dict[word_sequence[i - 1]], word_dict[word_sequence[i + 1]]]
4849

4950
# (target, context[0]), (target, context[1])..
5051
for w in context:

0 commit comments

Comments
 (0)