-
Notifications
You must be signed in to change notification settings - Fork 0
/
different_a_experimental.py
135 lines (110 loc) · 3.1 KB
/
different_a_experimental.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
import numpy as np
import matplotlib.pyplot as plt
import src.fpeqs as fpe
import src.numerics as num
import src.plotting_utils as pu
from tqdm.auto import tqdm
from src.utils import check_saved, load_file, save_file, experiment_runner
save = True
random_number = np.random.randint(0, 100)
names_cm = ["Purples", "Blues", "Greens", "Oranges", "Greys"]
def get_cmap(n, name="hsv"):
return plt.cm.get_cmap(name, n)
loss_name = "Huber"
delta_small = 0.1
delta_large = 10.0
percentage = 0.05
a_values = [0.1, 0.5, 1.0, 2.0, 10.0]
experimental_settings = [
{
"loss_name": loss_name,
"alpha_min": 0.01,
"alpha_max": 100,
"alpha_pts": 15,
"reg_param": 1.5,
"repetitions": 15,
"n_features": 500,
"percentage": percentage,
"delta_small": delta_small,
"delta_large": delta_large,
"a": a,
"experiment_type": "exp",
}
for a in a_values
]
theoretical_settings = [
{
"loss_name": loss_name,
"alpha_min": 0.01,
"alpha_max": 100,
"alpha_pts": 35,
"reg_param": 1.5,
"n_features": 500,
"percentage": percentage,
"delta_small": delta_small,
"delta_large": delta_large,
"a": a,
"experiment_type": "theory",
}
for a in a_values
]
n_exp = len(a_values)
alphas_num = [None] * n_exp
errors_mean_num = [None] * n_exp
errors_std_num = [None] * n_exp
alphas_theory = [None] * n_exp
errors_theory = [None] * n_exp
for idx, (exp_dict, theory_dict) in enumerate(
zip(tqdm(experimental_settings, desc="a values", leave=False), theoretical_settings)
):
file_exists, file_path = check_saved(**exp_dict)
if not file_exists:
experiment_runner(**exp_dict)
exp_dict.update({"file_path": file_path})
alphas_num[idx], errors_mean_num[idx], errors_std_num[idx] = load_file(**exp_dict)
file_exists, file_path = check_saved(**theory_dict)
if not file_exists:
experiment_runner(**theory_dict)
theory_dict.update({"file_path": file_path})
alphas_theory[idx], errors_theory[idx] = load_file(**theory_dict)
# ------------
pu.initialization_mpl()
fig, ax = plt.subplots(1, 1, figsize=(10, 8), tight_layout=True)
for idx, (al_n, err_m, err_s, al_t, err_t, a) in enumerate(
zip(
alphas_num,
errors_mean_num,
errors_std_num,
alphas_theory,
errors_theory,
a_values,
)
):
colormap = get_cmap(n_exp + 3)
ax.plot(
al_t,
err_t,
# marker='.',
color=colormap(idx + 3),
)
ax.errorbar(
al_n,
err_m,
err_s,
marker=".",
# linestyle="None",
color=colormap(idx + 3),
label=r"$a = {:.1f}$".format(a),
)
ax.set_title(
r"{} Loss - $\Delta = [{:.2f}, {:.2f}]$".format(loss_name, delta_small, delta_large)
)
ax.set_ylabel(r"$\frac{1}{d} E[||\hat{w} - w^\star||^2]$")
ax.set_xlabel(r"$\alpha$")
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlim([0.009, 110])
ax.legend()
if save:
pu.save_plot(fig, "{}_together_different_a_{:d}".format(loss_name, random_number))
plt.show()