-
Notifications
You must be signed in to change notification settings - Fork 2
/
kernels.py
78 lines (62 loc) · 1.68 KB
/
kernels.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
import numpy as np
from keras import backend as K
def D2(X, Y):
""" Calculate the pointwise (squared) distance.
Arguments:
X: of shape (n_sample, n_feature).
Y: of shape (n_center, n_feature).
Returns:
pointwise distances (n_sample, n_center).
"""
XX = K.sum(K.square(X), axis = 1, keepdims=True)
if X is Y:
YY = XX
else:
YY = K.sum(K.square(Y), axis = 1, keepdims=True)
XY = K.dot(X, K.transpose(Y))
d2 = K.reshape(XX, (K.shape(X)[0], 1)) \
+ K.reshape(YY, (1, K.shape(Y)[0])) \
- 2 * XY
return d2
def Gaussian(X, Y, s):
""" Gaussian kernel.
Arguments:
X: of shape (n_sample, n_feature).
Y: of shape (n_center, n_feature).
s: kernel bandwidth.
Returns:
kernel matrix of shape (n_sample, n_center).
"""
assert s > 0
d2 = D2(X, Y)
gamma = np.float32(1. / (2 * s ** 2))
G = K.exp(-gamma * K.clip(d2, 0, None))
return G
def Laplace(X, Y, s):
""" Laplace kernel.
Arguments:
X: of shape (n_sample, n_feature).
Y: of shape (n_center, n_feature).
s: kernel bandwidth.
Returns:
kernel matrix of shape (n_sample, n_center).
"""
assert s > 0
d2 = K.clip(D2(X, Y), 0, None)
d = K.sqrt(d2)
G = K.exp(- d / s)
return G
def Cauchy(X, Y, s):
""" Cauchy kernel.
Arguments:
X: of shape (n_sample, n_feature).
Y: of shape (n_center, n_feature).
s: kernel bandwidth.
Returns:
kernel matrix of shape (n_sample, n_center).
"""
assert s > 0
d2 = D2(X, Y)
s2 = np.float32(s**2)
G = 1 / K.exp( 1 + K.clip(d2, 0, None) / s2)
return G