-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkernels.py
More file actions
43 lines (37 loc) · 1.03 KB
/
kernels.py
File metadata and controls
43 lines (37 loc) · 1.03 KB
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
import numpy as np
from tqdm import tqdm
class LinearKernel:
def __init__(self):
pass
def __call__(self, X, Y=None):
if Y is None:
Y = X
return X @ Y.T
class PolynomialKernel:
def __init__(self, p=3):
self.p = p
def __call__(self, X, Y=None):
if Y is None:
Y = X
return (X @ Y.T + 1) ** self.p
class RBF:
def __init__(self, sigma=1.):
self.sigma = sigma
def __call__(self,X,Y=None):
if Y is None:
Y = X
dists = np.square(X)[:, np.newaxis].sum(axis=2) - 2*X @ Y.T + np.square(Y).sum(axis=1)
return np.exp(-dists/(2*self.sigma**2))
class LaplacianKernel():
def __init__(self, sigma):
self.sigma = sigma
def __call__(self, X, Y=None):
if Y is None:
Y = X
n = X.shape[0]
m = Y.shape[0]
K = np.zeros((n, m))
for i in tqdm(range(m)):
K[:, i] = np.sum(np.abs(X - Y[i, :]), axis=1)
K /= self.sigma ** 2
return np.exp(-K)