mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-04-29 06:46:22 +02:00
56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
import numpy as np
|
|
|
|
def linear_grid(D, n = 100, min_max = (-100, 100)):
|
|
"""
|
|
Creates a D-dimensional grid of n linearly spaced points
|
|
|
|
Parameters:
|
|
|
|
D: dimension of the grid
|
|
n: number of points
|
|
min_max: (min, max) list
|
|
|
|
|
|
"""
|
|
|
|
g = np.linspace(min_max[0], min_max[1], n)
|
|
G = np.ones((n, D))
|
|
|
|
return G*g[:,None]
|
|
|
|
def kmm_init(X, m = 10):
|
|
"""
|
|
This is the same initialization algorithm that is used
|
|
in Kmeans++. It's quite simple and very useful to initialize
|
|
the locations of the inducing points in sparse GPs.
|
|
|
|
:param X: data
|
|
:param m: number of inducing points
|
|
"""
|
|
|
|
# compute the distances
|
|
XXT = np.dot(X, X.T)
|
|
D = (-2.*XXT + np.diag(XXT)[:,np.newaxis] + np.diag(XXT)[np.newaxis,:])
|
|
|
|
# select the first point
|
|
s = np.random.permutation(X.shape[0])[0]
|
|
inducing = [s]
|
|
prob = D[s]/D[s].sum()
|
|
|
|
for z in range(m-1):
|
|
s = np.random.multinomial(1, prob.flatten()).argmax()
|
|
inducing.append(s)
|
|
prob = D[s]/D[s].sum()
|
|
|
|
inducing = np.array(inducing)
|
|
return X[inducing]
|
|
|
|
if __name__ == '__main__':
|
|
import pylab as plt
|
|
X = np.linspace(1,10, 100)[:, None]
|
|
X = X[np.random.permutation(X.shape[0])[:20]]
|
|
inducing = kmm_init(X, m = 5)
|
|
plt.figure()
|
|
plt.plot(X.flatten(), np.ones((X.shape[0],)), 'x')
|
|
plt.plot(inducing, 0.5* np.ones((len(inducing),)), 'o')
|
|
plt.ylim((0.0, 10.0))
|