-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
60 lines (45 loc) · 1.38 KB
/
utils.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# author: WenYi
# email: 1244058349@qq.com
import numpy as np
import pandas as pd
import torch
from concurrent.futures import ProcessPoolExecutor
from torch.utils.data import Dataset
def negativeTable(table_size, word_frequency):
vocab_size = len(word_frequency)
word_frequency_count = list(word_frequency.values())
word_frequency_count_sum = sum([count**0.75 for count in word_frequency_count])
sample_ratio = [count**0.75/word_frequency_count_sum for count in word_frequency_count]
idx = 0
ratio = sample_ratio[idx]
table = [0]*int(table_size)
for i in range(int(table_size)):
table[i] = idx
if i / table_size > ratio:
idx += 1
ratio += sample_ratio[idx]
if idx > vocab_size:
idx = vocab_size - 1
return table
def getNeagtives(word, table, neg_number=5):
table_size = len(table)
neg_result = list()
while neg_number > 0:
idx = np.random.randint(0, table_size)
if table[idx] != word:
neg_result.append(table[idx])
neg_number -= 1
return neg_result
class Word2VecDataSet(Dataset):
def __init__(self, data):
self.center_word = data[0]
self.neighbor_word = data[1]
self.neg_word = data[2]
def __getitem__(self, index):
center_word = self.center_word[index]
neighbor_word = self.neighbor_word[index]
neg_word = self.neg_word[index]
data = (center_word, neighbor_word, neg_word)
return data
def __len__(self):
return len(self.center_word)