-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment_rbm.py
217 lines (200 loc) · 6.38 KB
/
experiment_rbm.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
"""
Run Gaussian-Bernoulli Restricted Boltzmann Machine experiment
using data from the directories data/RBM/ and parametric/RBM
as proposed in Section 4.4 of our paper
KSD Aggregated Goodness-of-fit Test
Antonin Schrab, Benjamin Guedj, Arthur Gretton
https://arxiv.org/pdf/2202.00824.pdf
Results are saved as dataframes in the directory results/.
"""
from kernel import stein_kernel_matrices, ratio_ksd_stdev
from ksd_single import ksd_parametric
from ksd_aggregated import ksdagg_parametric
from ksdagg.np import ksdagg
from pathlib import Path
import numpy as np
import pandas as pd
import time
import argparse
# create results directory if it does not exist
Path("results").mkdir(exist_ok=True, parents=True)
# panda dataframe: lists of indices and entries
index_vals = []
results = []
test_names = (
"ksdagg",
"median",
"split",
"split_extra_data",
)
filenames = ["rbm_s" + str(s) for s in [0, 1, 2, 3, 4]]
# run all the experiments
t = time.time()
verbose = True
weights_type = "uniform"
kernel_type = "imq"
beta_imq = 0.5
# B1 = 500 as in B1_parametric
# B2 = 500 as in B2_parametric
B3 = 50
d = 50
l_minus = -20
l_plus = 0
alpha = 0.05
number_samples = 1000
repetitions = 200
ksdagg_power = np.zeros(len(filenames))
for f in range(len(filenames)):
filename = filenames[f]
rs = np.random.RandomState(0)
X_rep = np.load("data/RBM/X_" + filename + ".npy").reshape(-1, d)
score_X_rep = np.load("data/RBM/score_X_" + filename + ".npy").reshape(-1, d)
B_parametric = np.load("parametric/RBM/B_parametric" + str(number_samples) + ".npy")
B1_parametric = np.load(
"parametric/RBM/B1_parametric" + str(number_samples) + ".npy"
)
B1_parametric_split = np.load(
"parametric/RBM/B1_parametric_split" + str(number_samples) + ".npy"
)
B2_parametric = np.load(
"parametric/RBM/B2_parametric" + str(number_samples) + ".npy"
)
median_bandwidth = np.load(
"parametric/RBM/bandwidth" + str(number_samples) + ".npy"
)
if verbose:
print(" ")
print("Starting f =", f + 1, "/", len(filenames))
print("bandwidth", median_bandwidth)
ksdagg_param_results = np.zeros(repetitions)
ksdagg_results = np.zeros(repetitions)
median_results = np.zeros(repetitions)
split_results = np.zeros(repetitions)
split_extra_data_results = np.zeros(repetitions)
for r in range(repetitions):
indices = rs.choice(X_rep.shape[0] - 1, size=number_samples, replace=False)
X = X_rep[indices]
score_X = score_X_rep[indices]
X_extra = X_rep[indices + 1]
score_X_extra = score_X_rep[indices + 1]
# KSDAgg
ksdagg_results[r] = ksdagg(
X,
score_X,
kernel="imq",
number_bandwidths=10,
weights_type="uniform",
approx_type="wild bootstrap",
B1=2000,
B2=2000,
B3=50,
seed=42,
)
# KSDAgg parametric
ksdagg_param_results[r] = ksdagg_parametric(
X,
score_X,
alpha,
beta_imq,
kernel_type,
weights_type,
l_minus,
l_plus,
median_bandwidth,
B1_parametric,
B2_parametric,
B3,
)
# Median
median_results[r] = ksd_parametric(
X, score_X, alpha, beta_imq, kernel_type, median_bandwidth, B_parametric
)
# Stein kernel matrices
bandwidths_collection = np.array(
[2**i * median_bandwidth for i in range(l_minus, l_plus + 1)]
)
stein_kernel_matrices_list = stein_kernel_matrices(
X,
score_X,
kernel_type,
bandwidths_collection,
beta_imq,
)
stein_kernel_matrices_list_extra_data = stein_kernel_matrices(
X_extra,
score_X_extra,
kernel_type,
bandwidths_collection,
beta_imq,
)
# Split
split_size = int(number_samples // 2)
ratio_values = []
for i in range(len(stein_kernel_matrices_list)):
H = stein_kernel_matrices_list[i][:split_size, :split_size]
ratio_values.append(ratio_ksd_stdev(H))
selected_bandwidth = bandwidths_collection[np.argmax(ratio_values)]
split_results[r] = ksd_parametric(
X[split_size:],
score_X[split_size:],
alpha,
beta_imq,
kernel_type,
selected_bandwidth,
B1_parametric_split[np.argmax(ratio_values)],
)
# Split extra data
ratio_values = []
for i in range(len(stein_kernel_matrices_list_extra_data)):
H_extra = stein_kernel_matrices_list_extra_data[i]
ratio_values.append(ratio_ksd_stdev(H_extra))
selected_bandwidth = bandwidths_collection[np.argmax(ratio_values)]
split_extra_data_results[r] = ksd_parametric(
X,
score_X,
alpha,
beta_imq,
kernel_type,
selected_bandwidth,
B1_parametric[np.argmax(ratio_values)],
)
if (r + 1) % 10 == 0 and verbose:
print(
"Step f =",
f + 1,
"/",
len(filenames),
",",
r + 1,
"/",
repetitions,
"time:",
time.time() - t,
)
t = time.time()
power_level = (
np.mean(ksdagg_param_results),
np.mean(median_results),
np.mean(split_results),
np.mean(split_extra_data_results),
)
ksdagg_power[f] = np.mean(ksdagg_results)
if verbose:
for i in range(len(power_level)):
print(f, test_names[i], power_level[i])
for i in range(len(power_level)):
index_vals.append((f, test_names[i]))
results.append(power_level[i])
# save panda dataframe
index_names = (
"noise",
"test",
)
index = pd.MultiIndex.from_tuples(index_vals, names=index_names)
results_df = pd.Series(results, index=index).to_frame("power/level")
results_df.reset_index().to_csv("results/results_RBM.csv")
results_df.to_pickle("results/results_RBM.pkl")
# save numpy array
np.save("results/ksdagg_rbm.npy", ksdagg_power.reshape(1, -1))
if verbose:
print("Dataframes for RBM experiment have been saved in results/.")