-
Notifications
You must be signed in to change notification settings - Fork 985
/
Test.h
363 lines (318 loc) · 13.9 KB
/
Test.h
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#ifndef TEST_H
#define TEST_H
#include "Setting.h"
#include "Reader.h"
#include "Corrupt.h"
/*=====================================================================================
link prediction
======================================================================================*/
INT lastHead = 0;
INT lastTail = 0;
REAL l1_filter_tot = 0, l1_tot = 0, r1_tot = 0, r1_filter_tot = 0, l_tot = 0, r_tot = 0, l_filter_rank = 0, l_rank = 0, l_filter_reci_rank = 0, l_reci_rank = 0;
REAL l3_filter_tot = 0, l3_tot = 0, r3_tot = 0, r3_filter_tot = 0, l_filter_tot = 0, r_filter_tot = 0, r_filter_rank = 0, r_rank = 0, r_filter_reci_rank = 0, r_reci_rank = 0;
REAL l1_filter_tot_constrain = 0, l1_tot_constrain = 0, r1_tot_constrain = 0, r1_filter_tot_constrain = 0, l_tot_constrain = 0, r_tot_constrain = 0, l_filter_rank_constrain = 0, l_rank_constrain = 0, l_filter_reci_rank_constrain = 0, l_reci_rank_constrain = 0;
REAL l3_filter_tot_constrain = 0, l3_tot_constrain = 0, r3_tot_constrain = 0, r3_filter_tot_constrain = 0, l_filter_tot_constrain = 0, r_filter_tot_constrain = 0, r_filter_rank_constrain = 0, r_rank_constrain = 0, r_filter_reci_rank_constrain = 0, r_reci_rank_constrain = 0;
extern "C"
void initTest() {
lastHead = 0;
lastTail = 0;
l1_filter_tot = 0, l1_tot = 0, r1_tot = 0, r1_filter_tot = 0, l_tot = 0, r_tot = 0, l_filter_rank = 0, l_rank = 0, l_filter_reci_rank = 0, l_reci_rank = 0;
l3_filter_tot = 0, l3_tot = 0, r3_tot = 0, r3_filter_tot = 0, l_filter_tot = 0, r_filter_tot = 0, r_filter_rank = 0, r_rank = 0, r_filter_reci_rank = 0, r_reci_rank = 0;
l1_filter_tot_constrain = 0, l1_tot_constrain = 0, r1_tot_constrain = 0, r1_filter_tot_constrain = 0, l_tot_constrain = 0, r_tot_constrain = 0, l_filter_rank_constrain = 0, l_rank_constrain = 0, l_filter_reci_rank_constrain = 0, l_reci_rank_constrain = 0;
l3_filter_tot_constrain = 0, l3_tot_constrain = 0, r3_tot_constrain = 0, r3_filter_tot_constrain = 0, l_filter_tot_constrain = 0, r_filter_tot_constrain = 0, r_filter_rank_constrain = 0, r_rank_constrain = 0, r_filter_reci_rank_constrain = 0, r_reci_rank_constrain = 0;
}
extern "C"
void getHeadBatch(INT *ph, INT *pt, INT *pr) {
for (INT i = 0; i < entityTotal; i++) {
ph[i] = i;
pt[i] = testList[lastHead].t;
pr[i] = testList[lastHead].r;
}
}
extern "C"
void getTailBatch(INT *ph, INT *pt, INT *pr) {
for (INT i = 0; i < entityTotal; i++) {
ph[i] = testList[lastTail].h;
pt[i] = i;
pr[i] = testList[lastTail].r;
}
}
extern "C"
void testHead(REAL *con) {
INT h = testList[lastHead].h;
INT t = testList[lastHead].t;
INT r = testList[lastHead].r;
INT lef = head_lef[r], rig = head_rig[r];
REAL minimal = con[h];
INT l_s = 0;
INT l_filter_s = 0;
INT l_s_constrain = 0;
INT l_filter_s_constrain = 0;
for (INT j = 0; j < entityTotal; j++) {
if (j != h) {
REAL value = con[j];
if (value < minimal) {
l_s += 1;
if (not _find(j, t, r))
l_filter_s += 1;
}
while (lef < rig && head_type[lef] < j) lef ++;
if (lef < rig && j == head_type[lef]) {
if (value < minimal) {
l_s_constrain += 1;
if (not _find(j, t, r)) {
l_filter_s_constrain += 1;
}
}
}
}
}
if (l_filter_s < 10) l_filter_tot += 1;
if (l_s < 10) l_tot += 1;
if (l_filter_s < 3) l3_filter_tot += 1;
if (l_s < 3) l3_tot += 1;
if (l_filter_s < 1) l1_filter_tot += 1;
if (l_s < 1) l1_tot += 1;
if (l_filter_s_constrain < 10) l_filter_tot_constrain += 1;
if (l_s_constrain < 10) l_tot_constrain += 1;
if (l_filter_s_constrain < 3) l3_filter_tot_constrain += 1;
if (l_s_constrain < 3) l3_tot_constrain += 1;
if (l_filter_s_constrain < 1) l1_filter_tot_constrain += 1;
if (l_s_constrain < 1) l1_tot_constrain += 1;
l_filter_rank += (l_filter_s+1);
l_rank += (1+l_s);
l_filter_reci_rank += 1.0/(l_filter_s+1);
l_reci_rank += 1.0/(l_s+1);
l_filter_rank_constrain += (l_filter_s_constrain+1);
l_rank_constrain += (1+l_s_constrain);
l_filter_reci_rank_constrain += 1.0/(l_filter_s_constrain+1);
l_reci_rank_constrain += 1.0/(l_s_constrain+1);
lastHead++;
}
extern "C"
void testTail(REAL *con) {
INT h = testList[lastTail].h;
INT t = testList[lastTail].t;
INT r = testList[lastTail].r;
INT lef = tail_lef[r], rig = tail_rig[r];
REAL minimal = con[t];
INT r_s = 0;
INT r_filter_s = 0;
INT r_s_constrain = 0;
INT r_filter_s_constrain = 0;
for (INT j = 0; j < entityTotal; j++) {
if (j != t) {
REAL value = con[j];
if (value < minimal) {
r_s += 1;
if (not _find(h, j, r))
r_filter_s += 1;
}
while (lef < rig && tail_type[lef] < j) lef ++;
if (lef < rig && j == tail_type[lef]) {
if (value < minimal) {
r_s_constrain += 1;
if (not _find(h, j ,r)) {
r_filter_s_constrain += 1;
}
}
}
}
}
if (r_filter_s < 10) r_filter_tot += 1;
if (r_s < 10) r_tot += 1;
if (r_filter_s < 3) r3_filter_tot += 1;
if (r_s < 3) r3_tot += 1;
if (r_filter_s < 1) r1_filter_tot += 1;
if (r_s < 1) r1_tot += 1;
if (r_filter_s_constrain < 10) r_filter_tot_constrain += 1;
if (r_s_constrain < 10) r_tot_constrain += 1;
if (r_filter_s_constrain < 3) r3_filter_tot_constrain += 1;
if (r_s_constrain < 3) r3_tot_constrain += 1;
if (r_filter_s_constrain < 1) r1_filter_tot_constrain += 1;
if (r_s_constrain < 1) r1_tot_constrain += 1;
r_filter_rank += (1+r_filter_s);
r_rank += (1+r_s);
r_filter_reci_rank += 1.0/(1+r_filter_s);
r_reci_rank += 1.0/(1+r_s);
r_filter_rank_constrain += (1+r_filter_s_constrain);
r_rank_constrain += (1+r_s_constrain);
r_filter_reci_rank_constrain += 1.0/(1+r_filter_s_constrain);
r_reci_rank_constrain += 1.0/(1+r_s_constrain);
lastTail++;
}
extern "C"
void test_link_prediction() {
l_rank /= testTotal;
r_rank /= testTotal;
l_reci_rank /= testTotal;
r_reci_rank /= testTotal;
l_tot /= testTotal;
l3_tot /= testTotal;
l1_tot /= testTotal;
r_tot /= testTotal;
r3_tot /= testTotal;
r1_tot /= testTotal;
// with filter
l_filter_rank /= testTotal;
r_filter_rank /= testTotal;
l_filter_reci_rank /= testTotal;
r_filter_reci_rank /= testTotal;
l_filter_tot /= testTotal;
l3_filter_tot /= testTotal;
l1_filter_tot /= testTotal;
r_filter_tot /= testTotal;
r3_filter_tot /= testTotal;
r1_filter_tot /= testTotal;
printf("no type constraint results:\n");
printf("metric:\t\t\t MRR \t\t MR \t\t hit@10 \t hit@3 \t hit@1 \n");
printf("l(raw):\t\t\t %f \t %f \t %f \t %f \t %f \n", l_reci_rank, l_rank, l_tot, l3_tot, l1_tot);
printf("r(raw):\t\t\t %f \t %f \t %f \t %f \t %f \n", r_reci_rank, r_rank, r_tot, r3_tot, r1_tot);
printf("averaged(raw):\t\t %f \t %f \t %f \t %f \t %f \n",
(l_reci_rank+r_reci_rank)/2, (l_rank+r_rank)/2, (l_tot+r_tot)/2, (l3_tot+r3_tot)/2, (l1_tot+r1_tot)/2);
printf("\n");
printf("l(filter):\t\t %f \t %f \t %f \t %f \t %f \n", l_filter_reci_rank, l_filter_rank, l_filter_tot, l3_filter_tot, l1_filter_tot);
printf("r(filter):\t\t %f \t %f \t %f \t %f \t %f \n", r_filter_reci_rank, r_filter_rank, r_filter_tot, r3_filter_tot, r1_filter_tot);
printf("averaged(filter):\t %f \t %f \t %f \t %f \t %f \n",
(l_filter_reci_rank+r_filter_reci_rank)/2, (l_filter_rank+r_filter_rank)/2, (l_filter_tot+r_filter_tot)/2, (l3_filter_tot+r3_filter_tot)/2, (l1_filter_tot+r1_filter_tot)/2);
//type constrain
l_rank_constrain /= testTotal;
r_rank_constrain /= testTotal;
l_reci_rank_constrain /= testTotal;
r_reci_rank_constrain /= testTotal;
l_tot_constrain /= testTotal;
l3_tot_constrain /= testTotal;
l1_tot_constrain /= testTotal;
r_tot_constrain /= testTotal;
r3_tot_constrain /= testTotal;
r1_tot_constrain /= testTotal;
// with filter
l_filter_rank_constrain /= testTotal;
r_filter_rank_constrain /= testTotal;
l_filter_reci_rank_constrain /= testTotal;
r_filter_reci_rank_constrain /= testTotal;
l_filter_tot_constrain /= testTotal;
l3_filter_tot_constrain /= testTotal;
l1_filter_tot_constrain /= testTotal;
r_filter_tot_constrain /= testTotal;
r3_filter_tot_constrain /= testTotal;
r1_filter_tot_constrain /= testTotal;
printf("type constraint results:\n");
printf("metric:\t\t\t MRR \t\t MR \t\t hit@10 \t hit@3 \t hit@1 \n");
printf("l(raw):\t\t\t %f \t %f \t %f \t %f \t %f \n", l_reci_rank_constrain, l_rank_constrain, l_tot_constrain, l3_tot_constrain, l1_tot_constrain);
printf("r(raw):\t\t\t %f \t %f \t %f \t %f \t %f \n", r_reci_rank_constrain, r_rank_constrain, r_tot_constrain, r3_tot_constrain, r1_tot_constrain);
printf("averaged(raw):\t\t %f \t %f \t %f \t %f \t %f \n",
(l_reci_rank_constrain+r_reci_rank_constrain)/2, (l_rank_constrain+r_rank_constrain)/2, (l_tot_constrain+r_tot_constrain)/2, (l3_tot_constrain+r3_tot_constrain)/2, (l1_tot_constrain+r1_tot_constrain)/2);
printf("\n");
printf("l(filter):\t\t %f \t %f \t %f \t %f \t %f \n", l_filter_reci_rank_constrain, l_filter_rank_constrain, l_filter_tot_constrain, l3_filter_tot_constrain, l1_filter_tot_constrain);
printf("r(filter):\t\t %f \t %f \t %f \t %f \t %f \n", r_filter_reci_rank_constrain, r_filter_rank_constrain, r_filter_tot_constrain, r3_filter_tot_constrain, r1_filter_tot_constrain);
printf("averaged(filter):\t %f \t %f \t %f \t %f \t %f \n",
(l_filter_reci_rank_constrain+r_filter_reci_rank_constrain)/2, (l_filter_rank_constrain+r_filter_rank_constrain)/2, (l_filter_tot_constrain+r_filter_tot_constrain)/2, (l3_filter_tot_constrain+r3_filter_tot_constrain)/2, (l1_filter_tot_constrain+r1_filter_tot_constrain)/2);
}
/*=====================================================================================
triple classification
======================================================================================*/
Triple *negTestList;
extern "C"
void getNegTest() {
negTestList = (Triple *)calloc(testTotal, sizeof(Triple));
for (INT i = 0; i < testTotal; i++) {
negTestList[i] = testList[i];
negTestList[i].t = corrupt(testList[i].h, testList[i].r);
}
}
Triple *negValidList;
extern "C"
void getNegValid() {
negValidList = (Triple *)calloc(validTotal, sizeof(Triple));
for (INT i = 0; i < validTotal; i++) {
negValidList[i] = validList[i];
negValidList[i].t = corrupt(validList[i].h, validList[i].r);
}
}
extern "C"
void getTestBatch(INT *ph, INT *pt, INT *pr, INT *nh, INT *nt, INT *nr) {
getNegTest();
for (INT i = 0; i < testTotal; i++) {
ph[i] = testList[i].h;
pt[i] = testList[i].t;
pr[i] = testList[i].r;
nh[i] = negTestList[i].h;
nt[i] = negTestList[i].t;
nr[i] = negTestList[i].r;
}
}
extern "C"
void getValidBatch(INT *ph, INT *pt, INT *pr, INT *nh, INT *nt, INT *nr) {
getNegValid();
for (INT i = 0; i < validTotal; i++) {
ph[i] = validList[i].h;
pt[i] = validList[i].t;
pr[i] = validList[i].r;
nh[i] = negValidList[i].h;
nt[i] = negValidList[i].t;
nr[i] = negValidList[i].r;
}
}
REAL threshEntire;
extern "C"
void getBestThreshold(REAL *relThresh, REAL *score_pos, REAL *score_neg) {
REAL interval = 0.01;
REAL min_score, max_score, bestThresh, tmpThresh, bestAcc, tmpAcc;
INT n_interval, correct, total;
for (INT r = 0; r < relationTotal; r++) {
if (validLef[r] == -1) continue;
total = (validRig[r] - validLef[r] + 1) * 2;
min_score = score_pos[validLef[r]];
if (score_neg[validLef[r]] < min_score) min_score = score_neg[validLef[r]];
max_score = score_pos[validLef[r]];
if (score_neg[validLef[r]] > max_score) max_score = score_neg[validLef[r]];
for (INT i = validLef[r]+1; i <= validRig[r]; i++) {
if(score_pos[i] < min_score) min_score = score_pos[i];
if(score_pos[i] > max_score) max_score = score_pos[i];
if(score_neg[i] < min_score) min_score = score_neg[i];
if(score_neg[i] > max_score) max_score = score_neg[i];
}
n_interval = INT((max_score - min_score)/interval);
for (INT i = 0; i <= n_interval; i++) {
tmpThresh = min_score + i * interval;
correct = 0;
for (INT j = validLef[r]; j <= validRig[r]; j++) {
if (score_pos[j] <= tmpThresh) correct ++;
if (score_neg[j] > tmpThresh) correct ++;
}
tmpAcc = 1.0 * correct / total;
if (i == 0) {
bestThresh = tmpThresh;
bestAcc = tmpAcc;
} else if (tmpAcc > bestAcc) {
bestAcc = tmpAcc;
bestThresh = tmpThresh;
}
}
relThresh[r] = bestThresh;
}
}
REAL *testAcc;
REAL aveAcc;
extern "C"
void test_triple_classification(REAL *relThresh, REAL *score_pos, REAL *score_neg) {
testAcc = (REAL *)calloc(relationTotal, sizeof(REAL));
INT aveCorrect = 0, aveTotal = 0;
REAL aveAcc;
for (INT r = 0; r < relationTotal; r++) {
if (validLef[r] == -1 || testLef[r] ==-1) continue;
INT correct = 0, total = 0;
for (INT i = testLef[r]; i <= testRig[r]; i++) {
if (score_pos[i] <= relThresh[r]) correct++;
if (score_neg[i] > relThresh[r]) correct++;
total += 2;
}
testAcc[r] = 1.0 * correct / total;
aveCorrect += correct;
aveTotal += total;
}
aveAcc = 1.0 * aveCorrect / aveTotal;
printf("triple classification accuracy is %lf\n", aveAcc);
}
#endif