SVM三种寻优方法matlab代码 grid search、GA、PSO

文章转自:http://www.matlabsky.com/thread-12414-1-1.html

文章的转载主要用于学习,并无其他商业用途
在这里使用启发式算法PSO来进行参数寻优,用网格划分(grid search)来寻找最佳的参数c和g,虽然采用网格搜索能够找到在CV意义下的最高的分类准确率,即全局最优解,但有时候如果想在更大的范围内寻找最佳的参数c和g会很费时,采用启发式算法就可以不必遍历网格内的所有的参数点,也能找到全局最优解。


关于粒子群优化算法这里不打算过多介绍,想要 学习的朋友可以自己查看相关资料。


使用PSO来进行参数寻优在在libsvm-mat-2.89-3[FarutoUltimate3.0]工具箱中已经实现psoSVMcgForClass.m(分类问题参数寻优)、psoSVMcgForRegress.m(回归问题参数寻优)。


函数使用接口:
  1. 利用PSO参数寻优函数(分类问题):psoSVMcgForClass
  2. [bestCVaccuracy,bestc,bestg,pso_option]= 
  3. psoSVMcgForClass(train_label,train,pso_option)
  4. 输入:
  5. train_label:训练集的标签,格式要求与svmtrain相同。
  6. train:训练集,格式要求与svmtrain相同。
  7. pso_option:PSO中的一些参数设置,可不输入,有默认值,详细请看代码的帮助说明。
  8. 输出:
  9. bestCVaccuracy:最终CV意义下的最佳分类准确率。
  10. bestc:最佳的参数c。
  11. bestg:最佳的参数g。
  12. pso_option:记录PSO中的一些参数。
  13. ==========================================================
  14. 利用PSO参数寻优函数(回归问题):psoSVMcgForRegress
  15. [bestCVmse,bestc,bestg,pso_option]= 
  16. psoSVMcgForRegress(train_label,train,pso_option)
  17. 其输入输出与psoSVMcgForClass类似,这里不再赘述。
复制 代码
psoSVMcgForClass源代码
  1. function [bestCVaccuarcy,bestc,bestg,pso_option] = psoSVMcgForClass(train_label,train,pso_option)
  2. % psoSVMcgForClass

  3. %%
  4. % by faruto
  5. %Email:[email protected] QQ:516667408 http://blog.sina.com.cn/faruto BNU
  6. %last modified 2010.01.17

  7. %% 若转载请注明:
  8. % faruto and liyang , LIBSVM-farutoUltimateVersion 
  9. % a toolbox with implements for support vector machines based on libsvm, 2009. 

  10. % Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for
  11. % support vector machines, 2001. Software available at
  12. % http://www.csie.ntu.edu.tw/~cjlin/libsvm
  13. %% 参数初始化
  14. if nargin == 2
  15.     pso_option = struct('c1',1.5,'c2',1.7,'maxgen',200,'sizepop',20, ...
  16.         'k',0.6,'wV',1,'wP',1,'v',5, ...
  17.         'popcmax',10^2,'popcmin',10^(-1),'popgmax',10^3,'popgmin',10^(-2));
  18. end
  19. % c1:初始为1.5,pso参数局部搜索能力
  20. % c2:初始为1.7,pso参数全局搜索能力
  21. % maxgen:初始为200,最大进化数量
  22. % sizepop:初始为20,种群最大数量
  23. % k:初始为0.6(k belongs to [0.1,1.0]),速率和x的关系(V = kX)
  24. % wV:初始为1(wV best belongs to [0.8,1.2]),速率更新公式中速度前面的弹性系数
  25. % wP:初始为1,种群更新公式中速度前面的弹性系数
  26. % v:初始为3,SVM Cross Validation参数
  27. % popcmax:初始为100,SVM 参数c的变化的最大值.
  28. % popcmin:初始为0.1,SVM 参数c的变化的最小值.
  29. % popgmax:初始为1000,SVM 参数g的变化的最大值.
  30. % popgmin:初始为0.01,SVM 参数c的变化的最小值.

  31. Vcmax = pso_option.k*pso_option.popcmax;
  32. Vcmin = -Vcmax ;
  33. Vgmax = pso_option.k*pso_option.popgmax;
  34. Vgmin = -Vgmax ;

  35. eps = 10^(-3);

  36. %% 产生初始粒子和速度
  37. for i=1:pso_option.sizepop
  38.     
  39.     % 随机产生种群和速度
  40.     pop(i,1) = (pso_option.popcmax-pso_option.popcmin)*rand+pso_option.popcmin;
  41.     pop(i,2) = (pso_option.popgmax-pso_option.popgmin)*rand+pso_option.popgmin;
  42.     V(i,1)=Vcmax*rands(1,1);
  43.     V(i,2)=Vgmax*rands(1,1);
  44.     
  45.     % 计算初始适应度
  46.     cmd = ['-v ',num2str(pso_option.v),' -c ',num2str( pop(i,1) ),' -g ',num2str( pop(i,2) )];
  47.     fitness(i) = svmtrain(train_label, train, cmd);
  48.     fitness(i) = -fitness(i);
  49. end

  50. % 找极值和极值点
  51. [global_fitness bestindex]=min(fitness); % 全局极值
  52. local_fitness=fitness;   % 个体极值初始化

  53. global_x=pop(bestindex,:);   % 全局极值点
  54. local_x=pop;    % 个体极值点初始化

  55. % 每一代种群的平均适应度
  56. avgfitness_gen = zeros(1,pso_option.maxgen);

  57. %% 迭代寻优
  58. for i=1:pso_option.maxgen
  59.     
  60.     for j=1:pso_option.sizepop
  61.         
  62.         %速度更新
  63.         V(j,:) = pso_option.wV*V(j,:) + pso_option.c1*rand*(local_x(j,:) - pop(j,:)) + pso_option.c2*rand*(global_x - pop(j,:));
  64.         if V(j,1) > Vcmax
  65.             V(j,1) = Vcmax;
  66.         end
  67.         if V(j,1) < Vcmin
  68.             V(j,1) = Vcmin;
  69.         end
  70.         if V(j,2) > Vgmax
  71.             V(j,2) = Vgmax;
  72.         end
  73.         if V(j,2) < Vgmin
  74.             V(j,2) = Vgmin;
  75.         end
  76.         
  77.         %种群更新
  78.         pop(j,:)=pop(j,:) + pso_option.wP*V(j,:);
  79.         if pop(j,1) > pso_option.popcmax
  80.             pop(j,1) = pso_option.popcmax;
  81.         end
  82.         if pop(j,1) < pso_option.popcmin
  83.             pop(j,1) = pso_option.popcmin;
  84.         end
  85.         if pop(j,2) > pso_option.popgmax
  86.             pop(j,2) = pso_option.popgmax;
  87.         end
  88.         if pop(j,2) < pso_option.popgmin
  89.             pop(j,2) = pso_option.popgmin;
  90.         end
  91.         
  92.         % 自适应粒子变异
  93.         if rand>0.5
  94.             k=ceil(2*rand);
  95.             if k == 1
  96.                 pop(j,k) = (20-1)*rand+1;
  97.             end
  98.             if k == 2
  99.                 pop(j,k) = (pso_option.popgmax-pso_option.popgmin)*rand + pso_option.popgmin;
  100.             end
  101.         end
  102.         
  103.         %适应度值
  104.         cmd = ['-v ',num2str(pso_option.v),' -c ',num2str( pop(j,1) ),' -g ',num2str( pop(j,2) )];
  105.         fitness(j) = svmtrain(train_label, train, cmd);
  106.         fitness(j) = -fitness(j);
  107.         
  108.         cmd_temp = ['-c ',num2str( pop(j,1) ),' -g ',num2str( pop(j,2) )];
  109.         model = svmtrain(train_label, train, cmd_temp);
  110.         
  111.         if fitness(j) >= -65
  112.             continue;
  113.         end
  114.         
  115.         %个体最优更新
  116.         if fitness(j) < local_fitness(j)
  117.             local_x(j,:) = pop(j,:);
  118.             local_fitness(j) = fitness(j);
  119.         end
  120.         
  121.         if abs( fitness(j)-local_fitness(j) )<=eps && pop(j,1) < local_x(j,1)
  122.             local_x(j,:) = pop(j,:);
  123.             local_fitness(j) = fitness(j);
  124.         end
  125.         
  126.         %群体最优更新
  127.         if fitness(j) < global_fitness
  128.             global_x = pop(j,:);
  129.             global_fitness = fitness(j);
  130.         end
  131.         
  132.         if abs( fitness(j)-global_fitness )<=eps && pop(j,1) < global_x(1)
  133.             global_x = pop(j,:);
  134.             global_fitness = fitness(j);
  135.         end
  136.         
  137.     end
  138.     
  139.     fit_gen(i) = global_fitness;
  140.     avgfitness_gen(i) = sum(fitness)/pso_option.sizepop;
  141. end

  142. %% 结果分析
  143. figure;
  144. hold on;
  145. plot(-fit_gen,'r*-','LineWidth',1.5);
  146. plot(-avgfitness_gen,'o-','LineWidth',1.5);
  147. legend('最佳适应度','平均适应度',3);
  148. xlabel('进化代数','FontSize',12);
  149. ylabel('适应度','FontSize',12);
  150. grid on;

  151. % print -dtiff -r600 pso

  152. bestc = global_x(1);
  153. bestg = global_x(2);
  154. bestCVaccuarcy = -fit_gen(pso_option.maxgen);

  155. line1 = '适应度曲线Accuracy[PSOmethod]';
  156. line2 = ['(参数c1=',num2str(pso_option.c1), ...
  157.     ',c2=',num2str(pso_option.c2),',终止代数=', ...
  158.     num2str(pso_option.maxgen),',种群数量pop=', ...
  159.     num2str(pso_option.sizepop),')'];
  160. % line3 = ['Best c=',num2str(bestc),' g=',num2str(bestg), ...
  161. %     ' CVAccuracy=',num2str(bestCVaccuarcy),'%'];
  162. % title({line1;line2;line3},'FontSize',12);
  163. title({line1;line2},'FontSize',12);

猜你喜欢

转载自blog.csdn.net/good_learner_1/article/details/88972207