-
Notifications
You must be signed in to change notification settings - Fork 2
/
CRPP_AUC.py
488 lines (364 loc) · 15.4 KB
/
CRPP_AUC.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import sys
import tensorflow as tf
import numpy as np
import math
from tensorflow.python import debug as tf_debug
processed = sys.argv[1]
rank=sys.argv[2]
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in xrange(0, len(l), n):
yield l[i:i + n]
with open(processed, "r") as f:
content = f.readlines()
f.close()
with open(rank, "r") as f2:
content2 = f2.readlines()
f2.close()
intervals=[]
givenrankintervals = []
no_intervals = 100
for line in content2:
tokens=line.split("\t")
t1=float(tokens[0])/10000
t2=float(tokens[1])/10000
givenrankintervals.append((t1,t2))
ranks = [int(x) for x in tokens[2:]]
#print ranks
L=len(givenrankintervals)
p=len(content)/7
tlast = givenrankintervals[L-1][1]
interval_size = tlast/no_intervals
marker = 0
for i in range(no_intervals):
intervals.append((marker, marker+interval_size))
marker = marker + interval_size
intervalcounts=[[0 for x in range(no_intervals)] for x in range(p)]
pred_intervalcounts=[[0 for x in range(no_intervals)] for x in range(p)]
test_t=[]
h_series = [[] for x in range(p)]
integral = [[] for x in range(p)]
num_epochs = 100
truncated_backprop_length = 15
echo_step = 3
batch_size = 5
batchk_placeholder = tf.placeholder(tf.float32, [batch_size, truncated_backprop_length])
batcht_placeholder = tf.placeholder(tf.float32, [batch_size, truncated_backprop_length])
init_h=tf.placeholder(tf.float32, (batch_size))
omega = tf.constant([0.01], shape=(batch_size,))
a_list = []
b_list = []
c_list = []
alpha_list = []
beta_list = []
gamma_list = []
t_series = [[] for x in range(p)]
k_series = [[] for x in range(p)]
G_loss = [[] for x in range(p)]
a = [[] for x in range(p)]
b = [[] for x in range(p)]
c = [[] for x in range(p)]
a1 = []
b1 = []
c1 = []
d1 = []
def greater(x, c):
return tf.nn.relu(tf.sign(x-c))
def lesser(x, c):
return tf.nn.relu(tf.sign(c-x))
l=0
for n in range(p):
alpha=tf.Variable([0.001 for x in range(batch_size)])
beta=tf.Variable([0.001 for x in range(batch_size)])
gamma=tf.Variable([0.001 for x in range(batch_size)])
a[n]=tf.Variable([0.001 for x in range(batch_size)])
b[n]=tf.Variable([0.001 for x in range(batch_size)])
c[n]=tf.Variable([0.001 for x in range(batch_size)])
str_k=content[l]
k=[float(ki) for ki in str_k.split(",")]
str_train=content[l+1].rstrip()
train_t=[float(ti)/10000 for ti in str_train.split(",")]
str_test=content[l+6].rstrip()
test_t=[float(ti)/10000 for ti in str_test.split(",")]
l=l+7
first_training_instance = train_t[0]
last_training_instance = train_t[-1]
first_training_interval = 0
last_training_interval = no_intervals - 1
while train_t[0]>intervals[first_training_interval][1]:
first_training_interval = first_training_interval+1
while train_t[-1]<intervals[last_training_interval][0]:
last_training_interval = last_training_interval-1
trainingintervals = intervals[:last_training_interval]
counter = 0
for i in range(len(train_t)):
while train_t[i]>intervals[counter][1]:
counter = counter+1
intervalcounts[n][counter] = intervalcounts[n][counter] + 1
for i in range(len(test_t)):
while counter<no_intervals and test_t[i]>intervals[counter][1]:
counter = counter+1
if counter==no_intervals:
break
intervalcounts[n][counter] = intervalcounts[n][counter] + 1
last_test_interval = no_intervals - 1
while intervalcounts[n][last_test_interval]==0:
last_test_interval = last_test_interval-1
#print intervalcounts[n][last_test_interval], intervals[last_test_interval]
no_training_intervals = last_training_interval-first_training_interval
no_test_intervals = no_intervals - no_training_intervals
total_series_length = len(k)
num_batches = total_series_length//batch_size//truncated_backprop_length
k_series[n] = tf.unstack(batchk_placeholder, axis=1)
t_series[n] = tf.unstack(batcht_placeholder, axis=1)
# Forward pass
current_h = init_h
for i in range(len(k_series)):
current_k = k_series[n][i]
current_t = t_series[n][i]
numerator = -omega*current_t
denominator = current_k
frac = tf.truediv(numerator,denominator)
kernel = tf.exp(frac)
next_h = tf.exp((alpha*current_h + beta*kernel + gamma))
h_series[n].append(next_h)
current_h = next_h
G_loss[n] = tf.reduce_sum(tf.exp(a[n]*h_series[n][-1]+b[n]*t_series[n][-1]+c[n])/b[n] - tf.exp(a[n]*h_series[n][-1]+b[n]*t_series[n][0]+c[n])/b[n]) - sum([tf.reduce_sum(a[n]*h_series[n][-1]+b[n]*(t_series[n][k1+1]-t_series[n][k1])+c[n]) for k1 in range(len(t_series)-1)])
regularizer = tf.add_n([tf.nn.l2_loss(a[n]),tf.nn.l2_loss(b[n]),tf.nn.l2_loss(c[n]),tf.nn.l2_loss(alpha),tf.nn.l2_loss(beta),tf.nn.l2_loss(gamma)])
total_loss = G_loss[n]+regularizer
train_step = tf.train.AdagradOptimizer(0.3).minimize(total_loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
loss_list = []
h_list = []
last_h = np.ones((batch_size))
last_batchk = np.ones((batch_size, truncated_backprop_length))
last_batcht = np.ones((batch_size, truncated_backprop_length))
#sess = tf_debug.LocalCLIDebugWrapperSession(sess)
#sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan)
#writer = tf.summary.FileWriter("/home/supritam/Documents/aaai/code", sess.graph)
for epoch_idx in range(num_epochs):
_current_h = np.ones((batch_size))
#print("New data, epoch", epoch_idx)
for batch_idx in range(num_batches):
start_idx = batch_idx * batch_size * truncated_backprop_length
end_idx = start_idx + batch_size * truncated_backprop_length
batchk = list(chunks(k[start_idx:end_idx], truncated_backprop_length))
batcht = list(chunks(train_t[start_idx:end_idx], truncated_backprop_length))
#---debugging statements---
#print batchk, _current_h
_a, _b, _c, _total_loss, _train_step, _current_h, = sess.run(
[a[n], b[n], c[n], total_loss, train_step, current_h],
feed_dict={
batchk_placeholder:batchk,
batcht_placeholder:batcht,
init_h:_current_h,
})
last_h = _current_h
last_batchk = batchk
last_batcht = batcht
loss_list.append(_total_loss)
h_list.append(_current_h)
#if batch_idx%100 == 0:
#print("Step", batch_idx, "Loss", _total_loss)
#print("a", _a)
#print("b", _b)
#print("c", _c)
saved_a, saved_b, saved_c, saved_alpha, saved_beta, saved_gamma = sess.run([a[n], b[n], c[n], alpha, beta, gamma], feed_dict={
batchk_placeholder:last_batchk,
batcht_placeholder:last_batcht,
init_h:last_h,
})
a_list.append(saved_a)
b_list.append(saved_b)
c_list.append(saved_c)
alpha_list.append(saved_alpha)
beta_list.append(saved_beta)
gamma_list.append(saved_gamma)
for i in range(first_training_interval, last_training_interval+1):
t1, t2 = intervals[i]
pred_intervalcounts[n][i] = math.exp(sum(saved_a*last_h + saved_b*t2 + saved_c))/sum(saved_b) - math.exp(sum(saved_a*last_h + saved_b*t1 + saved_c))/sum(saved_b)
ADV_EPOCHS = 100
for counter in range(ADV_EPOCHS):
a1=[]
b1=[]
c1=[]
d1=[]
for i in range(p-1):
for j in range(i+1, p):
for n in range(last_training_interval+1):
predi= tf.exp(a[i]*h_series[i][-1]+b[i]*intervals[i][1]+c[i])/b[i] - (
tf.exp(a[i]*h_series[i][-1]+b[i]*intervals[i][0]+c[i])/b[i])
predj= tf.exp(a[j]*h_series[j][-1]+b[j]*intervals[j][1]+c[j])/b[j] - (
tf.exp(a[j]*h_series[j][-1]+b[j]*intervals[j][0]+c[j])/b[j])
diff = predi - predj
if intervalcounts[i][n] > intervalcounts[j][n]:
a1.append(greater(diff, 1))
if intervalcounts[i][n] < intervalcounts[j][n]:
b1.append(greater(diff, 1))
if intervalcounts[i][n] > intervalcounts[j][n]:
c1.append(lesser(diff, -1))
if intervalcounts[i][n] < intervalcounts[j][n]:
d1.append(lesser(diff, -1))
a1 = tf.reduce_sum(a1)
b1 = tf.reduce_sum(b1)
c1 = tf.reduce_sum(c1)
d1 = tf.reduce_sum(d1)
SwappedPairs = b1 + c1
l = 0
for n in range(p):
alpha=tf.Variable([0.001 for x in range(batch_size)])
beta=tf.Variable([0.001 for x in range(batch_size)])
gamma=tf.Variable([0.001 for x in range(batch_size)])
a[n]=tf.Variable([0.001 for x in range(batch_size)])
b[n]=tf.Variable([0.001 for x in range(batch_size)])
c[n]=tf.Variable([0.001 for x in range(batch_size)])
str_k=content[l]
k=[float(ki) for ki in str_k.split(",")]
str_train=content[l+1].rstrip()
train_t=[float(ti)/10000 for ti in str_train.split(",")]
str_test=content[l+6].rstrip()
test_t=[float(ti)/10000 for ti in str_test.split(",")]
l=l+7
first_training_instance = train_t[0]
last_training_instance = train_t[-1]
first_training_interval = 0
last_training_interval = no_intervals - 1
while train_t[0]>intervals[first_training_interval][1]:
first_training_interval = first_training_interval+1
while train_t[-1]<intervals[last_training_interval][0]:
last_training_interval = last_training_interval-1
trainingintervals = intervals[:last_training_interval]
counter = 0
for i in range(len(train_t)):
while train_t[i]>intervals[counter][1]:
counter = counter+1
intervalcounts[n][counter] = intervalcounts[n][counter] + 1
for i in range(len(test_t)):
while counter<no_intervals and test_t[i]>intervals[counter][1]:
counter = counter+1
if counter==no_intervals:
break
intervalcounts[n][counter] = intervalcounts[n][counter] + 1
last_test_interval = no_intervals - 1
while intervalcounts[n][last_test_interval]==0:
last_test_interval = last_test_interval-1
#print intervalcounts[n][last_test_interval], intervals[last_test_interval]
no_training_intervals = last_training_interval-first_training_interval
no_test_intervals = no_intervals - no_training_intervals
total_series_length = len(k)
num_batches = total_series_length//batch_size//truncated_backprop_length
regularizer = tf.add_n([tf.nn.l2_loss(a[n]),tf.nn.l2_loss(b[n]),tf.nn.l2_loss(c[n]),tf.nn.l2_loss(alpha),tf.nn.l2_loss(beta),tf.nn.l2_loss(gamma)])
D_loss = SwappedPairs
G_loss[n] = tf.reduce_sum(tf.exp(a[n]*h_series[n][-1]+b[n]*t_series[n][-1]+c[n])/b[n] - tf.exp(a[n]*h_series[n][-1]+b[n]*t_series[n][0]+c[n])/b[n]) - sum([tf.reduce_sum(a[n]*h_series[n][-1]+b[n]*(t_series[n][k1+1]-t_series[n][k1])+c[n]) for k1 in range(len(t_series)-1)])
adv_loss = tf.reduce_sum(D_loss) + tf.reduce_sum(G_loss[n])
total_loss = adv_loss+regularizer
train_step = tf.train.AdagradOptimizer(0.3).minimize(total_loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
loss_list = []
h_list = []
last_h = np.ones((batch_size))
last_batchk = np.ones((batch_size, truncated_backprop_length))
last_batcht = np.ones((batch_size, truncated_backprop_length))
#sess = tf_debug.LocalCLIDebugWrapperSession(sess)
#sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan)
#writer = tf.summary.FileWriter("/home/supritam/Documents/aaai/code", sess.graph)
for epoch_idx in range(num_epochs):
_current_h = np.ones((batch_size))
#print("New data, epoch", epoch_idx)
for batch_idx in range(num_batches):
start_idx = batch_idx * batch_size * truncated_backprop_length
end_idx = start_idx + batch_size * truncated_backprop_length
batchk = list(chunks(k[start_idx:end_idx], truncated_backprop_length))
batcht = list(chunks(train_t[start_idx:end_idx], truncated_backprop_length))
#---debugging statements---
#print batchk, _current_h
_a, _b, _c, _total_loss, _train_step, _current_h, = sess.run(
[a[n], b[n], c[n], total_loss, train_step, current_h],
feed_dict={
batchk_placeholder:batchk,
batcht_placeholder:batcht,
init_h:_current_h,
})
last_h = _current_h
last_batchk = batchk
last_batcht = batcht
#if batch_idx%100 == 0:
#print("Step", batch_idx, "Loss", _total_loss)
#print("a", _a)
#print("b", _b)
#print("c", _c)
saved_a, saved_b, saved_c, saved_alpha, saved_beta, saved_gamma = sess.run([a[n], b[n], c[n], alpha, beta, gamma], feed_dict={
batchk_placeholder:last_batchk,
batcht_placeholder:last_batcht,
init_h:last_h,
})
for i in range(first_training_interval, last_training_interval+1):
t1, t2 = intervals[i]
pred_intervalcounts[n][i] = math.exp(saved_a*last_h + saved_b*t2 + saved_c)/saved_b - math.exp(saved_a*last_h + saved_b*t1 + saved_c)/saved_b
for n in range(p):
cumulative = 0
cumulative_pred = 0
for i in range(first_training_interval, last_training_interval):
t1, t2 = intervals[i]
actual = intervalcounts[n][i]
cumulative = cumulative+actual
pred = pred_intervalcounts[n][i]
cumulative_pred = cumulative_pred+pred
trainAPE = trainAPE + abs(cumulative_pred-cumulative)/cumulative
#print "Dataset:", dataset, "Interval:", t1, t2, "MAPE:", trainAPE/(i+1)
trainMAPE = trainAPE/no_training_intervals
#print "Training MAPE:", trainMAPE
testAPE = trainAPE
for i in range(last_training_interval, last_test_interval+1):
#for i in range(last_training_interval, no_intervals):
t1, t2 = intervals[i]
actual = intervalcounts[n][i]
cumulative = cumulative+actual
pred = pred_intervalcounts[n][i]
cumulative_pred = cumulative_pred+pred
#print "Ntrue:", cumulative, "Npred:", cumulative_pred
testAPE = testAPE + abs(cumulative_pred-cumulative)/cumulative
#print "Dataset:", dataset, "Interval:", t1, t2, "MAPE:", testAPE/(i+1)
testMAPE = testAPE/no_intervals
#print "Test MAPE:", testMAPE
MAPEsum = MAPEsum + testMAPE
for x in range(rankintervals):
rankcounts[n][x] = sum(intervalcounts[n][last_training_interval+x*no_test_intervals/rankintervals:last_training_interval+(x+1)*no_test_intervals/rankintervals])
pred_rankcounts[n][x] = sum(pred_intervalcounts[n][last_training_interval+x*no_test_intervals/rankintervals:last_training_interval+(x+1)*no_test_intervals/rankintervals])
#print rankcounts[n]
#print pred_rankcounts[n]
#print
#print str(n)+"-"*50
#print
#with open("output_MAPE.txt", "a") as out:
#out.write(dataset + "\t" + names[n] + "\t" + str(float(testMAPE)) + "\n")
def convert_to_ranks(l):
aux = [(l[i], i) for i in range(len(l))]
aux2 = sorted(aux, key=lambda x:x[0], reverse=True)
rl = list(l)
for i in range(len(l)):
x = aux2[i]
rl[x[1]] = i
return rl
def SRCC(l1, l2):
rl1 = convert_to_ranks(l1)
rl2 = convert_to_ranks(l2)
return np.corrcoef(rl1, rl2)[0][1]
r = []
for x in range(rankintervals):
ranklist = convert_to_ranks([rankcounts[n][x] for n in range(p)])
pred_ranklist = convert_to_ranks([pred_rankcounts[n][x] for n in range(p)])
print ranklist, pred_ranklist
#r.append(np.corrcoef(ranklist, pred_ranklist)[0][1])
val = SRCC([rankcounts[n][x] for n in range(p)], [pred_rankcounts[n][x] for n in range(p)])
r.append(val)
#print "Dataset:", dataset, "Interval:", intervals[x*(no_intervals-1)/rankintervals][1], intervals[(x+1)*(no_intervals-1)/rankintervals][1], "SRCC:", val
r2 = []
for x in range(last_training_interval+1,no_intervals+1):
r2.append(SRCC([intervalcounts[n][x] for n in range(p)], [pred_intervalcounts[n][x] for n in range(p)]))
print r
meanSRCC = sum(r)/rankintervals
meanSRCC2 = sum(r2)/no_test_intervals
print "Mean MAPE:", MAPEsum/p, "Mean SRCC:", meanSRCC2, "No. of intervals:", no_intervals