-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_adaptive.py
More file actions
135 lines (92 loc) · 4.17 KB
/
Copy pathplot_adaptive.py
File metadata and controls
135 lines (92 loc) · 4.17 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
import os
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
path_fig = os.path.join(
'figs_adaptive',
'dataset_id={}-{}-adaptive.pdf'
)
weighted_banzhaf = np.arange(.1, 1, .1).round(1).tolist()
beta_shapley = [(16, 1), (4, 1), (1, 1), (1, 4), (1, 16), (16, 4), (2, 2), (8, 8), (4, 16)]
def skip_arg(arg):
if arg['n_queries_per_player'] != 1000:
return 1
if arg['n_queries_per_player_per_checkpoint'] != 10:
return 1
if arg['n_estimators'] == 0:
return 1
if arg['estimator'] not in ['linearAppr', 'Adalina', 'SHAP_IQ']:
return 1
if arg['paired_sampling']:
return 1
if arg['dataset_id'] not in [44, 1475, 41145, 41150]:
return 1
return 0
def plot_curves(results, dataset_id):
est2key = {
'linearAppr-empty' : r'$\gamma=u_{\emptyset}$',
'linearAppr-default' : r'$\gamma=\frac{u_{[n]}+u_{\emptyset}}{2}$',
'Adalina' : 'adaptive',
'SHAP_IQ' : 'SHAP-IQ'
}
ests = ['linearAppr-empty', 'linearAppr-default', 'Adalina', 'SHAP_IQ']
for j in [0, 1]:
if j:
path = path_fig.format(dataset_id, 'banzhaf')
else:
path = path_fig.format(dataset_id, 'shapley')
path_components = path.split(os.sep)
os.makedirs(os.sep.join(path_components[:-1]), exist_ok=True)
fig, ax = plt.subplots(figsize=(32, 24))
palette = sns.color_palette("tab10")
palette = [palette[1], palette[3], palette[0], palette[2]]
curves_all = []
for est in ests:
curves_all.append(results[est][j])
for i, (key, curves) in enumerate(zip(ests, curves_all)):
key = est2key[key]
curve_mean = curves.mean(axis=1)
curve_std = curves.std(axis=1)
ax.semilogy(np.arange(9), curve_mean, linewidth=10, label=key, c=palette[i])
ax.fill_between(np.arange(9), curve_mean - curve_std, curve_mean + curve_std, alpha=0.2, color=palette[i])
ax.tick_params(axis='x', labelsize=80)
ax.tick_params(axis='y', labelsize=80)
if j:
plt.xlabel('Weighted Banzhaf values', fontsize=100)
plt.xticks(np.arange(9), [str(e) for e in weighted_banzhaf])
else:
plt.xlabel('Beta Shapley values', fontsize=100)
plt.xticks(np.arange(9), [str(e) for e in beta_shapley])
xticklabels = ax.get_xticklabels()
for i in range(9):
xticklabels[i].set_rotation(-90)
plt.legend(fontsize=100)
plt.ylabel(r'$\|\hat{\phi}-\phi\|_{2} / \|\phi\|_{2}$', fontsize=100)
plt.savefig(path, bbox_inches='tight')
plt.close(fig)
if __name__ == '__main__':
from main import arg_dict
from args import process_arg_dict
from collections import defaultdict
args = process_arg_dict(arg_dict)
args_per_id = defaultdict(list)
for arg in args:
if skip_arg(arg):
continue
args_per_id[arg['dataset_id']].append(arg)
for dataset_id, args_2nd in args_per_id.items():
results = defaultdict(lambda : np.empty((2, 9, 10), dtype=np.float64))
for arg in args_2nd:
est = arg['estimator']
if 'aux' in arg:
est += '-' + arg['aux']
data = np.load(arg['path_groundtruth'])
groundtruth = data['groundtruth']
data = np.load(arg['path_estimate'])
estimate = data['estimate_traj'][-1]
error = np.linalg.norm(estimate - groundtruth) / np.linalg.norm(groundtruth)
if isinstance(arg['semivalue'], tuple):
results[est][0, beta_shapley.index(arg['semivalue']), arg['random_seed_estimator']] = error
else:
results[est][1, weighted_banzhaf.index(arg['semivalue']), arg['random_seed_estimator']] = error
plot_curves(results, dataset_id)