-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
77 lines (61 loc) · 2.3 KB
/
main.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
from element_wise_mat_appro import *
from utils import *
import time
U = load_data("satimage")
print(U.shape)
dim = 50
seed = 1
gamma = 5.0
metric = "rbf"
K = rbf_kernel(U,U,gamma)
#gamma = 0.1
#metric = "csrbf"
#K = construct_csRBF(U,U,gamma)
Anorm2 = ss.linalg.svds(K,k=1,which="LM",return_singular_vectors=False)
#Anorm2 = 1.0
if (metric != "csrbf"):
# RFF
t0 = time.time()
rff = RFF(gamma,c=dim,metric=metric)
Z = rff.transform(U)
print("done RFF kernel in %0.3fs" % (time.time() - t0))
appro_K = Z.dot(Z.T)
LO = func(K-appro_K)
spectral_error = ss.linalg.svds(LO,k=1,which='LM',return_singular_vectors=False)/Anorm2
print("relative approximation error of RFF=%0.10f" % spectral_error)
# Nystrom
t0 = time.time()
nystrom = Nystrom(gamma,c=dim,metric=metric,seed=seed)
L = nystrom.transform(U)
print("done Nystrom in %0.3fs" % (time.time() - t0))
appro_K = L.dot(L.T)
LO = func(K-appro_K)
spectral_error = ss.linalg.svds(LO,k=1,which='LM',return_singular_vectors=False)/Anorm2
print("relative approximation error of Nystrom=%0.10f" % spectral_error)
# FastSPSD
t0 = time.time()
fastspsd = FastSPSD(gamma,c=dim,s=dim*5,metric=metric,seed=seed)
Q,W = fastspsd.transform(U)
print("done FastSPSD kernel in %0.3fs" % (time.time() - t0))
appro_K = Q.dot(W).dot(Q.T)
LO = func(K-appro_K)
spectral_error = ss.linalg.svds(LO,k=1,which='LM',return_singular_vectors=False)/Anorm2
print("relative approximation error of FastSPSD=%0.10f" % spectral_error)
# ssrSVD
t0 = time.time()
ssrsvd = ssrSVD(gamma,d=dim,c=dim,s=dim*5,z=4,metric=metric,shift=False,seed=seed)
Q,s,V = ssrsvd.transform(U)
print("done ssrSVD kernel in %0.3fs" % (time.time() - t0))
appro_K = Q.dot(np.diag(s)).dot(V.T)
LO = func(K-appro_K)
spectral_error = ss.linalg.svds(LO,k=1,which='LM',return_singular_vectors=False)/Anorm2
print("relative approximation error of ssrSVD =%0.10f" % spectral_error)
# shift S3SPSD
t0 = time.time()
s3spsd = S3SPSD(gamma,c=dim,s=dim*5,z=4,metric=metric,shift=True,seed=seed)
Q,X,alpha = s3spsd.transform(U)
print("done S3SPSD kernel in %0.3fs" % (time.time() - t0))
appro_K = Q.dot(X).dot(Q.T)+alpha*np.identity(U.shape[0])
LO = func(K-appro_K)
spectral_error = ss.linalg.svds(LO,k=1,which='LM',return_singular_vectors=False)/Anorm2
print("relative approximation error of S3SPSD=%0.10f" % spectral_error)