Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slkernel
Home > sltoolbox > kernel > slkernel.m

slkernel

PURPOSE ^

SLKERNEL Computes the kernel for samples

SYNOPSIS ^

function K = slkernel(varargin)

DESCRIPTION ^

SLKERNEL Computes the kernel for samples

 $ Syntax $
   - K = slkernel(X0, kernel_type, ...)
   - K = slkernel(X0, X, kernel_type, ...)

 $ Description $
   - K = slkernel(X0, kernel_type, ...) Computes the Gram matrix for
     the samples in matrix X0 using the kernel specified by kernel_type. 
     kernel_type can be name of a built-in kernel type, or the name, 
     handle of an user-specified function. The user-specified function
     should take in two d x n matrix containing n pairs of vectors, and
     output a 1 x n vector of their kernel values. The available builtin
     kernels are given as follows:
     \*
     \t   Table 1.  Built-in Kernel Types          \\
     \h     name     &    description              \\
           'lin'     &  Linear Kernel: 
                        x1' * x2, 
                        with no parameters         \\
           'gauss'   &  Gaussian RBF Kernel:
                        exp(- ||x1 - x2||^2 / (2 * sigma^2)), 
                        with one parameter sigma   \\ 
           'poly'    &  Polynomial Kernel:
                        ((x1' * x2) + a)^k,
                        with two parameters: k and a \\
           'sigmoid' &  Sigmoidal Kernel:
                        tanh(k * (x1' * x2) + a),    
                        with two parameters: k and a \\
           'invquad' &  Inverse Quadratic Kernel:
                        1 / sqrt(||x1 - x2||^2 + a), 
                        with one parameter: a        \\
     \* 

   - K = slkernel(X0, X, kernel_type, p1, p2, ...) Computes the empirical
     kernel mapping for samples X with respect to data set X0. Suppose
     there are n0 samples in X0, n samples in X, then K will be an n0 * n
     matrix. Each column in K corresponds to a column in X.

 $ History $
   - Created by Dahua Lin on Jul 13rd, 2005
   - Modified by Dahua Lin on May 2nd, 2005
       - base on sltoolbox v4
       - re-organize the code structure
       - add the ability of user-specified kernel functions.

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slmetric_pw SLMETRIC_PW Compute the metric between column vectors pairwisely
  • slpweval SLPWEVAL Perform pairwise computation
This function is called by:
  • slkernelfea SLKERNELFEA Extracts kernelized mapped features

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function K = slkernel(varargin)
0002 %SLKERNEL Computes the kernel for samples
0003 %
0004 % $ Syntax $
0005 %   - K = slkernel(X0, kernel_type, ...)
0006 %   - K = slkernel(X0, X, kernel_type, ...)
0007 %
0008 % $ Description $
0009 %   - K = slkernel(X0, kernel_type, ...) Computes the Gram matrix for
0010 %     the samples in matrix X0 using the kernel specified by kernel_type.
0011 %     kernel_type can be name of a built-in kernel type, or the name,
0012 %     handle of an user-specified function. The user-specified function
0013 %     should take in two d x n matrix containing n pairs of vectors, and
0014 %     output a 1 x n vector of their kernel values. The available builtin
0015 %     kernels are given as follows:
0016 %     \*
0017 %     \t   Table 1.  Built-in Kernel Types          \\
0018 %     \h     name     &    description              \\
0019 %           'lin'     &  Linear Kernel:
0020 %                        x1' * x2,
0021 %                        with no parameters         \\
0022 %           'gauss'   &  Gaussian RBF Kernel:
0023 %                        exp(- ||x1 - x2||^2 / (2 * sigma^2)),
0024 %                        with one parameter sigma   \\
0025 %           'poly'    &  Polynomial Kernel:
0026 %                        ((x1' * x2) + a)^k,
0027 %                        with two parameters: k and a \\
0028 %           'sigmoid' &  Sigmoidal Kernel:
0029 %                        tanh(k * (x1' * x2) + a),
0030 %                        with two parameters: k and a \\
0031 %           'invquad' &  Inverse Quadratic Kernel:
0032 %                        1 / sqrt(||x1 - x2||^2 + a),
0033 %                        with one parameter: a        \\
0034 %     \*
0035 %
0036 %   - K = slkernel(X0, X, kernel_type, p1, p2, ...) Computes the empirical
0037 %     kernel mapping for samples X with respect to data set X0. Suppose
0038 %     there are n0 samples in X0, n samples in X, then K will be an n0 * n
0039 %     matrix. Each column in K corresponds to a column in X.
0040 %
0041 % $ History $
0042 %   - Created by Dahua Lin on Jul 13rd, 2005
0043 %   - Modified by Dahua Lin on May 2nd, 2005
0044 %       - base on sltoolbox v4
0045 %       - re-organize the code structure
0046 %       - add the ability of user-specified kernel functions.
0047 %
0048 
0049 %% parse and verify input arguments
0050 
0051 % for X0
0052 if ~isnumeric(varargin{1})
0053     error('sltoolbox:invalidarg', ...
0054         'The first argument should be an numeric matrix');
0055 end
0056 X0 = varargin{1};
0057 if ndims(X0) ~= 2
0058     error('sltoolbox:invaliddims', ...
0059         'The X0 should be a 2D matrix');
0060 end
0061 
0062 % for X
0063 if isnumeric(varargin{2})
0064     X = varargin{2};    
0065     if ndims(X) ~= 2
0066         error('sltoolbox:invaliddims', ...
0067             'X should be a 2D matrix');
0068     end
0069     ipkt = 3;       % argument index of kernel type
0070 else
0071     X = X0;
0072     ipkt = 2;        
0073 end
0074 
0075 % for kernel type
0076 if nargin < ipkt || isempty(varargin{ipkt})
0077     error('sltoolbox:invalidarg', ...
0078         'kernel type is not specified');
0079 end
0080 kernel_type = varargin{ipkt};
0081 
0082 % for extra parameters
0083 if nargin == ipkt       % no extra parameters
0084     params = {};
0085 else
0086     params = varargin(ipkt+1:end);
0087 end
0088 
0089 
0090 %% Determine the computation routine and delegate to it
0091 
0092 % determine for the built-in ones
0093 
0094 bik = false;
0095 if ischar(kernel_type)
0096     switch kernel_type
0097         case 'lin'
0098             bik = true;   % bik means built-in kernel
0099             fh_kernel = @lin_kernel;
0100         case 'gauss'
0101             bik = true;
0102             fh_kernel = @gauss_kernel;
0103         case 'poly'
0104             bik = true;
0105             fh_kernel = @poly_kernel;
0106         case 'sigmoid'
0107             bik = true;
0108             fh_kernel = @sigmoid_kernel;
0109         case 'invquad'
0110             bik = true;
0111             fh_kernel = @invquad_kernel;
0112     end
0113 end
0114 
0115 % delegate
0116 if bik
0117     K = fh_kernel(X0, X, params{:});
0118 else
0119     K = slpweval(X0, X, kernel_type, params{:});
0120 end
0121 
0122 
0123 %% The built-in kernel functions
0124 
0125 % Linear Kernel
0126 function K = lin_kernel(X0, X)
0127 
0128 K = X0' * X;
0129 
0130 
0131 % Gaussian Radius Base Function Kernel
0132 function K = gauss_kernel(X0, X, sigma)
0133 
0134 D2 = slmetric_pw(X0, X, 'sqdist');
0135 K = exp(- D2 / (2 * sigma * sigma));
0136 
0137 
0138 % Polynomial Kernel
0139 function K = poly_kernel(X0, X, k, a)
0140 
0141 K = (X0' * X + a).^k;
0142 
0143 
0144 % Sigmoidal Kernel
0145 function K = sigmoid_kernel(X0, X, k, a)
0146 
0147 K = tanh(k * (X0' * X) + a);
0148 
0149 
0150 % Inverse Quadratic Kernel
0151 function K = invquad_kernel(X0, X, a)
0152 
0153 D2 = slmetric_pw(X0, X, 'sqdist');
0154 K = 1 ./ sqrt(D2 + a);
0155 
0156

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com