-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw_plots.py
More file actions
151 lines (118 loc) · 5.08 KB
/
Copy pathdraw_plots.py
File metadata and controls
151 lines (118 loc) · 5.08 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
import numpy as np
import pickle
import matplotlib.pyplot as plt
from settings import *
import os
colors_lrl = {
'mean': {
1.0: 'red',
0.1: 'black'
},
'max': {
1.0: 'blue',
0.1: 'green'
}
}
colors_ltn = {
'sgd': {
0.01: 'purple',
0.1: 'grey',
0: 'orange'
},
'adam': {
0: 'red',
0.01: 'purple',
0.1: 'grey',
}
}
def find_color_lrl(method, schedule):
return colors_lrl[method][schedule]
def find_color_ltn(method, l):
return colors_ltn[method][l]
def find_label_lrl(method, schedule):
return 'ILR ({})'.format(schedule)
def find_label_ltn(method, l):
return '{} ({})'.format(method.upper(), l)
def generate_plots(key, title, axs, plot_row, plot_col, aggregate='mean', target=1.0):
x_axis = list(range(n_steps + 1))
for method in methods:
for lrl_schedule in lrl_schedules:
l_results = []
for p in results_lrl:
if p['target'] == target and p['method'] == method and p['schedule'] == lrl_schedule:
# There used to be a mean here??
fuzzy_sat_lrl = p[key]
if len(fuzzy_sat_lrl) < n_steps + 1:
to_fill = n_steps + 1 - len(fuzzy_sat_lrl)
fuzzy_sat_lrl = np.concatenate([fuzzy_sat_lrl, np.array([fuzzy_sat_lrl[-1]] * to_fill)])
l_results.append(fuzzy_sat_lrl)
if aggregate == 'mean':
sat_mean = np.stack(l_results).mean(axis=0)
if aggregate == 'mse':
sat_mean = np.sqrt(np.square(target - np.stack(l_results)).mean(axis=0))
axs[plot_row, plot_col].plot(x_axis,
sat_mean,
color=find_color_lrl(method, lrl_schedule),
label=find_label_lrl(method, lrl_schedule))
for reg_l in regularization_lambda_list:
for sgd_method in sgd_methods:
fuzzy_sat_ltn = np.array([p[key]
for p in results_ltn
if p['target'] == target and p['lambda'] == reg_l and p['sgd_method'] == sgd_method])
if aggregate == 'mean':
sat_mean = fuzzy_sat_ltn.mean(axis=0)
if aggregate == 'mse':
sat_mean = np.sqrt(np.square(target - fuzzy_sat_ltn).mean(axis=0))
axs[plot_row, plot_col].plot(x_axis,
sat_mean,
ls='--',
color=find_color_ltn(sgd_method, reg_l),
label=find_label_ltn(sgd_method, reg_l))
if title:
axs[plot_row, plot_col].set_title(title, fontweight='bold')
results_lrl = None
results_ltn = None
def create_figures(axes, col, tnorm, target):
generate_plots('sat_f', tnorm.capitalize(), axs=axes, plot_row=0, plot_col=col, aggregate='mean', target=target)
generate_plots('norm1_f', None, axs=axes, plot_row=1, plot_col=col, target=target)
for amt_rulez in [20, 91]:
plt.rcParams["figure.figsize"] = (11,5)
plt.subplots_adjust(right=0.7)
fig, axes = plt.subplots(2, 3, sharex='col', sharey='row')
plt.subplots_adjust(left=0.1,
bottom=0.1,
right=0.9,
top=0.85,
wspace=0.1,
hspace=0.2)
for ax in axes.flat:
ax.set(xlabel='Iteration')
axes[0,0].set_ylabel('Satisfaction', fontweight='bold')
axes[0,1].set_ylabel('Satisfaction', fontweight='bold')
axes[1,0].set_ylabel('L1 norm', fontweight='bold')
axes[1,1].set_ylabel('L1 norm', fontweight='bold')
# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axes.flat:
ax.label_outer()
for col, tnorm in enumerate(tnorms_plot):
if not os.path.exists(f'results/{tnorm}/lrl_{amt_rulez}_rules'):
continue
if not os.path.exists(f'plots/{tnorm}'):
os.makedirs(f'plots/{tnorm}')
base_path = f'plots/{tnorm}/{amt_rulez}_rules'
if not os.path.exists(base_path):
os.makedirs(base_path)
with open(f'results/{tnorm}/lrl_{amt_rulez}_rules', 'rb') as f:
results_lrl = pickle.load(f)
with open(f'results/{tnorm}/ltn_{amt_rulez}_rules', 'rb') as f:
results_ltn = pickle.load(f)
print(f'Creating figures for {tnorm} with {amt_rulez}', flush=True)
create_figures(axes, col, tnorm, target)
create_figures(axes, col, tnorm, target)
handles, labels = axes[0,0].get_legend_handles_labels()
lg = fig.legend(handles[:5], labels[:5], loc='upper center', ncol=5, prop={'size': 12}, borderaxespad=0.2)
# lg = fig.legend(handles[:5], labels[:5], loc='upper center', ncol=5, prop={'size': 18}, borderaxespad=0.2)
fig.savefig(f'plots_final/results_{amt_rulez}_{target}_final.png',
bbox_extra_artists=(lg,),
bbox_inches='tight')
plt.close()