def eval(self, X, Y): """Evalute the kernel on data X and Y """ b = self.b c = self.c D2 = util.dist2_matrix(X, Y) K = (c**2 + D2)**b return K
def gradXY_sum(self, X, Y): """ Compute \sum_{i=1}^d \frac{\partial^2 k(X, Y)}{\partial x_i \partial y_i} evaluated at each x_i in X, and y_i in Y. X: nx x d numpy array. Y: ny x d numpy array. Return a nx x ny numpy array of the derivatives. """ b = self.b c = self.c D2 = util.dist2_matrix(X, Y) # d = input dimension d = X.shape[1] c2D2 = c**2 + D2 T1 = -4.0 * b * (b - 1) * D2 * (c2D2**(b - 2)) T2 = -2.0 * b * d * c2D2**(b - 1) return T1 + T2
def gradX_Y(self, X, Y, dim): """ Compute the gradient with respect to the dimension dim of X in k(X, Y). X: nx x d Y: ny x d Return a numpy array of size nx x ny. """ D2 = util.dist2_matrix(X, Y) # 1d array of length nx Xi = X[:, dim] # 1d array of length ny Yi = Y[:, dim] # nx x ny dim_diff = Xi[:, np.newaxis] - Yi[np.newaxis, :] b = self.b c = self.c Gdim = (2.0 * b * (c**2 + D2)**(b - 1)) * dim_diff assert Gdim.shape[0] == X.shape[0] assert Gdim.shape[1] == Y.shape[0] return Gdim