-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
318 lines (250 loc) · 10.9 KB
/
plot.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
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# df = pd.read_csv('./favor_0.5_0.csv')
# for name in ['softmax', 'sparsemax', 'linear', 'favor', 'topk', 'rand']:
# if name in ['topk', 'rand']:
# for p in [0.2, 0.5, 0.8]:
# for i in range(5):
# df2 = pd.read_csv(f"{name}_{p}_{i}.csv")
# df = pd.concat([df, df2], keys=df.columns, ignore_index=True)
# else:
# for p in [0.5]:
# for i in range(5):
# df2 = pd.read_csv(f"{name}_{p}_{i}.csv")
# df = pd.concat([df, df2], keys=df.columns,ignore_index=True)
def load_data(tgt_name, tgt_plot_name):
data = {
'epoch':[],
tgt_name:[],
'Model':[]
}
for i, row in df.iterrows():
data['epoch'].append(row['epoch'])
data[tgt_name].append(row[tgt_name])
if row['model'] == 'sparsemax':
data['Model'].append('Sparse Hopfield [Hu et al., 2023]')
elif row['model'] == 'softmax':
data['Model'].append('Dense Hopfield [Ramsauer et al. 2020]')
elif row['model'] == 'favor':
data['Model'].append('Random Feature Hopfield')
elif row['model'] == 'linear':
data['Model'].append('Linear Hopfield')
elif row['model'] == 'topk':
p = round(float(row['prob'])*100)
data['Model'].append(f'Top {p}% Hopfield')
elif row['model'] == 'rand':
p = round(float(row['prob']), 1)
data['Model'].append(f'Random Masked Hopfield {p}')
return pd.DataFrame(data)
def generate_plot(tgt_name, tgt_plot_name):
data = load_data(tgt_name, tgt_plot_name)
fig = plt.figure(figsize=(12,8))
plt.title(tgt_plot_name, fontsize=20)
# sns.set_theme(context='talk',font='sans-serif',font_scale=1.0)
sns.despine(left=False,top=True, right=True, bottom=False)
sns.lineplot(data=data, x="Memory Size", y=tgt_name, hue="Model", errorbar=None)
plt.xlabel("Memory Size",fontsize=20)
plt.ylabel(tgt_plot_name,fontsize=20)
plt.yticks(fontsize=20)
plt.xticks(fontsize=20)
plt.legend(fontsize=20, prop = {"size": 20})
fig.tight_layout()
plt.savefig(f"{tgt_plot_name}.png")
plt.clf()
# generate_plot("train acc", "Train Accuracy")
# generate_plot("test acc", "Validation Accuracy")
# generate_plot("train loss", "Train Loss")
# generate_plot("test loss", "Validation Loss")
def load_data_rand_only(tgt_name, tgt_plot_name):
df = pd.read_csv('./mts_cost.csv')
data = {
'Memory Size':[],
tgt_name:[],
'Model':[]
}
for i, row in df.iterrows():
if row['model'] == 'rand_fast':
data['Memory Size'].append(row['Sequence Length'])
data[tgt_name].append(row[tgt_name])
p = round(float(row['Prob']), 1)
data['Model'].append(f'Random Masked Hopfield {p}')
return pd.DataFrame(data)
def load_data2(tgt_name, tgt_plot_name):
df = pd.read_csv('./mts_cost.csv')
data = {
'Memory Size':[],
tgt_name:[],
'Model':[]
}
for i, row in df.iterrows():
if row['model'] == 'sparsemax':
data['Model'].append('Sparse Hopfield [Hu et al., 2023]')
data['Memory Size'].append(row['Sequence Length'])
data[tgt_name].append(row[tgt_name])
elif row['model'] == 'softmax':
data['Model'].append('Dense Hopfield [Ramsauer et al. 2020]')
data['Memory Size'].append(row['Sequence Length'])
data[tgt_name].append(row[tgt_name])
elif row['model'] == 'favor':
data['Model'].append('Random Feature Hopfield')
data['Memory Size'].append(row['Sequence Length'])
data[tgt_name].append(row[tgt_name])
elif row['model'] == 'linear':
data['Model'].append('Linear Hopfield')
data['Memory Size'].append(row['Sequence Length'])
data[tgt_name].append(row[tgt_name])
elif row['model'] == 'topk':
p = round(float(row['Prob'])*100)
data['Model'].append(f'Top {p}% Hopfield')
data['Memory Size'].append(row['Sequence Length'])
data[tgt_name].append(row[tgt_name])
elif row['model'] == 'window':
data['Model'].append('Window Hopfield')
data['Memory Size'].append(row['Sequence Length'])
data[tgt_name].append(row[tgt_name])
else:
print()
# elif row['model'] == 'rand_fast':
# p = round(float(row['Prob']), 1)
# data['Model'].append(f'Random Masked Hopfield {p}')
return pd.DataFrame(data)
def generate_plot2(tgt_name, tgt_plot_name):
data = load_data2(tgt_name, tgt_plot_name)
fig = plt.figure(figsize=(12,8))
plt.title(tgt_plot_name, fontsize=20)
# sns.set_theme(context='talk',font='sans-serif',font_scale=1.0)
sns.despine(left=False,top=True, right=True, bottom=False)
sns.lineplot(data=data, x="Memory Size", y=tgt_name, hue="Model", errorbar=None, alpha=0.9, style='Model', linewidth=3)
plt.xlabel("Memory Size",fontsize=20)
plt.ylabel(tgt_plot_name,fontsize=20)
plt.yticks(fontsize=20)
plt.xticks(fontsize=20)
plt.legend(fontsize=20, prop = {"size": 20})
fig.tight_layout()
plt.savefig(f"{tgt_plot_name}.png")
plt.clf()
# generate_plot2("duration", "duration")
# generate_plot2("Flops", "Flops")
def generate_plot_rand_only(tgt_name, tgt_plot_name):
data = load_data_rand_only(tgt_name, tgt_plot_name)
fig = plt.figure(figsize=(12,8))
plt.title(tgt_plot_name, fontsize=20)
# sns.set_theme(context='talk',font='sans-serif',font_scale=1.0)
sns.despine(left=False,top=True, right=True, bottom=False)
sns.lineplot(data=data, x="Memory Size", y=tgt_name, hue="Model", errorbar=None, alpha=0.8, style='Model', linewidth=3)
plt.xlabel("Memory Size",fontsize=20)
plt.ylabel(tgt_plot_name,fontsize=20)
plt.yticks(fontsize=20)
plt.xticks(fontsize=20)
plt.legend(fontsize=20, prop = {"size": 20})
fig.tight_layout()
plt.savefig(f"{tgt_plot_name}_rand.png")
plt.clf()
# generate_plot_rand_only("duration", "duration")
# generate_plot_rand_only("Flops", "Flops")
def load_data_mil(tgt_name=None, tgt_plot_name=None):
df = pd.read_csv('loss_results/mnist_mil.csv')
tgt_name = 'best test acc'
data = {
'Bag Size':[],
"Test Accuracy":[],
'Model':[]
}
for i, row in df.iterrows():
data['Bag Size'].append(int(row['bag_size']))
data["Test Accuracy"].append(float(row[tgt_name]))
if row['mode'] == 'sparsemax':
data['Model'].append('Sparse Hopfield [Hu et al., 2023]')
elif row['mode'] == 'softmax':
data['Model'].append('Dense Hopfield [Ramsauer et al. 2020]')
elif row['mode'] == 'favor':
data['Model'].append('Random Feature Hopfield')
elif row['mode'] == 'linear':
data['Model'].append('Linear Hopfield')
elif row['mode'] == 'topk':
p = round(float(row['prob'])*100)
data['Model'].append(f'Top {p}% Hopfield')
elif row['mode'] == 'rand':
p = round(float(row['prob']), 1)
data['Model'].append(f'Random Masked Hopfield {p}')
return pd.DataFrame(data).sort_values(by=['Model'])
def generate_plot_mil(tgt_name, tgt_plot_name):
data = load_data_mil(tgt_name, tgt_plot_name)
fig = plt.figure(figsize=(12,6))
plt.title(tgt_plot_name, fontsize=20)
# sns.set_theme(context='talk',font='sans-serif',font_scale=1.0)
sns.despine(left=False,top=True, right=True, bottom=False)
sns.lineplot(data=data, x="Bag Size", y="Test Accuracy", hue="Model", alpha=0.8,marker="o", errorbar=None, markersize=5, linewidth=3)
plt.xlabel("Memory Size",fontsize=20)
plt.ylabel(tgt_plot_name,fontsize=20)
plt.yticks(fontsize=20)
plt.xticks(fontsize=20)
plt.legend(fontsize=20, prop = {"size": 20})
fig.tight_layout()
plt.savefig(f"{tgt_plot_name}_mil.png")
plt.clf()
# generate_plot_mil("", "")
# logs = {
# 'duration':[],
# 'Model':[],
# 'Horizon Length':[],
# 'Prob':[]
# }
# df = pd.DataFrame(logs)
# df.to_csv('./time_series_duration_etth1.csv', index=False)
def load_data_mts(tgt_name=None, tgt_plot_name=None):
df = pd.read_csv('time_series_duration_etth1.csv')
tgt_name = 'duration'
data = {
'Horizon Length':[],
"Time (ms) per epoch":[],
'Model':[]
}
for i, row in df.iterrows():
# data['Horizon Length'].append(int(row['Horizon Length']))
# data["Time (ms) per epoch"].append(float(row[tgt_name]))
if row['Model'] == 'sparsemax':
data['Horizon Length'].append(int(row['Horizon Length']))
data["Time (ms) per epoch"].append(float(row[tgt_name]))
data['Model'].append('Sparse Hopfield [Hu et al., 2023]')
elif row['Model'] == 'window':
data['Horizon Length'].append(int(row['Horizon Length']))
data["Time (ms) per epoch"].append(float(row[tgt_name]))
data['Model'].append('Window Hopfield')
elif row['Model'] == 'softmax':
data['Horizon Length'].append(int(row['Horizon Length']))
data["Time (ms) per epoch"].append(float(row[tgt_name]))
data['Model'].append('Dense Hopfield [Ramsauer et al. 2020]')
elif row['Model'] == 'favor':
data['Horizon Length'].append(int(row['Horizon Length']))
data["Time (ms) per epoch"].append(float(row[tgt_name]))
data['Model'].append('Random Feature Hopfield')
elif row['Model'] == 'linear':
data['Horizon Length'].append(int(row['Horizon Length']))
data["Time (ms) per epoch"].append(float(row[tgt_name]))
data['Model'].append('Linear Hopfield')
elif row['Model'] == 'topk':
data['Horizon Length'].append(int(row['Horizon Length']))
data["Time (ms) per epoch"].append(float(row[tgt_name]))
p = round(float(row['Prob'])*100)
data['Model'].append(f'Top {p}% Hopfield')
return pd.DataFrame(data).sort_values(by=['Model'])
def generate_plot_mts(tgt_name, tgt_plot_name):
data = load_data_mts(tgt_name, tgt_plot_name)
fig = plt.figure(figsize=(12,6))
plt.title(tgt_plot_name, fontsize=20)
# sns.set_theme(context='talk',font='sans-serif',font_scale=1.0)
sns.despine(left=False,top=True, right=True, bottom=False)
sns.lineplot(data=data, x="Horizon Length", y="Time (ms) per epoch", hue="Model", style="Model", alpha=0.8,marker="o", errorbar=None, markersize=5, linewidth=3)
plt.xlabel("Memory Size",fontsize=20)
plt.ylabel(tgt_plot_name,fontsize=20)
plt.yticks(fontsize=20)
plt.xticks(fontsize=20)
plt.legend(fontsize=20, prop = {"size": 20})
fig.tight_layout()
plt.savefig(f"{tgt_plot_name}_mts.png")
plt.clf()
generate_plot_mts("", "")