极限学习机(extreme learning machine,ELM)作为一种新兴的机器学习方法,已经成为了一个热门的研究方向。 ELM 随机确定单隐含层网络的输入权值和隐含层节点偏置,通过简单计算即可解析得到输出权值,不仅加快了学习速度,而且具有很好的泛化能力。 核函数 ELM 将核函数引入到极限学习机理论中,可以得到最小平方优化解,解决了
ELM 算法随机初始化问题,对模型学习参数具有较好的鲁棒性,具有更稳定的、更好的泛化性能。
function omega = kernel_matrix(Xtrain,kernel_type, kernel_pars,Xt)
nb_data = size(Xtrain,1);%nxm 样本数nb
if strcmp(kernel_type,'RBF_kernel'),
if nargin<4,
XXh = sum(Xtrain.^2,2)*ones(1,nb_data);%nxn
omega = XXh+XXh'-2*(Xtrain*Xtrain');
omega = exp(-omega./kernel_pars(1));
else
XXh1 = sum(Xtrain.^2,2)*ones(1,size(Xt,1));
XXh2 = sum(Xt.^2,2)*ones(1,nb_data);
omega = XXh1+XXh2' - 2*Xtrain*Xt';
omega = exp(-omega./kernel_pars(1));
end
elseif strcmp(kernel_type,'lin_kernel')
if nargin<4,
omega = Xtrain*Xtrain';
else
omega = Xtrain*Xt';
end
elseif strcmp(kernel_type,'poly_kernel')
if nargin<4,
omega = (Xtrain*Xtrain'+kernel_pars(1)).^kernel_pars(2);
else
omega = (Xtrain*Xt'+kernel_pars(1)).^kernel_pars(2);
end
elseif strcmp(kernel_type,'wav_kernel')
if nargin<4,
XXh = sum(Xtrain.^2,2)*ones(1,nb_data);
omega = XXh+XXh'-2*(Xtrain*Xtrain');
XXh1 = sum(Xtrain,2)*ones(1,nb_data);
omega1 = XXh1-XXh1';
omega = cos(kernel_pars(3)*omega1./kernel_pars(2)).*exp(-omega./kernel_pars(1));
else
XXh1 = sum(Xtrain.^2,2)*ones(1,size(Xt,1));
XXh2 = sum(Xt.^2,2)*ones(1,nb_data);
omega = XXh1+XXh2' - 2*(Xtrain*Xt');
XXh11 = sum(Xtrain,2)*ones(1,size(Xt,1));
XXh22 = sum(Xt,2)*ones(1,nb_data);
omega1 = XXh11-XXh22';
clc;clear;close all
%% 将鲸鱼与改进鲸鱼用于函数极小值寻优,验证改进算法的有效性
% 优化函数极小值
popsize=10;
Max_iter=100;
dim=5;
[best0,pop0,trace0]=woa(popsize,Max_iter,dim);
best0
[best,pop,trace]=vnwoa(popsize,Max_iter,dim);
best
%%
figure
subplot(121)
plot(trace0)
hold on
plot(trace)
legend('woa','vnwoa')
subplot(122)
plot(log10(trace0))%对结果取对数 使结果更加直观
hold on
plot(log10(trace))
legend('woa','vnwoa')
function y=boundary(x,hmin,hmax);
[m n]=size(x);
for i=1:m
for j=1:n
if x(i,j)>=hmax
x(i,j)=rand*(hmax-hmin)+hmin;
elseif x(i,j)<=hmin
x(i,j)=rand*(hmax-hmin)+hmin;
end
end
y=x;
end%
% ylabel('样本标签')
% legend('真实标签', '预测标签');
% title(['KELM分类精度为:',num2str(acc*100),'%'])
xlabel('Number of test samples');
ylabel('Class label');
legend('Actual output label','Desired output label');
title(['Accuracy:',num2str(acc*100),'%']);
legend('boxoff');
hold off;
omega = cos(kernel_pars(3)*omega1./kernel_pars(2)).*exp(-omega./kernel_pars(1));
end
end
function [Leader_score,Leader_pos,Convergence_curve]=woa(sizepop,Max_iter,dim)
lb=-100;
ub=100;
Convergence_curve=zeros(1,Max_iter);
for i=1:sizepop
Positions(i,:)=rand(1,dim).*(ub-lb)+lb;
end
for i=1:sizepop
p(i)=fun(Positions(i,:)); %计算当前个体适应度值
end
[~, index]=min(p);
Leader_pos=Positions(index,:);
Leader_score=p(index);
% Main loop
for t=1:Max_iter
a=5-t*((2)/Max_iter);
a2=-1+t*((-1)/Max_iter);
for i=1:size(Positions,1)
r1=rand;
r2=rand;
A=2*a*r1-a;
C=2*r2;
b=1;
l=(a2-1)*rand+1;
p = rand;
for j=1:size(Positions,2)
if p<0.5
if abs(A)>=1
rand_leader_index = floor(sizepop*rand+1);
X_rand = Positions(rand_leader_index, :);
D_X_rand=abs(C*X_rand(j)-Positions(i,j));
Positions(i,j)=X_rand(j)-A*D_X_rand;
elseif abs(A)<1
D_Leader=abs(C*Leader_pos(j)-Positions(i,j));
Positions(i,j)=Leader_pos(j)-A*D_Leader;
end
elseif p>=0.5
distance2Leader=abs(Leader_pos(j)-Positions(i,j));
Positions(i,j)=distance2Leader*exp(b.*l).*cos(l.*2*pi)+Leader_pos(j);
end
end
Positions(i,:)=boundary(Positions(i,:),lb,ub);
fitness=fun(Positions(i,:));
if fitness
Leader_pos=Positions(i,:);
end
end
Convergence_curve(t)=Leader_score;
end
function f =luoyiman_localbest(x1,x2,x3,x4,x5)
%求解当前个体粒子环形邻域中的局部最优,然后用这个局部最优替换当前个体
K0=[x1;x2;x3;x4;x5];
K1=[fun(x1), ...
fun(x2), ...
fun(x3), ...
fun(x4), ...
fun(x5)];
[~ ,index]=min(K1);
f=K0(index,:);
function fit=fun(pop)
% 这3种函数的最小值都是0
%1.sphere函数
% fit=sum(pop.^2);
%2. ackly函数
% fit=-20*exp(-0.2*sqrt(mean(pop.^2)))-exp(mean(cos(2*pi.*pop)))+20+exp(1);
% 3.rastrigin's函数
fit=20+pop(1)^2+pop(2)^2-10*(cos(2*pi*pop(1))+cos(2*pi*pop(2)));
[1]吴忠强, 卢雪琴, 何怡林,等. 基于鲸鱼算法优化极限学习机的微电网故障诊断方法[J]. 智慧电力, 2022, 50(2):7.
[2]郭延华, 赵帅. 基于KPCA-WOA-KELM的岩爆烈度预测[J]. 河北工程大学学报:自然科学版, 2021, 38(2):7.
[3]裘日辉, 刘康玲, 谭海龙,等. 基于极限学习机的分类算法及在故障识别中的应用[J]. 浙江大学学报:工学版, 2016, 50(10):8.
部分理论引用网络文献,若有侵权联系博主删除。