def pair_eval(self, X, Y): """Evaluate k(x1, y1), k(x2, y2), ...""" sigma2s = self.sigma2s Xs = old_div(X, np.sqrt(sigma2s)) Ys = old_div(Y, np.sqrt(sigma2s)) k = KGauss(1.0) return k.pair_eval(Xs, Ys)
def eval(self, X, Y): """ Equivalent to dividing each dimension with the corresponding width (not width^2) and using the standard Gaussian kernel. """ sigma2s = self.sigma2s Xs = old_div(X, np.sqrt(sigma2s)) Ys = old_div(Y, np.sqrt(sigma2s)) k = KGauss(1.0) return k.eval(Xs, Ys)
def pair_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: n x d numpy array. Y: n x d numpy array. Return a one-dimensional length-n numpy array of the derivatives. """ d = X.shape[1] sigma2 = self.sigma2 D2 = np.sum((X - Y)**2, 1) Kvec = np.exp(old_div(-D2, (2.0 * self.sigma2))) G = Kvec / sigma2 * (d - old_div(D2, sigma2)) return G
def gradXY_sum(self, X, Y): r""" 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. """ (n1, d1) = X.shape (n2, d2) = Y.shape assert d1==d2, 'Dimensions of the two inputs must be the same' d = d1 sigma2 = self.sigma2 D2 = np.sum(X**2, 1)[:, np.newaxis] - 2*np.dot(X, Y.T) + np.sum(Y**2, 1) K = np.exp(old_div(-D2,(2.0*sigma2))) G = K/sigma2*(d - old_div(D2,sigma2)) return G
def pair_eval(self, X, Y): """ Evaluate k(x1, y1), k(x2, y2), ... Parameters ---------- X, Y : n x d numpy array Return ------- a numpy array with length n """ (n1, d1) = X.shape (n2, d2) = Y.shape assert n1 == n2, 'Two inputs must have the same number of instances' assert d1 == d2, 'Two inputs must have the same dimension' D2 = np.sum((X - Y)**2, 1) Kvec = np.exp(old_div(-D2, (2.0 * self.sigma2))) return Kvec
def eval(self, X, Y): """ Evaluate the Gaussian kernel on the two 2d numpy arrays. Parameters ---------- X : n1 x d numpy array Y : n2 x d numpy array Return ------ K : a n1 x n2 Gram matrix. """ #(n1, d1) = X.shape #(n2, d2) = Y.shape #assert d1==d2, 'Dimensions of the two inputs must be the same' sumx2 = np.reshape(np.sum(X**2, 1), (-1, 1)) sumy2 = np.reshape(np.sum(Y**2, 1), (1, -1)) D2 = sumx2 - 2 * np.dot(X, Y.T) + sumy2 K = np.exp(old_div(-D2, (2.0 * self.sigma2))) return K