⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elm.m

📁 神经网络的ELM算法
💻 M
字号:
function [TrainingTime, TrainingAccuracy, TestingAccuracy] = elm(TrainingData_File, TestingData_File, NumberofHiddenNeurons, ActivationFunction, Elm_Type)

% Usage: elm(TrainingData_File, TestingData_File, Elm_Type, NumberofHiddenNeurons, ActivationFunction)
% OR:    [TrainingTime, TestingTime, TrainingAccuracy, TestingAccuracy] = elm(TrainingData_File, TestingData_File, Elm_Type, NumberofHiddenNeurons, ActivationFunction)
%
% Input:
% TrainingData_File     - Filename of training data set
% TestingData_File      - Filename of testing data set
% Elm_Type              - 0 for regression; 1 for (both binary and multi-classes) classification
% NumberofHiddenNeurons - Number of hidden neurons assigned to the ELM
% ActivationFunction    - Type of activation function:
%                           'sig' for Sigmoidal function
%                           'sin' for Sine function
%                           'hardlim' for Hardlim function
%
% Output: 
% TrainingTime          - Time (seconds) spent on training ELM
% TestingTime           - Time (seconds) spent on predicting ALL testing data
% TrainingAccuracy      - Training accuracy: 
%                           RMSE for regression or correct classification rate for classification
% TestingAccuracy       - Testing accuracy: 
%                           RMSE for regression or correct classification rate for classification
%
% MULTI-CLASSE CLASSIFICATION: NUMBER OF OUTPUT NEURONS WILL BE AUTOMATICALLY SET EQUAL TO NUMBER OF CLASSES
% FOR EXAMPLE, if there are 7 classes in all, there will have 7 output
% neurons; neuron 5 has the highest output means input belongs to 5-th class
%
% Sample1 regression: [TrainingTime, TestingTime, TrainingAccuracy, TestingAccuracy] = elm('sinc_train', 'sinc_test', 0, 20, 'sig')
% Sample2 classification: elm('diabetes_train', 'diabetes_test', 1, 20, 'sig')
%


%%%%%%%%%%% Macro definition
REGRESSION=0;
CLASSIFIER=1;

%%%%%%%%%%% Load training dataset
train_data=load(TrainingData_File);
T=train_data(:,1)';
P=train_data(:,2:size(train_data,2))';
clear train_data;                                   %   Release raw training data array

%%%%%%%%%%% Load testing dataset
test_data=load(TestingData_File);
TV.T=test_data(:,1)';
TV.P=test_data(:,2:size(test_data,2))';
clear test_data;                                    %   Release raw testing data array

NumberofTrainingData=size(P,2);
NumberofTestingData=size(TV.P,2);
NumberofInputNeurons=size(P,1);

if Elm_Type~=REGRESSION
    %%%%%%%%%%%% Preprocessing the data of classification
    sorted_target=sort(cat(2,T,TV.T),2);
    label=zeros(1,1);                               %   Find and save in 'label' class label from training and testing data sets
    label(1,1)=sorted_target(1,1);
    j=1;
    for i = 2:(NumberofTrainingData+NumberofTestingData)
        if sorted_target(1,i) ~= label(1,j)
            j=j+1;
            label(1,j) = sorted_target(1,i);
        end
    end
    number_class=j;
    NumberofOutputNeurons=number_class;
    
    %%%%%%%%%% Processing the targets of training
    temp_T=zeros(NumberofOutputNeurons, NumberofTrainingData);
    for i = 1:NumberofTrainingData
        for j = 1:number_class
            if label(1,j) == T(1,i)
                break; 
            end
        end
        temp_T(j,i)=1;
    end
    T=temp_T*2-1;

    %%%%%%%%%% Processing the targets of testing
    temp_TV_T=zeros(NumberofOutputNeurons, NumberofTestingData);
    for i = 1:NumberofTestingData
        for j = 1:number_class
            if label(1,j) == TV.T(1,i)
                break; 
            end
        end
        temp_TV_T(j,i)=1;
    end
    TV.T=temp_TV_T*2-1;
end                                                 %   end if of Elm_Type

%%%%%%%%%%% Calculate weights & biases
start_time_train=cputime;

%%%%%%%%%%% Random generate input weights InputWeight (w_i) and biases BiasofHiddenNeurons (b_i) of hidden neurons
InputWeight=rand(NumberofHiddenNeurons,NumberofInputNeurons)*2-1;
switch lower(ActivationFunction)
    case {'rbf'}
        BiasofHiddenNeurons=rand(NumberofHiddenNeurons,1);

        ind=ones(1,NumberofTrainingData);
        for i=1:NumberofHiddenNeurons
            this_weight=InputWeight(i,:);            
            extend_weight=this_weight(ind,:)';
            if NumberofInputNeurons==1
                tempH(i,:)=-((P-extend_weight).^2);
            else
                tempH(i,:)=-sum((P-extend_weight).^2);
            end
        end
        BiasMatrix=BiasofHiddenNeurons(:,ind);  
        tempH=tempH./BiasMatrix;
        clear extend_weight;    

    case {'rbf_gamma'}
        BiasofHiddenNeurons=rand(NumberofHiddenNeurons,1)*0.5;

        ind=ones(1,NumberofTrainingData);
        for i=1:NumberofHiddenNeurons
            this_weight=InputWeight(i,:);            
            extend_weight=this_weight(ind,:)';
            if NumberofInputNeurons==1
                tempH(i,:)=-((P-extend_weight).^2);
            else
                tempH(i,:)=-sum((P-extend_weight).^2);
            end
        end
        BiasMatrix=BiasofHiddenNeurons(:,ind);  
        tempH=tempH.*BiasMatrix;
        clear extend_weight;  
    otherwise
        BiasofHiddenNeurons=rand(NumberofHiddenNeurons,1);

        %%%%%%%% feedforward NN
        tempH=InputWeight*P;
        ind=ones(1,NumberofTrainingData);
        BiasMatrix=BiasofHiddenNeurons(:,ind);      %   Extend the bias matrix BiasofHiddenNeurons to match the demention of H
        tempH=tempH+BiasMatrix;
end

clear P;                                    %   Release input of training data
clear BiasMatrix                            %   Release bias matrix by Qinyu 19/04/2004

%%%%%%%%%%% Calculate hidden neuron output matrix H
switch lower(ActivationFunction)
    case {'sig','sigmoid'}
        %%%%%%%% Sigmoid 
        H = 1 ./ (1 + exp(-tempH));
    case {'sin','sine'}
        %%%%%%%% Sine
        H = sin(tempH);    
    case {'hardlim'}
        %%%%%%%% Hard Limit
        H = hardlim(tempH);            
    case {'rbf','rbf_gamma'}
        %%%%%%%% RBF
        H = exp(tempH);                    
        %%%%%%%% More activation functions can be added here                
end
clear tempH;                                        %   Release the temparary array for calculation of hidden neuron output matrix H

%%%%%%%%%%% Calculate output weights OutputWeight (beta_i)
OutputWeight=pinv(H') * T';
end_time_train=cputime;
TrainingTime=end_time_train-start_time_train;        %   Calculate CPU time (seconds) spent for training ELM

%%%%%%%%%%% Calculate the training accuracy
Y=(H' * OutputWeight)';                             %   Y: the actual output of the training data
if Elm_Type == REGRESSION
    TrainingAccuracy=sqrt(mse(T - Y));               %   Calculate training accuracy (RMSE) for regression case
end
clear H;

%%%%%%%%%%% Calculate the output of testing input
start_time_test=cputime;
switch lower(ActivationFunction)
    case {'rbf'}

        ind=ones(1,NumberofTestingData);
        for i=1:NumberofHiddenNeurons
            this_weight=InputWeight(i,:);            
            extend_weight=this_weight(ind,:)';
            if NumberofInputNeurons==1
                tempH_test(i,:)=-((TV.P-extend_weight).^2);
            else
                tempH_test(i,:)=-sum((TV.P-extend_weight).^2);
            end
            
        end
        BiasMatrix=BiasofHiddenNeurons(:,ind);  
        tempH_test=tempH_test./BiasMatrix;
        clear extend_weight;    
    case {'rbf_gamma'}
        ind=ones(1,NumberofTestingData);
        for i=1:NumberofHiddenNeurons
            this_weight=InputWeight(i,:);            
            extend_weight=this_weight(ind,:)';
            if NumberofInputNeurons==1
                tempH_test(i,:)=-((TV.P-extend_weight).^2);
            else
                tempH_test(i,:)=-sum((TV.P-extend_weight).^2);
            end
        end
        BiasMatrix=BiasofHiddenNeurons(:,ind);  
        tempH_test=tempH_test.*BiasMatrix;
        clear extend_weight;  

    otherwise
        %%%%%%%% feedforward NN
        tempH_test=InputWeight*TV.P;
        ind=ones(1,NumberofTestingData);
        BiasMatrix=BiasofHiddenNeurons(:,ind);      %   Extend the bias matrix BiasofHiddenNeurons to match the demention of H
        tempH_test=tempH_test + BiasMatrix;
end

clear TV.P;             %   Release input of testing data             
clear BiasMatrix                            %   Release bias matrix 

switch lower(ActivationFunction)
    case {'sig','sigmoid'}
        %%%%%%%% Sigmoid 
        H_test = 1 ./ (1 + exp(-tempH_test));
    case {'sin','sine'}
        %%%%%%%% Sine
        H_test = sin(tempH_test);        
    case {'hardlim'}
        %%%%%%%% Hard Limit
        H_test = hardlim(tempH_test);        
    case {'rbf','rbf_gamma'}
        %%%%%%%% RBF
        H_test = exp(tempH_test);                            
        %%%%%%%% More activation functions can be added here        
end
TY=(H_test' * OutputWeight)';                       %   TY: the actual output of the testing data
end_time_test=cputime;
TestingTime=end_time_test-start_time_test;           %   Calculate CPU time (seconds) spent by ELM predicting the whole testing data

if Elm_Type == REGRESSION
    TestingAccuracy=sqrt(mse(TV.T - TY));            %   Calculate testing accuracy (RMSE) for regression case
end

if Elm_Type == CLASSIFIER
%%%%%%%%%% Calculate training & testing classification accuracy
    MissClassificationRate_Training=0;
    MissClassificationRate_Testing=0;

    for i = 1 : size(T, 2)
        [x, label_index_expected]=max(T(:,i));
        [x, label_index_actual]=max(Y(:,i));
        if label_index_actual~=label_index_expected
            MissClassificationRate_Training=MissClassificationRate_Training+1;
        end
    end
    TrainingAccuracy=1-MissClassificationRate_Training/size(T,2);
    for i = 1 : size(TV.T, 2)
        [x, label_index_expected]=max(TV.T(:,i));
        [x, label_index_actual]=max(TY(:,i));
        if label_index_actual~=label_index_expected
            MissClassificationRate_Testing=MissClassificationRate_Testing+1;
        end
    end
    TestingAccuracy=1-MissClassificationRate_Testing/size(TV.T,2);  
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -