-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernels.py
More file actions
62 lines (54 loc) · 1.99 KB
/
kernels.py
File metadata and controls
62 lines (54 loc) · 1.99 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import tensorflow as tf
import numpy as np
from scipy.spatial.distance import pdist, cdist, squareform
def rbf(X, X2=None, lengthscale=1.0, variance=1.0):
lengthscale = tf.convert_to_tensor(lengthscale, tf.float64)
variance = tf.convert_to_tensor(variance, tf.float64)
X = tf.convert_to_tensor(X)
X = X / lengthscale
Xs = tf.reduce_sum(tf.square(X), 1)
if X2 is None:
X2 = X
X2s = Xs
else:
X2 = tf.convert_to_tensor(X2)
X2 = X2 / lengthscale
X2s = tf.reduce_sum(tf.square(X2), 1)
square = tf.reshape(Xs, [-1, 1]) + tf.reshape(X2s, [1, -1]) - \
2 * tf.matmul(X, X2, transpose_b=True)
output = variance * tf.exp(-square / 2)
return output
def RationalQuadratic(X, X2=None, lengthScale=0.5, alpha=0.1, variance=1.0):
lengthScale = tf.convert_to_tensor(lengthScale, tf.float64)
alpha = tf.convert_to_tensor(alpha, tf.float64)
X = tf.convert_to_tensor(X)
X = X / alpha
X = X / lengthScale
Xs = tf.reduce_sum(tf.square(X), 1)
if X2 is None:
X2 = X
X2s = Xs
else:
X2 = tf.convert_to_tensor(X2)
X2 = X2 / alpha
X2 = X2 / lengthScale
X2s = tf.reduce_sum(tf.square(X2), 1)
square = tf.reshape(Xs, [-1, 1]) + tf.reshape(X2s, [1, -1]) - 2 *\
tf.matmul(X, X2, transpose_b=True)
output = 1 + (square / 2)
K = variance * tf.pow(output, - alpha)
return K
def ExpSineSquared(X, Xs=None, lengthScale=0.5, period=8.0, sigma=1.0):
if Xs is None:
X = X.eval(session=tf.Session())
dists = squareform(pdist(X, metric='euclidean'))
arg = np.pi * dists / period
sin_arg = np.sin(arg)
K = np.exp(- 2 * (sin_arg / lengthScale) ** 2)
else:
X = X.eval(session=tf.Session())
Xs = Xs.eval(session=tf.Session())
dists = cdist(X, Xs, metric='euclidean')
K = np.exp(- 2 * (np.sin(np.pi / period * dists) / lengthScale) ** 2)
K = sigma * K
return tf.convert_to_tensor(K, tf.float64)