-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperf_utils.py
More file actions
149 lines (137 loc) · 5.85 KB
/
perf_utils.py
File metadata and controls
149 lines (137 loc) · 5.85 KB
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
from matplotlib import pyplot as plt
import numpy as np
import re
def get_f1_at_each_step(path):
list_of_lists = []
with open(path, 'r') as f:
for line in f:
inner_list = [float(elt.strip()) for elt in line.split('\t')]
list_of_lists.append(inner_list)
f1_matrix = np.array(list_of_lists)
# return [np.mean(f1_matrix[i,:i+1]) for i in range(6)]
return [np.mean(f1_matrix[i,:i+1]) for i in range(len(f1_matrix))]
def get_forg_at_each_step(path):
list_of_lists = []
with open(path, 'r') as f:
for line in f:
inner_list = [float(elt.strip()) for elt in line.split('\t')]
list_of_lists.append(inner_list)
f1_matrix = np.array(list_of_lists)
bwt = [0]
# for i in [1,2,3,4,5]:
for i in range(1,len(f1_matrix)):
temp_bwt=[]
for j in range(i):
temp_bwt.append(f1_matrix[i,j]-f1_matrix[i-1,j])
bwt.append(np.mean(temp_bwt))
return bwt
def get_new_at_each_step(path):
list_of_lists = []
with open(path, 'r') as f:
for line in f:
inner_list = [float(elt.strip()) for elt in line.split('\t')]
list_of_lists.append(inner_list)
f1_matrix = np.array(list_of_lists)
# return [f1_matrix[i,i] for i in range(6)]
return [f1_matrix[i,i] for i in range(len(f1_matrix))]
def get_overall_f1(path):
list_of_lists = []
with open(path, 'r') as f:
for line in f:
inner_list = [float(elt.strip()) for elt in line.split('\t')]
list_of_lists.append(inner_list)
f1_matrix = np.array(list_of_lists)
return np.mean(f1_matrix[1,:2])
def get_overall_f1_all(path,t=6):
list_of_lists = []
with open(path, 'r') as f:
for line in f:
inner_list = [float(elt.strip()) for elt in line.split('\t')]
list_of_lists.append(inner_list)
f1_matrix = np.array(list_of_lists)
if t==6:
# return np.mean(f1_matrix[5,:])
return np.mean(f1_matrix[len(f1_matrix)-1,:])
else: # t is a list
return np.mean([f1_matrix[5,i] for i in t])
def get_forgetting(path):
list_of_lists = []
with open(path, 'r') as f:
for line in f:
inner_list = [float(elt.strip()) for elt in line.split('\t')]
list_of_lists.append(inner_list)
f1_matrix = np.array(list_of_lists)
temp_forgetting = []
for i in range(1): # for i in range(5):
temp_forgetting.append(np.max(f1_matrix[i:-1,i])-f1_matrix[1,i]) # temp_forgetting.append(np.max(f1_matrix[i:-1,i])-f1_matrix[5,i])
return np.mean(temp_forgetting)
def get_forgetting_all(path,t=6):
list_of_lists = []
with open(path, 'r') as f:
for line in f:
inner_list = [float(elt.strip()) for elt in line.split('\t')]
list_of_lists.append(inner_list)
f1_matrix = np.array(list_of_lists)
temp_forgetting = []
# for i in range(5):
# temp_forgetting.append(np.max(f1_matrix[i:-1,i])-f1_matrix[5,i])
for i in range(len(f1_matrix)-1):
temp_forgetting.append(np.max(f1_matrix[i:-1,i])-f1_matrix[len(f1_matrix)-1,i])
if t==6:
return np.mean(temp_forgetting)
else: # t is a list
return np.mean([temp_forgetting[i] for i in t if i!=5])
def get_newtask(path):
list_of_lists = []
with open(path, 'r') as f:
for line in f:
inner_list = [float(elt.strip()) for elt in line.split('\t')]
list_of_lists.append(inner_list)
f1_matrix = np.array(list_of_lists)
return f1_matrix[1,1]
def get_newtask_all(path,t=6):
list_of_lists = []
with open(path, 'r') as f:
for line in f:
inner_list = [float(elt.strip()) for elt in line.split('\t')]
list_of_lists.append(inner_list)
f1_matrix = np.array(list_of_lists)
# new_task = [f1_matrix[i,i] for i in range(6)]
new_task = [f1_matrix[i,i] for i in range(len(f1_matrix))]
if t==6:
return np.mean(new_task)
else:
return np.mean([new_task[i] for i in t])
def get_oldtask(path):
list_of_lists = []
with open(path, 'r') as f:
for line in f:
inner_list = [float(elt.strip()) for elt in line.split('\t')]
list_of_lists.append(inner_list)
f1_matrix = np.array(list_of_lists)
return f1_matrix[1,0]
# def get_res_fname(rand_idx,seed,path,dataset):
# if 'ANCLMAS' in path or 'ANCLEWC' in path:
# return dataset+'_bert_adapter_ewc_ancl_'+'random'+str(rand_idx)+'_seed'+str(seed)+'_f1.txt'
# elif 'ANCLLWF' in path:
# return dataset+'_bert_adapter_lwf_ancl_'+'random'+str(rand_idx)+'_seed'+str(seed)+'_f1.txt'
# elif 'LWF' in path:
# return dataset+'_bert_adapter_lwf_'+'random'+str(rand_idx)+'_seed'+str(seed)+'_f1.txt'
# elif 'LAEWC' in path or 'LAMAS' in path:
# return dataset+'_bert_adapter_ewc_freeze_'+'random'+str(rand_idx)+'_seed'+str(seed)+'_f1.txt'
# else:
# return dataset+'_bert_adapter_ewc_'+'random'+str(rand_idx)+'_seed'+str(seed)+'_f1.txt'
def get_res_fname(rand_idx,seed,path,dataset,val=True):
path_append = '_val.txt' if val else '.txt'
if 'ANCLMAS' in path or 'ANCLEWC' in path:
return dataset+'_bert_adapter_ewc_ancl_'+'random'+str(rand_idx)+'_seed'+str(seed)+'_f1'+path_append
elif 'ANCLLWF' in path:
return dataset+'_bert_adapter_lwf_ancl_'+'random'+str(rand_idx)+'_seed'+str(seed)+'_f1'+path_append
elif 'LAEWC' in path or 'LAMAS' in path:
return dataset+'_bert_adapter_ewc_freeze_'+'random'+str(rand_idx)+'_seed'+str(seed)+'_f1'+path_append
elif 'UPGD' in path:
return dataset+'_bert_adapter_upgd_'+'random'+str(rand_idx)+'_seed'+str(seed)+'_f1'+path_append
elif 'RP2F' in path:
return dataset+'_bert_adapter_rp2f_sh_'+'random'+str(rand_idx)+'_seed'+str(seed)+'_f1'+path_append
elif 'Adabop' in path:
return dataset+'_bert_adapter_adabop_'+'random'+str(rand_idx)+'_seed'+str(seed)+'_f1'+path_append