-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathinput_data.py
executable file
·215 lines (206 loc) · 7.64 KB
/
input_data.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import os
import random
import numpy as np
class DataSet(object):
def __init__(self, head_list, tail_list, relation_list,
head_set, tail_set, entity_total, tag_total):
self._num_examples = len(head_list)
self._head_list = np.array(head_list)
self._tail_list = np.array(tail_list)
self._relation_list = np.array(relation_list)
self._head_set = np.array(head_set)
self._tail_set = np.array(tail_set)
self._entity_total = entity_total
self._tag_total = tag_total
self._epochs_completed = 0
self._index_in_epoch = 0
@property
def entity_total(self):
return self._entity_total
@property
def tag_total(self):
return self._tag_total
@property
def num_examples(self):
return self._num_examples
@property
def epochs_completed(self):
return self._epochs_completed
def next_batch(self, batch_size, aeBeta):
"""Return the next `batch_size` examples from this data set."""
start = self._index_in_epoch
self._index_in_epoch += batch_size
if self._index_in_epoch > self._num_examples:
# Finished epoch
self._epochs_completed += 1
# Shuffle the data
perm = np.random.permutation(np.arange(self._num_examples))
perm = perm.flatten().tolist()
self._head_list = self._head_list[perm]
self._tail_list = self._tail_list[perm]
self._relation_list = self._relation_list[perm]
# Start next epoch
start = 0
self._index_in_epoch = batch_size
assert batch_size <= self._num_examples
end = self._index_in_epoch
tag_size = self._tag_total
entity_size = self._entity_total
pos_h = []
pos_t = []
pos_r = []
pos_b = []
neg_h = []
neg_t = []
neg_r = []
neg_b = []
for i in range(start, end):
cur_h = self._head_list[i]
cur_t = self._tail_list[i]
cur_r = self._relation_list[i]
set_r = set(cur_r)
r_one_hot = np.zeros(tag_size, dtype=float)
b = np.ones(tag_size, dtype=float)
r_one_hot[cur_r] = 1.0
b[cur_r] = aeBeta
#replace head
pos_h.append(cur_h)
pos_t.append(cur_t)
pos_r.append(r_one_hot)
pos_b.append(b)
rand_h = random.randint(0, entity_size-1)
while(rand_h in self._tail_set[cur_t]):
rand_h = random.randint(0, entity_size-1)
neg_h.append(rand_h)
neg_t.append(cur_t)
neg_r.append(r_one_hot)
neg_b.append(b)
#repalce tail
pos_h.append(cur_h)
pos_t.append(cur_t)
pos_r.append(r_one_hot)
pos_b.append(b)
rand_t = random.randint(0, entity_size-1)
while(rand_t in self._head_set[cur_h]):
rand_t = random.randint(0, entity_size-1)
neg_h.append(cur_h)
neg_t.append(rand_t)
neg_r.append(r_one_hot)
neg_b.append(b)
#replace relation
pos_h.append(cur_h)
pos_t.append(cur_t)
pos_r.append(r_one_hot)
pos_b.append(b)
rand_set_r = set([])
rand_r = random.randint(0, tag_size-1)
len_r = len(cur_r)
while(len(rand_set_r) < len_r and len(rand_set_r) + len_r < tag_size):
if (rand_r not in set_r) and (rand_r not in rand_set_r):
rand_set_r.add(rand_r)
rand_r = random.randint(0, tag_size-1)
rand_cur_r = [r for r in rand_set_r]
rand_r_one_hot = np.zeros(tag_size, dtype=float)
rand_r_one_hot[rand_cur_r] = 1.0
rand_b = np.ones(tag_size, dtype=float)
rand_b[rand_cur_r] = aeBeta
neg_h.append(cur_h)
neg_t.append(cur_t)
neg_r.append(rand_r_one_hot)
neg_b.append(rand_b)
return np.array(pos_h), np.array(pos_t), np.array(pos_r), np.array(pos_b),\
np.array(neg_h), np.array(neg_t), np.array(neg_r), np.array(neg_b)
def next_test_batch(self, batch_size):
start = self._index_in_epoch
self._index_in_epoch += batch_size
if self._index_in_epoch > self._num_examples:
# Finished epoch
self._epochs_completed += 1
# Start next epoch
start = 0
self._index_in_epoch = batch_size
assert batch_size <= self._num_examples
end = self._index_in_epoch
tag_size = self._tag_total
entity_size = self._entity_total
pos_h = []
pos_t = []
pos_r = []
for i in range(start, end):
cur_h = self._head_list[i]
cur_t = self._tail_list[i]
cur_r = self._relation_list[i]
r_one_hot = np.zeros(tag_size, dtype=float)
b = np.ones(tag_size, dtype=float)
r_one_hot[cur_r] = 1.0
pos_h.append(cur_h)
pos_t.append(cur_t)
pos_r.append(r_one_hot)
return np.array(pos_h), np.array(pos_t), np.array(pos_r)
def next_autoencoder_batch(self, batch_size, aeBeta):
start = self._index_in_epoch
self._index_in_epoch += batch_size
if self._index_in_epoch > self._num_examples:
# Finished epoch
self._epochs_completed += 1
# Shuffle the data
perm = np.random.permutation(np.arange(self._num_examples))
perm = perm.flatten().tolist()
self._head_list = self._head_list[perm]
self._tail_list = self._tail_list[perm]
self._relation_list = self._relation_list[perm]
# Start next epoch
start = 0
self._index_in_epoch = batch_size
assert batch_size <= self._num_examples
end = self._index_in_epoch
vec_size = self._tag_total
vecs = []
bs = []
for i in range(start, end):
vec_index = self._relation_list[i]
vec = np.zeros(vec_size, dtype=float)
b = np.ones(vec_size, dtype=float)
vec[vec_index] = 1.0
b[vec_index] = aeBeta
vecs.append(vec)
bs.append(b)
return np.array(vecs), np.array(bs)
def read_triples(filename):
head_list = []
tail_list = []
relation_list = []
head_set = []
tail_set = []
fin = open(filename, 'r')
content = fin.readline()
_, entity_total, tag_total = [int(i) for i in content.split()]
for i in xrange(entity_total):
head_set.append(set())
tail_set.append(set())
while 1:
content = fin.readline()
if content == '':
break
values = [int(i) for i in content.split()]
head_list.append(values[0])
tail_list.append(values[1])
head_set[values[0]].add(values[1])
tail_set[values[1]].add(values[0])
relation_list.append(values[2:])
fin.close()
return DataSet(head_list, tail_list, relation_list,
head_set, tail_set, entity_total, tag_total)
def read_data_sets(train_dir='aminer_small'):
class DataSets(object):
pass
data_sets = DataSets()
TRAIN = 'train.txt'
VALID = 'valid.txt'
TEST = 'test.txt'
data_sets.train = read_triples(os.path.join(train_dir, TRAIN))
data_sets.valid = read_triples(os.path.join(train_dir, VALID))
data_sets.test = read_triples(os.path.join(train_dir, TEST))
data_sets.entity_total = data_sets.train.entity_total
data_sets.tag_total = data_sets.train.tag_total
return data_sets