-
Notifications
You must be signed in to change notification settings - Fork 1
/
script_plot_attention_study_graph.py
159 lines (128 loc) · 4.45 KB
/
script_plot_attention_study_graph.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
from utils.results_perser_utils import file_parser
from utils.dynamic_plot_lib_v3 import dynamic_plot
import argparse
import os
import numpy as np
import math
class plots():
def __init__(self,title,grid_on = True):
SIZE = 12
fontsize = {'text':15,
'xtick':SIZE,
'xtick':15,
'title':15,
'axis':SIZE,
'legend':15,
'labels':SIZE,
'figure':SIZE}
self.plot = dynamic_plot(title,'#Attention Layers','F1',fontsize = fontsize)
self.grid_on = grid_on
def update(self,**arg):
key = arg['label']
f1 = np.array(arg['f1'])
layers = np.array(arg['layers'],dtype=int)
color = 'g'
karg = {'linestyle':'-'}
if 'color' in arg:
color = arg['color']
if 'linestyle' in arg:
karg['linestyle'] = arg['linestyle']
idx = np.argmax(f1)
m_max,f1_max = layers[idx],f1[idx]
self.axis_limit = {'xmin':min(layers),'xmax':max(layers),'ymin':0,'ymax':1}
self.plot.add_plot( key,
color=color,
save=False,
scale = 5,
window = 0,
label = key + " (best layer = %s)"%(m_max),
**karg)
self.plot.add_plot('scatter',
color=color,
save=False,
window = 0,
scale= 70,
framework='scatter'
)
# Add line data
self.plot.update_plot(key,layers,f1)
# Add best point
self.plot.update_plot('scatter',m_max,f1_max,color = color)
# Add fill area on the graph
if 'fill' in arg:
self.plot.addon(key, fill = arg['fill'])
self.plot.show(grid_on =self.grid_on,axis= self.axis_limit)
def save_data_file(self,root):
self.plot.save_data_file(root)
def hold(self):
self.plot.hold_fig()
def compt_sequence_stats(results,seq,enc,field):
attention = range(5)
net = enc
sessions = 'cross_val_' + '%02d'%(seq)
net_values = {}
values = {'mean':[],'std':[],'layers':[]}
for att in attention:
#for layer in layers:
encoder = results[(results.session == sessions) & (results.modelA == att)& (results.modelB == net)][field]
mean_value = round(np.mean(encoder[encoder!=-1]),3)
std_value = round(np.std(encoder[encoder!=-1]),3)
values['mean'].append(mean_value)
values['std'].append(std_value)
values['layers'].append(att)
return(values)
def compt_attention_stats(results,enc,field):
layers = range(5)
encoder = enc
values = {'mean':[],'std':[],'layers':[]}
for layer in layers:
attention = results[(results.modelA == layer) & (results.modelB == encoder)][field]
mean_value = round(np.mean(attention[attention!=-1]),3)
if math.isnan(mean_value) :
continue
std_value = round(np.std(attention[attention!=-1]),3)
if math.isnan(std_value):
std_value= 0
values['mean'].append(mean_value)
values['std'].append(std_value)
values['layers'].append(layer)
return(values)
if __name__ == '__main__':
parser = argparse.ArgumentParser("./script_plot_attention_study_graph.py")
parser.add_argument(
'--file', '-f',
type=str,
#default = "results/attention_select_study.txt",
default = "results_paper/attention_study.txt",
required=False,
help='Dataset to train with. No Default',
)
FLAGS, unparsed = parser.parse_known_args()
# Get File
file_to_parse = FLAGS.file
# Parse file
results = file_parser(file_to_parse)
# Demo: get all data belonging to cross_val_00
f1_scores_mean = compt_attention_stats(results,enc=5,field='F1')
print("Mean F1: {}".format(f1_scores_mean['mean']))
fig = plots('',grid_on=False)
sequences = [0,2,5,6,8]
for seq in sequences:
f1_scores = compt_sequence_stats(results,seq,enc=5,field='F1')
print("Seq {}: {}".format(seq,f1_scores['mean']))
if seq==8:
f1_scores_08 = f1_scores
fig.update(color = 'k',
label='mean',
f1 = f1_scores_mean['mean'],
layers=f1_scores_mean['layers'],
fill = f1_scores_mean['std'],
linestyle='--'
)
fig.update(color = 'k',
label='08',
f1 = f1_scores_08['mean'],
layers=f1_scores_08['layers'],
linestyle='-'
)
fig.hold()