示例代码-机器学习中常用的几种核函数

 

机器学习中常用的几种核函数

 

  • 线性核函数:
    k ( x , y ) = x T y k(x,y) = x^Ty k(x,y)=xTy
  • 多项式核函数:
    k ( x , y ) = ( x T y + 1 ) d k(x,y) = (x^Ty+1)^d k(x,y)=(xTy+1)d
  • 指数核函数:
    k ( x , y ) = e − ∥ x − y ∥ 2 δ 2 k(x,y) = e^{-\frac{\parallel x-y\parallel }{2 \delta^2}} k(x,y)=e2δ2xy
  • 高斯核函数:
    k ( x , y ) = e − ∥ x − y ∥ 2 2 δ 2 k(x,y) = e^{-\frac{\parallel x-y\parallel ^2 }{2 \delta^2}} k(x,y)=e2δ2xy2

 
常用核函数示例代码(下载链接:[Link]):
 

%% compute kernel matrix between two feature matrices with Gaussian, Exponential, Polynomial kernel ,etc.
%  Written by Kai-Xuan Chen (chenkx.jsh@aliyun.com). If you find any bugs, please contact me.
% 
%  input:
%       fea_a,fea_b : columns of vectors of data points. 
%       options     : Struct value in Matlab.
%               options.type_kernel  -  Choices are:
%                   'Gau'      - Gaussian kernel:      e^{-(|x-y|^2)/2t^2}   
%                   'Exp'      - Exponential kernel:   e^{-(|x-y|)/2t^2}  
%                   'Pol'      - Polynomial kernel:    (x'*y+1)^d    
%                   'Lin'      - Linear kernel:        x'*y    
%
%                options.t       -  parameter for Gaussian, Exponential
%                options.d       -  parameter for Poly
% output:
%       kernel_matrix: kernel matrix between two feature matrices
% 
% 
% If you find this code useful for your research, we appreciate it very much if you can cite our related works:
% 1.
% Chen K X, Wu X J, Ren J Y, et al. More About Covariance Descriptors for Image Set Coding: Log-Euclidean Framework based Kernel Matrix  Representation[C] 
% //Proceedings of the IEEE International Conference on Computer Vision Workshops. 2019: 0-0.
% 2.
% Chen K X, Wu X J, Wang R, et al. Riemannian kernel based Nystr?m method for approximate infinite-dimensional covariance descriptors with application to image set classification[C] 
% //2018 24th International conference on pattern recognition (ICPR). IEEE, 2018: 651-656.
%



function kernel_matrix = compute_kernelMatrix(fea_a, fea_b, options)

    if (~exist('options','var'))
       options = [];
    else
       if ~isstruct(options) 
           error('parameter error!');
       end
    end
    if ~isfield(options,'type_kernel')
        options.type_kernel = 'Gau';
    end
    

    switch options.type_kernel
        case 'Gau' 
%           Gaussian kernel:      e^{-(|x-y|^2)/2t^2}            
            if ~isfield(options,'t')
                options.t = 0.2;
            end
            dis_matrix = compute_eudDist(fea_a, fea_b);    
            K = exp(-dis_matrix.^2/(2*options.t^2));           
  
        case 'Exp'        
%           Exponential kernel:   e^{-(|x-y|)/2t^2}  
            if ~isfield(options,'t')
                options.t = 0.2;
            end
            dis_matrix = compute_eudDist(fea_a, fea_b);
            K = exp(-dis_matrix/(2*options.t^2));            

        case 'Pol'     
%           Polynomial kernel:    (x'*y+1)^d    
            if ~isfield(options,'d')
                options.t = 2;
            end
            inner_matrix = fea_a'*fea_b;
            K = (inner_matrix+1).^options.d;          

        case 'Lin'     
%           Linear kernel:        x'*y    
            inner_matrix = fea_a'*fea_b;
            K = inner_matrix;
                      
        otherwise
            error('KernelType does not exist!');
    end
    kernel_matrix = K;
end


function dis_matrix = compute_eudDist(fea_a, fea_b)
    
    aa = sum(fea_a.*fea_a, 1);
    bb = sum(fea_b.*fea_b, 1);
    ab = fea_a'*fea_b;
    temp_d = bsxfun(@plus,aa',bb) - 2*ab;
    temp_d(temp_d<0) = 0;
    dis_matrix = sqrt(temp_d);
    if isequal(fea_a, fea_b)
        dis_matrix = max(dis_matrix, dis_matrix');
    end

end

 

  

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值