MATLAB机器学习系列-8 极限学习机(Extreme Learning Machine, ELM)原理及其代码实现
生活随笔
收集整理的這篇文章主要介紹了
MATLAB机器学习系列-8 极限学习机(Extreme Learning Machine, ELM)原理及其代码实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
極限學習機(Extreme Learning Machine, ELM)
極限學習機網絡結構和BP網絡結構類似。 和BP神經網絡不同點:原理
前向傳播圖見下
T為目標輸出,g為激活函數
把上圖簡潔化,寫成矩陣,H為輸入層與隱含層相乘的結果
該算法創造者認為如果隱含層神經元個數等于輸入樣本個數相等,則
HB -T 矩陣范數為0,0誤差。
如果隱含層的神經元個數小于待學習的樣本個數,模型可以以一定精度學習,即模型的誤差小于一定的值
權重學習流程
因為可能存在H的逆矩陣不存在的情況,導致最后B沒有解,當時作者提出這個算法的時候給出的解決方法是求偽逆矩陣。
現在的解決方法,如圖
在H后面加上一個小塊矩陣,使
可逆。
則權重解為
極限學習機相比于BP神經網絡,SVM有什么特征
- 易用性。除了預定義的網絡架構外,不需要手動調優任何參數
- 更快的學習速度。大多數訓練可以在毫秒、秒和分鐘內完成
- 更高的泛化性能。在大多數情況下,該算法比BP算法具有更好的泛化性能與SVM相似或優于SVM的泛化性能。
- 適用于幾乎所有的非線性激活函數。幾乎所有分段連續(包括不連續)微分、非微分函數)可作為激活函數使用。BP神經網絡要求激活函數可導可微分。
- 適合于完全復雜的激活函數。完全復雜函數也可以作為激活函數使用ELM。
極限學習機( ELM)仿真
重點函數
- nargin:n arg in:自動計算出方法輸入了幾個參數
- error:給出錯誤信息
- pinv:求偽逆矩陣
- sin / hardlim:涉及到激活函數
- elmtrain 自己寫的函數,用于ELM訓練,記住:每一列代表一個樣本
elmtrain.m
function [IW,B,LW,TF,TYPE] = elmtrain(P,T,N,TF,TYPE) % ELMTRAIN Create and Train a Extreme Learning Machine % Syntax % [IW,B,LW,TF,TYPE] = elmtrain(P,T,N,TF,TYPE) % Description % Input % P - Input Matrix of Training Set (R*Q) % T - Output Matrix of Training Set (S*Q) % N - Number of Hidden Neurons (default = Q) % TF - Transfer Function: % 'sig' for Sigmoidal function (default) % 'sin' for Sine function % 'hardlim' for Hardlim function % TYPE - Regression (0,default) or Classification (1) % Output % IW - Input Weight Matrix (N*R) % B - Bias Matrix (N*1) % LW - Layer Weight Matrix (N*S) % Example % Regression: % [IW,B,LW,TF,TYPE] = elmtrain(P,T,20,'sig',0) % Y = elmtrain(P,IW,B,LW,TF,TYPE) % Classification % [IW,B,LW,TF,TYPE] = elmtrain(P,T,20,'sig',1) % Y = elmtrain(P,IW,B,LW,TF,TYPE) % See also ELMPREDICT % Yu Lei,11-7-2010 % Copyright www.matlabsky.com % $Revision:1.0 $ if nargin < 2error('ELM:Arguments','Not enough input arguments.'); end if nargin < 3N = size(P,2); end if nargin < 4TF = 'sig'; end if nargin < 5TYPE = 0; end if size(P,2) ~= size(T,2)error('ELM:Arguments','The columns of P and T must be same.'); end [R,Q] = size(P); if TYPE == 1T = ind2vec(T); end [S,Q] = size(T); % Randomly Generate the Input Weight Matrix IW = rand(N,R) * 2 - 1; % Randomly Generate the Bias Matrix B = rand(N,1); BiasMatrix = repmat(B,1,Q); % Calculate the Layer Output Matrix H tempH = IW * P + BiasMatrix; switch TFcase 'sig'H = 1 ./ (1 + exp(-tempH));case 'sin'H = sin(tempH);case 'hardlim'H = hardlim(tempH); end % Calculate the Output Weight Matrix LW = pinv(H') * T';elmpredict.m
function Y = elmpredict(P,IW,B,LW,TF,TYPE) % ELMPREDICT Simulate a Extreme Learning Machine % Syntax % Y = elmtrain(P,IW,B,LW,TF,TYPE) % Description % Input % P - Input Matrix of Training Set (R*Q) % IW - Input Weight Matrix (N*R) % B - Bias Matrix (N*1) % LW - Layer Weight Matrix (N*S) % TF - Transfer Function: % 'sig' for Sigmoidal function (default) % 'sin' for Sine function % 'hardlim' for Hardlim function % TYPE - Regression (0,default) or Classification (1) % Output % Y - Simulate Output Matrix (S*Q) % Example % Regression: % [IW,B,LW,TF,TYPE] = elmtrain(P,T,20,'sig',0) % Y = elmtrain(P,IW,B,LW,TF,TYPE) % Classification % [IW,B,LW,TF,TYPE] = elmtrain(P,T,20,'sig',1) % Y = elmtrain(P,IW,B,LW,TF,TYPE) % See also ELMTRAIN % Yu Lei,11-7-2010 % Copyright www.matlabsky.com % $Revision:1.0 $ if nargin < 6error('ELM:Arguments','Not enough input arguments.'); end % Calculate the Layer Output Matrix H Q = size(P,2); BiasMatrix = repmat(B,1,Q); tempH = IW * P + BiasMatrix; switch TFcase 'sig'H = 1 ./ (1 + exp(-tempH));case 'sin'H = sin(tempH);case 'hardlim'H = hardlim(tempH); end % Calculate the Simulate Output Y = (H' * LW)'; if TYPE == 1temp_Y = zeros(size(Y));for i = 1:size(Y,2)[~,index] = max(Y(:,i));temp_Y(index,i) = 1;endY = vec2ind(temp_Y); end分類
鳶尾花侯種類識別
這個數據集網上很多,就不放啦
main_iris.m
%% I. 清空環境變量 clear all clc%% II. 訓練集/測試集產生 %% % 1. 導入數據 load iris_data.mat%% % 2. 隨機產生訓練集和測試集 P_train = []; T_train = []; P_test = []; T_test = []; for i = 1:3temp_input = features((i-1)*50+1:i*50,:);temp_output = classes((i-1)*50+1:i*50,:);n = randperm(50);% 訓練集——120個樣本P_train = [P_train temp_input(n(1:40),:)'];T_train = [T_train temp_output(n(1:40),:)'];% 測試集——30個樣本P_test = [P_test temp_input(n(41:50),:)'];T_test = [T_test temp_output(n(41:50),:)']; end%% III. ELM創建/訓練 [IW,B,LW,TF,TYPE] = elmtrain(P_train,T_train,20,'sig',1);%% IV. ELM仿真測試 T_sim_1 = elmpredict(P_train,IW,B,LW,TF,TYPE); T_sim_2 = elmpredict(P_test,IW,B,LW,TF,TYPE);%% V. 結果對比 result_1 = [T_train' T_sim_1']; result_2 = [T_test' T_sim_2']; %% % 1. 訓練集正確率 k1 = length(find(T_train == T_sim_1)); n1 = length(T_train); Accuracy_1 = k1 / n1 * 100; disp(['訓練集正確率Accuracy = ' num2str(Accuracy_1) '%(' num2str(k1) '/' num2str(n1) ')'])%% % 2. 測試集正確率 k2 = length(find(T_test == T_sim_2)); n2 = length(T_test); Accuracy_2 = k2 / n2 * 100; disp(['測試集正確率Accuracy = ' num2str(Accuracy_2) '%(' num2str(k2) '/' num2str(n2) ')'])%% VI. 繪圖 figure(2) plot(1:30,T_test,'bo',1:30,T_sim_2,'r-*') grid on xlabel('測試集樣本編號') ylabel('測試集樣本類別') string = {'測試集預測結果對比(ELM)';['(正確率Accuracy = ' num2str(Accuracy_2) '%)' ]}; title(string) legend('真實值','ELM預測值')回歸
汽油辛烷值預測
代碼和數據百度云
鏈接:https://pan.baidu.com/s/1o08kfLvUN3n5pYyE9ycfWg 提取碼:q9vz 復制這段內容后打開百度網盤手機App,操作更方便哦
作者:電氣工程的計算機萌新-余登武
總結
以上是生活随笔為你收集整理的MATLAB机器学习系列-8 极限学习机(Extreme Learning Machine, ELM)原理及其代码实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么计算平安银行信用卡的分期利息 算法一
- 下一篇: 汇中面签后多久到账