-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench.py
46 lines (37 loc) · 1.51 KB
/
bench.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
from pmlb import fetch_data
from memory_profiler import memory_usage
from sklearn import svm
import numpy as np
import time
import sys
import os
X, y = fetch_data("clean2", return_X_y=True, local_cache_dir="./pmlb/")
# Make sure the input data is C-style row contigueous array (row orientated)
X = np.ascontiguousarray(X)
y = np.ascontiguousarray(y)
def run():
linearsvc = svm.LinearSVC(loss="hinge", max_iter=2500, random_state=0)
t = time.time()
if sys.platform.startswith("linux"):
mem_usage = memory_usage((linearsvc.fit, [X, y]), interval=1)
else:
linearsvc.fit(X, y)
time_taken = time.time() - t
print(f"Run took {time_taken} seconds and {linearsvc.n_iter_} iterations")
print("last 10 coefficients: ", linearsvc.coef_[:, -10:])
print("Intercept: ", linearsvc.intercept_)
if sys.platform.startswith("linux"):
print("Max memory usage: ", max(mem_usage))
return linearsvc.coef_, linearsvc.intercept_, time_taken
liblinear_coef, liblinear_intercept, liblinear_time = run()
sys.path.remove(os.getcwd()) # so that python imports from installed site-package
import lisbon # noqa
lisbon_coef, lisbon_intercept, lisbon_time = run()
if not np.allclose(liblinear_coef, lisbon_coef, rtol=0, atol=1e-15) or not np.allclose(
liblinear_intercept, lisbon_intercept, rtol=0, atol=1e-15
):
print("Lisbon result is different from liblinear. There might be an error.")
exit(1)
if lisbon_time > liblinear_time:
print("Lisbon is slower than liblinear.")
exit(1)