-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment_2.py
144 lines (116 loc) · 4.39 KB
/
experiment_2.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
import sys
import os
from kernel_methods import KernelApprox
import numpy as np
import time
from py_markdown_table.markdown_table import markdown_table
from cross import aca_partial
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def format_e(n):
a = '%.3E' % n
return a.split('E')[0].rstrip('0').rstrip('.') + 'E' + a.split('E')[1]
kernel_names = ["matern", "squared-exponential", "multiquadrics", "thin-plate-spline"]
# The number of Chebyshev points to take
num_chebyshev_points = 32
# The number of source and target points.
num_source_points = 5000
num_target_points = 5000
# The number of samples taken from the parameter space
num_samples = 300
# Dimension of the problem
dimension = 3
# generate source and target points
X = np.random.uniform(0, 1, (num_source_points, dimension))
Y = np.random.uniform(1, 2, (num_target_points, dimension))
D_b = np.sqrt(3)
# initialize low rank kernel approximation object
TTK = KernelApprox(X, Y, num_chebyshev_points)
# tensor train lists.
offline_time_tt = []
online_time_tt = []
errs_tt = []
storage_cost_tt = []
data_tt = []
# ACA lists
errs_ACA = []
time_ACA = []
data_aca = []
tol_lst = [1E-4, 1E-6, 1E-8]
compression_ranks = []
# generate parameter space
param_ell = np.random.uniform(D_b * .5, D_b, num_samples)
param_nu = np.random.uniform(.5, 3, num_samples)
print("Dimension of Problem: ", dimension)
print("Number of Chebyshev Points used: ", num_chebyshev_points)
print("Num of Source Points: ", num_source_points)
print("Num of Target Points: ", num_target_points)
print("Source Box: ", 0, 1)
print("Target Box: ", 1, 2)
for kernel_name in kernel_names:
for my_tol in tol_lst:
offline_t0 = time.perf_counter()
if kernel_name == "matern":
sto, Fsh, Fth = TTK.parametric_kernel_approximation(kernel_name, [(D_b * .5, D_b), (.5, 3)], "tt",
tol=my_tol)
else:
sto, Fsh, Fth = TTK.parametric_kernel_approximation(kernel_name, [(D_b * .5, D_b)], "tt",
tol=my_tol)
offline_t = time.perf_counter() - offline_t0
r_compr = 0
vec_fun = TTK.get_curr_kernel_function()
for i in range(num_samples):
ell = param_ell[i]
nu = param_nu[i]
if kernel_name == "matern":
ell = param_ell[i]
nu = param_nu[i]
my_param = [ell, nu]
else:
ell = param_ell[i]
my_param = [ell]
param_tuple = tuple(my_param)
p_vec_fun = lambda diff_sq: vec_fun(diff_sq, *param_tuple)
online_t0 = time.perf_counter()
Mh = TTK.online_mode(my_param)
online_time_tt.append(time.perf_counter() - online_t0)
K_true = TTK.generate_true_parametric_kernel_mat(my_param)
K_approx = Fsh @ Mh @ Fth
r_compr = min(list(np.shape(Mh)))
t3 = time.perf_counter()
U, S = aca_partial(X, Y, my_tol, p_vec_fun)
time_ACA.append(time.perf_counter() - t3)
# r_compr_2 = U.shape[1]
aca_err = TTK.evaluate_error(K_true, U @ S, 'fro')
errs_ACA.append(aca_err)
my_err_2 = TTK.evaluate_error(K_true, K_approx, 'fro')
errs_tt.append(my_err_2)
dict_tt = {"Kernel": kernel_name,
"Tol": format_e(my_tol),
"Offline Time (s)": format_e(offline_t),
"Storage(MB)": format_e(sto * 8E-6),
"Online Time (s)": format_e(np.mean(online_time_tt)),
"Error": format_e(max(errs_tt))
}
dict_aca = {"Kernel": kernel_name,
"Tol": format_e(my_tol),
"Online Time (s)": format_e(np.mean(time_ACA)),
"Speed Up": np.mean(time_ACA) / np.mean(online_time_tt),
"Error": format_e(max(errs_ACA))}
data_tt.append(dict_tt)
data_aca.append(dict_aca)
eprint("done")
errs_tt = []
errs_ACA = []
time_ACA = []
online_time_tt = []
print("\n" + "-"*40)
print("PTTK".center(40))
print("-"*40)
markdown = markdown_table(data_tt).get_markdown()
print(markdown)
print("\n" + "-"*40)
print("ACA".center(40))
print("-"*40)
markdown = markdown_table(data_aca).get_markdown()
print(markdown)