2013-07-17 2 views

ответ

5

Это должно быть то, что вы ищете. Он взят из here

% With Fast Computation of the RBF kernel matrix 
% To speed up the computation, we exploit a decomposition of the Euclidean distance (norm) 
% 
% Inputs: 
%  ker: 'lin','poly','rbf','sam' 
%  X:  data matrix with training samples in rows and features in columns 
%  X2:  data matrix with test samples in rows and features in columns 
%  sigma: width of the RBF kernel 
%  b:  bias in the linear and polinomial kernel 
%  d:  degree in the polynomial kernel 
% 
% Output: 
%  K: kernel matrix 
% 
% Gustavo Camps-Valls 
% 2006(c) 
% Jordi ([email protected]), 2007 
% 2007-11: if/then -> switch, and fixed RBF kernel 

function K = kernelmatrix(ker,X,X2,sigma) 

switch ker 
    case 'lin' 
     if exist('X2','var') 
      K = X' * X2; 
     else 
      K = X' * X; 
     end 

    case 'poly' 
     if exist('X2','var') 
      K = (X' * X2 + b).^d; 
     else 
      K = (X' * X + b).^d; 
     end 

    case 'rbf' 

     n1sq = sum(X.^2,1); 
     n1 = size(X,2); 

     if isempty(X2); 
      D = (ones(n1,1)*n1sq)' + ones(n1,1)*n1sq -2*X'*X; 
     else 
      n2sq = sum(X2.^2,1); 
      n2 = size(X2,2); 
      D = (ones(n2,1)*n1sq)' + ones(n1,1)*n2sq -2*X'*X2; 
     end; 
     K = exp(-D/(2*sigma^2)); 

    case 'sam' 
     if exist('X2','var'); 
      D = X'*X2; 
     else 
      D = X'*X; 
     end 
     K = exp(-acos(D).^2/(2*sigma^2)); 

    otherwise 
     error(['Unsupported kernel ' ker]) 
end 
1

Для получения/матрицы ядра Grahm (матрица внутренних продуктов) сделать:

function [ Kern ] = produce_kernel_matrix(X, t, beta) 
% 
X = X'; 
t = t'; 
X_T_2 = sum(X.^2,2) + sum(t.^2,2).' - (2*X)*t.'; % ||x||^2 + ||t||^2 - 2<x,t> 
Kern =exp(-beta*X_T_2); % 
end 

затем сделать интерполяцию сделать:

function [ mdl ] = learn_RBF_linear_algebra(X_training_data, Y_training_data, mdl) 
% 
Kern_matrix = produce_kernel_matrix_bsxfun(X_training_data, mdl.t, mdl.beta); % (N x K) 
C = Kern_matrix \ Y_training_data'; % (K x D) = (N x K)' x (N x D) 
mdl.c = C; % (K x D) 
end 

ноты бета is 1/2sigma

Смежные вопросы