-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_bibd.py
144 lines (127 loc) · 4.73 KB
/
test_bibd.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 scipy
from scipy import linalg
import numpy as np
from sliding_windows_frequent_directions import (
FastSlidingWindowFD,
SlidingWindowFD,
OptSwfd,
)
from swfd_dense import SwfdDense
from lmfd import LMFD
from difd import DIFD
from rowsample import SWR, SWOR
from tqdm import trange
import time
import pickle
from pympler.asizeof import asizeof
import argparse
import fastfdwithdump
import frequent_directions
parser = argparse.ArgumentParser()
parser.add_argument("-m")
args = parser.parse_args()
def run():
np.random.seed(0)
A = scipy.io.loadmat("dataset/bibd_22_8.mat")["Problem"][0][0][2].T.todense()
A = A.astype(np.float64)
A = A[:100000, :]
epochs, d = A.shape
N = 10000
R = np.max(np.linalg.norm(A, axis=1) ** 2)
r = np.min(np.linalg.norm(A, axis=1) ** 2)
# print(epochs, d)
# print(R, r)
# exit(0)
ls = [10, 20, 50, 100, 150]
ls = [150]
query_step = epochs // 500
# query_step = epochs + 1
results = {}
method = args.m
print(method)
for l in ls:
with open(f"logs/bibd,{method},l={l},N={N}.txt", "w") as f:
max_error = 0.0
sum_error = 0.0
sum_update_time_ms = 0
sum_query_time_ms = 0
max_error_epoch = 0
query_count = 1
max_size = 0
match method:
case "opt":
swfd = OptSwfd(N, d, l, error_threshold=N * R / l)
case "r1a":
swfd = FastSlidingWindowFD(N, d, l, error_threshold=N * R / l)
case "ours":
swfd = SlidingWindowFD(N, d, l, error_threshold=N * R / l)
case "lmfd":
swfd = LMFD(N, d, l)
case "difd":
swfd = DIFD(N, d, l, R)
case "swr":
swfd = SWR(N, l, d)
case "swor":
swfd = SWOR(N, l, d)
# max_size = asizeof(swfd)
pkl_path = f"logs/bibd,{method},l={l},N={N}.pkl"
for t in trange(epochs, desc=pkl_path):
a = A[t : t + 1, :]
A_w = A[max(0, t + 1 - N) : t + 1]
start_time = time.process_time_ns()
swfd.fit(a)
# max_size = max(max_size, asizeof(swfd))
end_time = time.process_time_ns()
elapsed_time = end_time - start_time
sum_update_time_ms += elapsed_time // (10**6)
if t % query_step == 0:
start_time = time.process_time_ns()
B_t, _, _, _ = swfd.get()
end_time = time.process_time_ns()
elapsed_time = end_time - start_time
sum_query_time_ms += elapsed_time // (10**6)
A_f = linalg.norm(A_w) ** 2
eA_f = A_f / l
A_wB_w = A_w.T @ A_w - B_t.T @ B_t
A_wB_w = linalg.norm(A_wB_w, 2)
if method == "ours":
eA_f *= 4
if eA_f - A_wB_w < 0:
print(
f"l={l}, t={t}, A_wB_w={A_wB_w}, eA_f={eA_f}, error = {eA_f - A_wB_w}"
)
relative_error = A_wB_w / A_f
# max_error = max(max_error, relative_error)
if relative_error > max_error:
max_error = relative_error
max_error_epoch = t
sum_error += relative_error
max_size = max(max_size, swfd.get_size())
query_count += 1
avg_error = sum_error / query_count
avg_update_time = sum_update_time_ms / epochs
avg_query_time = sum_query_time_ms / query_count
results[l] = {
"max_error": max_error,
"max_error_epoch": max_error_epoch,
"avg_error": avg_error,
"avg_update_time": avg_update_time,
"avg_query_time": avg_query_time,
"max_size": max_size,
}
f.write(
f"l={l}, max_error={max_error}, avg_error={avg_error}, avg_update_time={avg_update_time}, avg_query_time={avg_query_time}, max_size={max_size}\n"
)
match method:
case "r1a":
print(fastfdwithdump.SVD_COUNT)
case "ours":
print(
frequent_directions.SVD_COUNT_OURS,
frequent_directions.FLUSH_HIT,
frequent_directions.FLUSH_ENTER,
)
with open(pkl_path, "wb") as f:
pickle.dump(results, f)
if __name__ == "__main__":
run()