-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_n-params.py
130 lines (116 loc) · 4.88 KB
/
plot_n-params.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
import matplotlib.pyplot as plt
import json, os, re
import numpy as np
from scipy.optimize import curve_fit
if __name__ == '__main__':
#models = set('gpt2', 'gpt2-xl', 'falcon-7b', 'falcon-40b', 'llama-13b', 'llama-65b', 'gpt-3.5-turbo', 'gpt-4')
with open ('model_to_n-params.json', 'r') as f:
model_to_nparams = json.load(f)
files = [ f
for f in os.listdir('./')
if re.search('performance_summary_.+\.json', f) is not None
]
performance = {'WebNLG': {}, 'NYT': {}}
for file in files:
with open(file, 'r') as f:
models, perfs = zip(*json.load(f).items())
models = [ m.split(' ')[0] for m in models ]
perfs = np.asarray([ list(p.values()) for p in perfs ])
if 'webnlg_modified' in file:
dataset = 'WebNLG'
setting = re.search('(?<=performance_summary_).+(?=_webnlg_modified)', file).group(0)
elif 'nyt' in file:
dataset = 'NYT'
setting = re.search('(?<=performance_summary_).+(?=_nyt)', file).group(0)
performance[dataset][setting] = (models, perfs)
plt.rcParams.update({'font.size': 44})
fig = plt.figure(figsize=(17,8))
ax = plt.subplot(111)
marker = dict(zip(('zero-shot', 'zero-shot+kb', 'few-shots'), ('o','*','v')))
label = dict(zip(('zero-shot', 'zero-shot+kb', 'few-shots'), ('Zero-Shot','Zero-Shot + KB','Few-Shots')))
scatter = []
kb_scatter = []
fs_scatter = []
label = {'zero-shot': '2-Shots', 'zero-shot+kb': '0.5-Shots (KB)', 'few-shots': '5-shots (KB)'}
fit = lambda x,a,b: a * x + b #lambda x,a,b: a*np.log(x) + b
for setting in ('zero-shot', 'zero-shot+kb', 'few-shots'):
nparams = [
model_to_nparams[model]
for dataset in ('WebNLG', 'NYT')
for model in performance[dataset][setting][0]
]
if setting != 'zero-shot':
indices = (
performance[dataset][setting][0].index('gpt-4') + 8,
performance[dataset][setting][0].index('gpt-3.5-turbo') + 8
)
else:
indices = ()
if setting == 'zero-shot':
color = ["#1f77b4" for _ in range(len(nparams))]
elif setting == 'zero-shot+kb':
color = ["#ff7f0e" for _ in range(len(nparams))]
else:
color = ["#2ca02c" for _ in range(len(nparams))]
for i in indices:
color[i] = "grey"
web_perf = performance['WebNLG'][setting][1][:,2]
nyt_perf = performance['NYT'][setting][1][:,2]
f1 = np.concatenate(
(web_perf / web_perf.max(),
nyt_perf / nyt_perf.max())
)
nparams = np.log(nparams)
ax.scatter(nparams, f1, s=200, label=label[setting], c=color)
x = nparams
if setting == 'zero-shot':
for i,item in enumerate(zip(nparams, f1)):
scatter.append(item)
elif setting == 'zero-shot+kb':
for i,item in enumerate(zip(nparams, f1)):
if i not in indices:
kb_scatter.append(item)
else:
for i,item in enumerate(zip(nparams, f1)):
if i not in indices:
fs_scatter.append(item)
#pars, _ = curve_fit(fit, x, f1)
#space = np.linspace(min(x), max(x), num=100)
#plt.plot(space, fit(space, *pars), linewidth=5)
def get_r2(y_pred, y_true):
ss_res = ((y_pred - y_true)**2).sum()
ss_tot = ((y_true - y_true.mean())**2).sum()
return 1 - (ss_res/ss_tot)
for scat in (scatter, kb_scatter, fs_scatter):
x, y = zip(*scat)
space = np.linspace(min(x), max(x), num=100)
pars = np.polyfit(x, y, 1)#curve_fit(fit, x, y)
print(pars)
print(get_r2(fit(np.array(x), *pars), np.array(y)))
ax.plot(space, fit(space, *pars), linewidth=5, linestyle='--')
#plt.xscale('log')
plt.xlabel(r'$N$ Parameters')
plt.ylabel('Normalized $F1$')
box = ax.get_position()
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
#plt.legend()
plt.tight_layout()
plt.savefig('f1_vs_nparams.pdf', format='pdf', dpi=300)
plt.show()
for dataset in ('WebNLG', 'NYT'):
plt.figure(figsize=(10,8))
for setting in ('zero-shot', 'zero-shot+kb', 'few-shots'):
nparams = [
model_to_nparams[model]
for model in performance[dataset][setting][0]
]
f1 = performance[dataset][setting][1][:,2]
f1 = f1 / f1.max()
plt.scatter(nparams, f1, marker=marker[setting], s=2000, label=label[setting])
plt.xscale('log')
plt.xlabel(r'$N$ Parameters')
plt.ylabel('Normalized $F1$')
#plt.legend()
#plt.show()
plt.tight_layout()
plt.savefig(f'f1_vs_nparams_{dataset}.pdf', format='pdf', dpi=300)