-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlabel_corr.py
127 lines (109 loc) · 4.49 KB
/
label_corr.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import numpy as np
import itertools
import json
import sys
from collections import defaultdict
from tqdm import tqdm
import gluonnlp
from sklearn.metrics.pairwise import cosine_similarity
from collections import defaultdict
sys.path.insert(0, './resources')
import constant
def build_concurr_matrix(emb_name='fasttext', emb_source='wiki-news-300d-1M-subword', goal='open'):
# def build_concurr_matrix(emb_name='glove', emb_source='glove.840B.300d', goal='open'):
data_path = 'data/release/'
# build the yid concurr matrix
if goal == 'onto':
label2id = constant.ANS2ID_DICT["onto"]
id2label = constant.g_id2ans
else:
label2id = constant.ANS2ID_DICT["open"]
id2label = constant.open_id2ans
if goal != 'onto':
print('Building label word embedding for open')
words = []
for label in label2id.keys():
words += label.split('_')
word_counter = gluonnlp.data.count_tokens(words)
word_vocab = gluonnlp.Vocab(word_counter)
embed = gluonnlp.embedding.create(emb_name, source=emb_source)
word_vocab.set_embedding(embed)
label_vectors = []
for id_ in range(len(label2id.keys())):
label = id2label[id_]
label_words = label.split('_')
label_vectors.append(word_vocab.embedding[label_words].asnumpy().sum(0))
affinity = cosine_similarity(label_vectors)
else:
print("BOW features for ontonotes")
words = []
for label in label2id.keys():
label = label.replace('/', ' ')
labels = label.strip().split()
words += labels
word_counter = gluonnlp.data.count_tokens(words)
word_vocab = gluonnlp.Vocab(word_counter)
embed = gluonnlp.embedding.create(emb_name, source=emb_source)
word_vocab.set_embedding(embed)
label_list = []
label_vectors = []
for id_ in range(len(label2id.keys())):
label = id2label[id_]
label = label.replace('/', ' ')
labels = label.strip().split()
label_list.append(labels)
label_vectors.append(word_vocab.embedding[labels].asnumpy().sum(0))
label_vectors = np.array(label_vectors)
affinity = cosine_similarity(label_vectors)
matrix = np.zeros((len(label2id.keys()), len(label2id.keys())))
if goal == 'onto':
train_file_list = ['ontonotes/augmented_train.json']
else:
train_file_list = ['distant_supervision/headword_train.json', 'distant_supervision/el_train.json', 'crowd/train_m.json']
type_count = defaultdict(int)
for f_id, file in enumerate(train_file_list):
file = data_path + file
with open(file) as f:
for sent in tqdm(f.readlines()):
line_elem = json.loads(sent.strip())
y_strs = line_elem['y_str']
# y_ids = list(set([label2id[x] for x in y_strs if x in label2id]))
for x in y_strs:
type_count[x] += 1
y_ids = [label2id[x] for x in y_strs if x in label2id]
if len(y_ids) > 1:
for (x, y) in itertools.combinations(y_ids, 2):
# if x == y:
# print(y_strs)
# assert False
matrix[x,y] = matrix[x,y] + 1
# print(type_count['child'])
# print(type_count['daughter'])
# print(np.mean(list(type_count.values())))
# add self-connection
matrix += np.identity(matrix.shape[0])
# print(len(concurr_labels))
# print(np.count_nonzero(matrix)/np.prod(matrix.shape))
target = np.tanh(np.log(matrix + 1e-8))
mask = (matrix == 0).astype(float)
mask_inverse = (matrix > 0).astype(float)
return matrix, affinity, target, mask, mask_inverse
if __name__ == '__main__':
co_occurence, _, _, _, _ = build_concurr_matrix()
co_occurence = co_occurence - np.identity(co_occurence.shape[0])
print(np.max(co_occurence))
# id2label = constant.open_id2ans
# label2id = constant.ANS2ID_DICT["open"]
# person_id = label2id['person']
# # print(co_occurence[:10,:10])
# person_id_row = co_occurence[person_id, :]
# label_freq = {}
# inconsistent_pairs = []
# for index, value in enumerate(person_id_row):
# if value != 0:
# label_freq[id2label[index]] = value
# else:
# inconsistent_pairs.append(['person', id2label[index]])
# # print(label_freq)
# print(inconsistent_pairs)
# with open(z)