利用PSO优化SVM
- 格式:doc
- 大小:16.50 KB
- 文档页数:4
1.利用PSO参数寻优函数(分类问题):psoSVMcgForClass2.[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参数寻优函数(回归问题):psoSVMcgForRegress15.[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.% psoSVMcgForClass3.4.%%5.% by faruto6.%Email:patrick.lee@ QQ:516667408/faruto BNU7.%last modified 2010.01.178.9.%% 若转载请注明:10.% faruto and liyang , LIBSVM-farutoUltimateVersion11.% a toolbox with implements for support vector machines based on libsvm,2009.12.%13.% Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for14.% support vector machines, 2001. Software available at15.% .tw/~cjlin/libsvm16.%% 参数初始化17.if nargin == 218. pso_option =struct('c1',1.5,'c2',1.7,'maxgen',200,'sizepop',20, ...19. 'k',0.6,'wV',1,'wP',1,'v',5, ...20. 'popcmax',10^2,'popcmin',10^(-1),'popgmax',10^3,'popgmin',10^(-2));21.end22.% c1:初始为1.5,pso参数局部搜索能力23.% c2:初始为1.7,pso参数全局搜索能力24.% maxgen:初始为200,最大进化数量25.% sizepop:初始为20,种群最大数量26.% k:初始为0.6(k belongs to [0.1,1.0]),速率和x的关系(V = kX)27.% wV:初始为1(wV best belongs to [0.8,1.2]),速率更新公式中速度前面的弹性系数28.% wP:初始为1,种群更新公式中速度前面的弹性系数29.% v:初始为3,SVM Cross Validation参数30.% popcmax:初始为100,SVM 参数c的变化的最大值.31.% popcmin:初始为0.1,SVM 参数c的变化的最小值.32.% popgmax:初始为1000,SVM 参数g的变化的最大值.33.% popgmin:初始为0.01,SVM 参数c的变化的最小值.34.35.Vcmax = pso_option.k*pso_option.popcmax;36.Vcmin = -Vcmax ;37.Vgmax = pso_option.k*pso_option.popgmax;38.Vgmin = -Vgmax ;39.40.eps = 10^(-3);41.42.%% 产生初始粒子和速度43.for i=1:pso_option.sizepop44.45. % 随机产生种群和速度46. pop(i,1) =(pso_option.popcmax-pso_option.popcmin)*rand+pso_option.popcmin; 47. pop(i,2) =(pso_option.popgmax-pso_option.popgmin)*rand+pso_option.popgmin;48. V(i,1)=Vcmax*rands(1,1);49. V(i,2)=Vgmax*rands(1,1);50.51. % 计算初始适应度52. cmd = ['-v ',num2str(pso_option.v),' -c ',num2str( pop(i,1) ),' -g',num2str( pop(i,2) )];53. fitness(i) = svmtrain(train_label, train, cmd);54. fitness(i) = -fitness(i);55.end56.57.% 找极值和极值点58.[global_fitness bestindex]=min(fitness); % 全局极值59.local_fitness=fitness; % 个体极值初始化60.61.global_x=pop(bestindex,:); % 全局极值点62.local_x=pop; % 个体极值点初始化63.64.% 每一代种群的平均适应度65.avgfitness_gen = zeros(1,pso_option.maxgen);66.67.%% 迭代寻优68.for i=1:pso_option.maxgen69.70. for j=1:pso_option.sizepop71.72. %速度更新73. V(j,:) = pso_option.wV*V(j,:) +pso_option.c1*rand*(local_x(j,:) - pop(j,:)) +pso_option.c2*rand*(global_x - pop(j,:));74. if V(j,1) > Vcmax75. V(j,1) = Vcmax;76. end77. if V(j,1) < Vcmin78. V(j,1) = Vcmin;79. end80. if V(j,2) > Vgmax81. V(j,2) = Vgmax;82. end83. if V(j,2) < Vgmin84. V(j,2) = Vgmin;85. end86.87. %种群更新88. pop(j,:)=pop(j,:) + pso_option.wP*V(j,:);89. if pop(j,1) > pso_option.popcmax90. pop(j,1) = pso_option.popcmax;91. end92. if pop(j,1) < pso_option.popcmin93. pop(j,1) = pso_option.popcmin;94. end95. if pop(j,2) > pso_option.popgmax96. pop(j,2) = pso_option.popgmax;97. end98. if pop(j,2) < pso_option.popgmin99. pop(j,2) = pso_option.popgmin;100. end102. % 自适应粒子变异103. if rand>0.5104. k=ceil(2*rand);105. if k == 1106. pop(j,k) = (20-1)*rand+1;107. end108. if k == 2109. pop(j,k) =(pso_option.popgmax-pso_option.popgmin)*rand + pso_option.popgmin; 110. end111. end112.113. %适应度值114. cmd = ['-v ',num2str(pso_option.v),' -c',num2str( pop(j,1) ),' -g ',num2str( pop(j,2) )];115. fitness(j) = svmtrain(train_label, train, cmd);116. fitness(j) = -fitness(j);117.118. cmd_temp = ['-c ',num2str( pop(j,1) ),' -g ',num2str( pop(j,2) )];119. model = svmtrain(train_label, train, cmd_temp);120.121. if fitness(j) >= -65122. continue;123. end124.125. %个体最优更新126. if fitness(j) < local_fitness(j)127. local_x(j,:) = pop(j,:);128. local_fitness(j) = fitness(j);129. end130.131. if abs( fitness(j)-local_fitness(j) )<=eps && pop(j,1) < local_x(j,1)132. local_x(j,:) = pop(j,:);133. local_fitness(j) = fitness(j);134. end135.136. %群体最优更新137. if fitness(j) < global_fitness138. global_x = pop(j,:);139. global_fitness = fitness(j);140. end142. if abs( fitness(j)-global_fitness )<=eps && pop(j,1) < global_x(1)143. global_x = pop(j,:);144. global_fitness = fitness(j);145. end146.147. end148.149. fit_gen(i) = global_fitness;150. avgfitness_gen(i) = sum(fitness)/pso_option.sizepop;151.end152.153.%% 结果分析154.figure;155.hold on;156.plot(-fit_gen,'r*-','LineWidth',1.5);157.plot(-avgfitness_gen,'o-','LineWidth',1.5);158.legend('最佳适应度','平均适应度',3);159.xlabel('进化代数','FontSize',12);160.ylabel('适应度','FontSize',12);161.grid on;162.163.% print -dtiff -r600 pso164.165.bestc = global_x(1);166.bestg = global_x(2);167.bestCVaccuarcy = -fit_gen(pso_option.maxgen);168.169.line1 = '适应度曲线Accuracy[PSOmethod]';170.line2 = ['(参数c1=',num2str(pso_option.c1), ...171. ',c2=',num2str(pso_option.c2),',终止代数=', ...172. num2str(pso_option.maxgen),',种群数量pop=', ...173. num2str(pso_option.sizepop),')'];174.% line3 = ['Best c=',num2str(bestc),' g=',num2str(bestg), ... 175.% ' CVAccuracy=',num2str(bestCVaccuarcy),'%'];176.% title({line1;line2;line3},'FontSize',12);177.title({line1;line2},'FontSize',12);。
%% 清空环境clcclearload wine;train = [wine(1:30,:);wine(60:95,:);wine(131:153,:)];train_label = [wine_labels(1:30);wine_labels(60:95);wine_labels(131:153)];test = [wine(31:59,:);wine(96:130,:);wine(154:178,:)];test_label = [wine_labels(31:59);wine_labels(96:130);wine_labels(154:178)];[train,pstrain] = mapminmax(train');pstrain.ymin = 0;pstrain.ymax = 1;[train,pstrain] = mapminmax(train,pstrain);[test,pstest] = mapminmax(test');pstest.ymin = 0;pstest.ymax = 1;[test,pstest] = mapminmax(test,pstest);train = train';test = test';%% 参数初始化%粒子群算法中的两个参数c1 = 1.6; % c1 belongs to [0,2]c2 = 1.5; % c2 belongs to [0,2]maxgen=300; % 进化次数sizepop=30; % 种群规模popcmax=10^(2);popcmin=10^(-1);popgmax=10^(3);popgmin=10^(-2);k = 0.6; % k belongs to [0.1,1.0];Vcmax = k*popcmax;Vcmin = -Vcmax ;Vgmax = k*popgmax;Vgmin = -Vgmax ;% SVM参数初始化v = 3;%% 产生初始粒子和速度for i=1:sizepop% 随机产生种群pop(i,1) = (popcmax-popcmin)*rand+popcmin; % 初始种群pop(i,2) = (popgmax-popgmin)*rand+popgmin;V(i,1)=Vcmax*rands(1); % 初始化速度V(i,2)=Vgmax*rands(1);% 计算初始适应度cmd = ['-v ',num2str(v),' -c ',num2str( pop(i,1) ),' -g ',num2str( pop(i,2) )];fitness(i) = svm train(train_label, train, cmd);fitness(i) = -fitness(i);end% 找极值和极值点[global_fitness bestindex]=min(fitness); % 全局极值local_fitness=fitness; % 个体极值初始化global_x=pop(bestindex,:); % 全局极值点local_x=pop; % 个体极值点初始化tic%% 迭代寻优for i=1:maxgenfor j=1:sizepop%速度更新wV = 0.9; % wV best belongs to [0.8,1.2]V(j,:) = wV*V(j,:) + c1*rand*(local_x(j,:) - pop(j,:)) + c2*rand*(global_x - pop(j,:));if V(j,1) > VcmaxV(j,1) = Vcmax;endif V(j,1) < VcminV(j,1) = Vcmin;endif V(j,2) > VgmaxV(j,2) = Vgmax;endif V(j,2) < VgminV(j,2) = Vgmin;end%种群更新wP = 0.6;pop(j,:)=pop(j,:)+wP*V(j,:);if pop(j,1) > popcmaxpop(j,1) = popcmax;endif pop(j,1) < popcminpop(j,1) = popcmin;endif pop(j,2) > popgmaxpop(j,2) = popgmax;endif pop(j,2) < popgminpop(j,2) = popgmin;end% 自适应粒子变异if rand>0.5k=ceil(2*rand);if k == 1pop(j,k) = (20-1)*rand+1;endif k == 2pop(j,k) = (popgmax-popgmin)*rand+popgmin;endend%适应度值cmd = ['-v ',num2str(v),' -c ',num2str( pop(j,1) ),' -g ',num2str( pop(j,2) )];fitness(j) = svmtrain(train_label, train, cmd);fitness(j) = -fitness(j);end%个体最优更新if fitness(j) < local_fitness(j)local_x(j,:) = pop(j,:);local_fitness(j) = fitness(j);end%群体最优更新if fitness(j) < global_fitnessglobal_x = pop(j,:);global_fitness = fitness(j);endfit_gen(i)=global_fitness;endtoc%% 结果分析plot(-fit_gen,'LineWidth',5);title(['适应度曲线','(参数c1=',num2str(c1),',c2=',num2str(c2),',终止代数=',num2str(maxgen),')'],'FontSize',13);xlabel('进化代数');ylabel('适应度');bestc = global_x(1)bestg = global_x(2)bestCVaccuarcy = -fit_gen(maxgen)cmd = ['-c ',num2str( bestc ),' -g ',num2str( bestg )];model = svmtrain(train_label,train,cmd);[trainpre,trainacc] = svmpredict(train_label,train,model);trainacc[testpre,testacc] = svmpredict(test_label,test,model);testacc。
W el di ng T echno l ogy V01.42N o,10O ct,2013焊接质量控制与管理57文章编号:1002—025X(2013)10--0057—04基于PSO—SV M的焊缝缺陷X射线检测蔡晓龙1,穆向阳1,高炜欣1,魏巍2(1.西安石油大学陕西省钻机控制重点实验室,陕西西安710065;2.西安西北石油管道公司,陕西西安710018)摘要:为了提高X射线焊缝缺陷检测的实时性,本文研究了将粒子群优化(Par t i cl e Sw ar m O p t i m i zat i on,PSO)算法与支持向量机(Su ppo r t V ect or M achi ne,SV M)算法相结合应用于焊管焊缝缺陷检测的方法。
该方法首先提取焊缝缺陷的特征描述,然后利用SV M算法进行焊缝缺陷的检测,过程中采用PSO算法优化SV M模型参数,最后将PSO—SV M和基于网格寻优的SV M分类方法进行了对比。
试验结果表明,基于PSO—SV M的焊缝缺陷检测方法具有较高的识别精度,平均分类准确率达98%,且相对于后者其实时性提高了39.87%,这表明PS0一SV M方法能够有效地应用于焊管焊缝缺陷检测实时性的提高。
关键词:粒子群优化:模型参数;缺陷检测;支持向量机中图分类号:T G441.7文献标志码:B0引言随着计算机和数字图像处理技术的发展,x射线焊缝缺陷检测技术以其直观可靠、数字化精度高等特点被广泛应用于石油焊管和压力容器制造等重要行业的无损检测领域中f1]。
该方法主要针对检测图像进行现代图像处理、被检对象的特征参数提取、分类器训练建模以及分类决策几个部分。
近年来。
支持向量机(SV M)算法在小样本、非线性和高维数建模中表现出了许多特有的优势,被广泛应用于识别系统的分类器建模中。
康维新等人构建了二层一对一SV M多分类器模型,该方法对小样本测试环境的适应能力强,并有较好的分类准确率[2]。
基于PSO算法的SVM参数优化方法研究基于粒子群优化算法(Particle Swarm Optimization, PSO)的支持向量机(Support Vector Machine, SVM)参数优化是近年来机器学习领域中的热门研究方向。
本文将探讨PSO算法在SVM参数优化中的应用,并介绍其原理和优势。
首先,我们需要介绍一下支持向量机(SVM)。
SVM是一种常用的监督学习算法,可用于分类和回归问题。
其核心思想是在特征空间中找到一个最优的超平面来使不同类别的样本尽可能地分开。
SVM参数优化包括核函数选择、惩罚参数(C)以及其他控制参数的选择。
然而,SVM参数优化是一个复杂的优化问题,传统方法通常需要进行大量的计算和试验。
为了降低计算复杂度,提高参数优化效率,近年来研究者开始引入PSO算法来求解SVM参数优化问题。
PSO算法是一种启发式优化算法,模拟了鸟群捕食的行为。
在PSO算法中,每个解(粒子)都有一个速度和位置,并与其他粒子共享信息。
通过不断更新速度和位置,粒子会向全局最优解靠近。
在使用PSO算法进行SVM参数优化时,需要将SVM参数作为优化目标函数的参数。
PSO算法通过不断更新粒子的速度和位置来优化SVM参数,使得SVM模型在训练集上的性能最优。
具体而言,PSO算法的每个粒子可以看作是一个SVM的参数组合,包括核函数选择、惩罚参数(C)等。
每个粒子通过评估其对应的SVM模型在训练集上的性能来计算适应度值。
然后,粒子根据自己的当前最优位置和全局最优位置来更新速度和位置,以期望找到更好的解。
PSO算法有以下几个优势适合用于SVM参数优化。
首先,PSO算法具有全局能力,能够在参数空间中找到最优解。
其次,PSO算法不依赖于问题的具体形式,适用于各种类型的SVM参数优化。
而且,PSO算法不需要计算梯度,因此能够避免陷入局部最优解。
目前,PSO算法在SVM参数优化中得到了广泛的应用,并取得了较好的结果。
%% 清空环境
clc
clear
load wine;
train = [wine(1:30,:);wine(60:95,:);wine(131:153,:)];
train_label = [wine_labels(1:30);wine_labels(60:95);wine_labels(131:153)];
test = [wine(31:59,:);wine(96:130,:);wine(154:178,:)];
test_label = [wine_labels(31:59);wine_labels(96:130);wine_labels(154:178)];
[train,pstrain] = mapminmax(train');
pstrain.ymin = 0;
pstrain.ymax = 1;
[train,pstrain] = mapminmax(train,pstrain);
[test,pstest] = mapminmax(test');
pstest.ymin = 0;
pstest.ymax = 1;
[test,pstest] = mapminmax(test,pstest);
train = train';
test = test';
%% 参数初始化
%粒子群算法中的两个参数
c1 = 1.6; % c1 belongs to [0,2]
c2 = 1.5; % c2 belongs to [0,2]
maxgen=300; % 进化次数
sizepop=30; % 种群规模
popcmax=10^(2);
popcmin=10^(-1);
popgmax=10^(3);
popgmin=10^(-2);
k = 0.6; % k belongs to [0.1,1.0];
Vcmax = k*popcmax;
Vcmin = -Vcmax ;
Vgmax = k*popgmax;
Vgmin = -Vgmax ;
% SVM参数初始化
v = 3;
%% 产生初始粒子和速度
for i=1:sizepop
% 随机产生种群
pop(i,1) = (popcmax-popcmin)*rand+popcmin; % 初始种群
pop(i,2) = (popgmax-popgmin)*rand+popgmin;
V(i,1)=Vcmax*rands(1); % 初始化速度
V(i,2)=Vgmax*rands(1);
% 计算初始适应度
cmd = ['-v ',num2str(v),' -c ',num2str( pop(i,1) ),' -g ',num2str( pop(i,2) )];
fitness(i) = svm train(train_label, train, cmd);
fitness(i) = -fitness(i);
end
% 找极值和极值点
[global_fitness bestindex]=min(fitness); % 全局极值
local_fitness=fitness; % 个体极值初始化
global_x=pop(bestindex,:); % 全局极值点
local_x=pop; % 个体极值点初始化
tic
%% 迭代寻优
for i=1:maxgen
for j=1:sizepop
%速度更新
wV = 0.9; % wV best belongs to [0.8,1.2]
V(j,:) = wV*V(j,:) + c1*rand*(local_x(j,:) - pop(j,:)) + c2*rand*(global_x - pop(j,:));
if V(j,1) > Vcmax
V(j,1) = Vcmax;
end
if V(j,1) < Vcmin
V(j,1) = Vcmin;
end
if V(j,2) > Vgmax
V(j,2) = Vgmax;
end
if V(j,2) < Vgmin
V(j,2) = Vgmin;
end
%种群更新
wP = 0.6;
pop(j,:)=pop(j,:)+wP*V(j,:);
if pop(j,1) > popcmax
pop(j,1) = popcmax;
end
if pop(j,1) < popcmin
pop(j,1) = popcmin;
end
if pop(j,2) > popgmax
pop(j,2) = popgmax;
end
if pop(j,2) < popgmin
pop(j,2) = popgmin;
end
% 自适应粒子变异
if rand>0.5
k=ceil(2*rand);
if k == 1
pop(j,k) = (20-1)*rand+1;
end
if k == 2
pop(j,k) = (popgmax-popgmin)*rand+popgmin;
end
end
%适应度值
cmd = ['-v ',num2str(v),' -c ',num2str( pop(j,1) ),' -g ',num2str( pop(j,2) )];
fitness(j) = svmtrain(train_label, train, cmd);
fitness(j) = -fitness(j);
end
%个体最优更新
if fitness(j) < local_fitness(j)
local_x(j,:) = pop(j,:);
local_fitness(j) = fitness(j);
end
%群体最优更新
if fitness(j) < global_fitness
global_x = pop(j,:);
global_fitness = fitness(j);
end
fit_gen(i)=global_fitness;
end
toc
%% 结果分析
plot(-fit_gen,'LineWidth',5);
title(['适应度曲线','(参数c1=',num2str(c1),',c2=',num2str(c2),',终止代数=',num2str(maxgen),')'],'FontSize',13);
xlabel('进化代数');ylabel('适应度');
bestc = global_x(1)
bestg = global_x(2)
bestCVaccuarcy = -fit_gen(maxgen)
cmd = ['-c ',num2str( bestc ),' -g ',num2str( bestg )];
model = svmtrain(train_label,train,cmd);
[trainpre,trainacc] = svmpredict(train_label,train,model);
trainacc
[testpre,testacc] = svmpredict(test_label,test,model);
testacc。