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

📄 orth_gramschmidt.m

📁 本人编写的GramSchmidt神经网络算法
💻 M
字号:
function [TrainingTime, TrainingAccuracy, TestingAccuracy]=Orth_GramSchmidt(TrainingData_File, TestingData_File, NumberofHiddenNeurons, ActivationFunction, Problem_Type)


% How to run like this : Orth_GramSchmidt( 'abalone_train', 'abalone_test', 10, 'sig', 0);

REGRESSION = 0;
CLASSIFIER = 1;

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

%%%%%%%%%%% Load testing dataset
test_data=load(TestingData_File);
t_testing=test_data(:,1)';
x_testing=test_data(:,2:size(test_data,2))';
clear test_data;   

NumberofTrainingData=size(I,2);
NumberofTestingData=size(x_testing,2);
NumberofInputNeurons=size(I,1);
NumberofOutputNeurons=size(T,1);

if Problem_Type~=REGRESSION
    %%%%%%%%%%%% Preprocessing the data of classification
    sorted_target=sort(cat(2,T,t_testing),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) == t_testing(1,i)
                break; 
            end
        end
        temp_TV_T(j,i)=1;
    end
    t_testing = temp_TV_T*2-1;
end  
clear temp_T;

% Q=zeros(NumberofTrainingData,NumberofTrainingData);
Htrainout=zeros(NumberofTrainingData,NumberofHiddenNeurons);
InputWeight=zeros(NumberofInputNeurons,NumberofHiddenNeurons);
HiddenBias=zeros(1,NumberofHiddenNeurons);
L=0;

starting_cpu=cputime;
%%%%%%%%%%%%%%%     Train matrix    %%%%%%%%
while L<NumberofHiddenNeurons
    L=L+1;
    switch lower(ActivationFunction)
        case {'rbf'}
            InputWeight(:,L)=2*rand(NumberofInputNeurons,1)-1; % randomly chose InputWeight for Neuron L;  for other activation functions except RBF
            temp=rand(1,1);
            while temp<10^-3
                temp = rand(1,1);
            end
            HiddenBias(L)=temp;
        case {'rbf_gamma'}
            InputWeight(:,L)=2*rand(NumberofInputNeurons,1)-1; % randomly chose InputWeight for Neuron L;  for other activation functions except RBF
            HiddenBias(L)=0.5*rand(1,1);
        otherwise
            InputWeight(:,L)=2*rand(NumberofInputNeurons,1)-1; % randomly chose InputWeight for Neuron L;  for other activation functions except RBF
            HiddenBias(L)=2*rand(1,1)-1;
    end
    Htrainout(:,L)=hidden_output(I,InputWeight(:,L),HiddenBias(L),ActivationFunction,NumberofInputNeurons)';
end % End while when TrainingResidualError not larger than min_goal

[Q,R]=GramSchmidt(Htrainout);
weights_QR=T*Q; % row vector
TrainingTime = cputime-starting_cpu;
%%%%%%%%%%%%%%%     computing training error    %%%%%%%%
% matrix_trans=pinv(Htrainout)*Q;
% Out_train = (Htrainout*(matrix_trans*weights_QR'))';
InverseR=pinv(R);
Out_train = (Htrainout*(InverseR*weights_QR'))';

%%%%%%%%%%%%%%%     Test matrix    %%%%%%%%
% %%%%%%%%%%% Load testing dataset
% test_data=load(TestingData_File);
% t_testing=test_data(:,1)';
% x_testing=test_data(:,2:size(test_data,2))';
% clear test_data;                                    %   Release raw testing data array
test_cpu = cputime;

NumberofTestingData=size(x_testing,2);
HidenTestOutput=zeros(NumberofTestingData,NumberofHiddenNeurons);
L=0;
while L<NumberofHiddenNeurons
    L=L+1;
    HidenTestOutput(:,L)=hidden_output(x_testing,InputWeight(:,L),HiddenBias(L),ActivationFunction,NumberofInputNeurons)';
end % End while when TrainingResidualError not larger than min_goal
%%%%%%%%%%%%%%%     computing testing error    %%%%%%%%
Out_test = (HidenTestOutput*(InverseR*weights_QR'))';

TestingTime = cputime - test_cpu;
% TestingAccuracy= sqrt(mse(t_testing'-Out_test));

if Problem_Type == REGRESSION
    TrainingAccuracy= sqrt(mse(T-Out_train));
    TestingAccuracy= sqrt(mse(t_testing-Out_test));
end

if Problem_Type == CLASSIFIER
    MissClassificationRate_Training=0;
    MissClassificationRate_Testing=0;

    for i = 1 : size(T, 2)
        [x, label_index_expected]=max(T(:,i));
        [x, label_index_actual]=max(Out_train(:,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(t_testing, 2)
        [x, label_index_expected]=max(t_testing(:,i));
        [x, label_index_actual]=max(Out_test(:,i));
        if label_index_actual~=label_index_expected
            MissClassificationRate_Testing=MissClassificationRate_Testing+1;
        end
    end
    TestingAccuracy = 1-MissClassificationRate_Testing/size(t_testing,2); 
end

function y1=hidden_output(x,w,b,ActivationFunction,NumberofInputs)

switch lower(ActivationFunction)
    case {'sin','sine'}
        %%%%%%%% Sines
        y1=sin(w'*x+b);     
    case {'rbf'}
        %%%%%%%% RBF
        NumberofTraining=size(x,2);
        ind=ones(1,NumberofTraining);
                            
        extend_weight=w(:,ind);%%   w is column vector
        if NumberofInputs==1
            tempH=-((x-extend_weight).^2);
        else
            tempH=-sum((x-extend_weight).^2);
        end

        BiasMatrix=b(:,ind);  
        tempH=tempH./BiasMatrix;
        clear extend_weight;    
        y1=exp(tempH); 
%         y1=exp(tempH)+0.0001;
    case {'rbf_gamma'}
        %%%%%%%% RBF
        NumberofTraining=size(x,2);
        ind=ones(1,NumberofTraining);
                            
        extend_weight=w(:,ind);%%   w is column vector
        if NumberofInputs==1
            tempH=-((x-extend_weight).^2);
        else
            tempH=-sum((x-extend_weight).^2);
        end

        BiasMatrix=b(:,ind);  
        tempH=tempH.*BiasMatrix;
        clear extend_weight;    
        y1=exp(tempH); 
%         y1=exp(tempH)+0.0001;         
    case {'tri'}
        %%%%%%%% Triangle
        x1=w'*x+b;
        if abs(x1)>1
            y1=0;
        elseif x1>0
            y1=1-x1;
        else
            y1=x1+1;
        end
    case {'hardlim'}
        %%%%%%%% Hardlimit
        x1=w'*x+b;
        y1=sign(x1);
    case {'gau'}
        %%%%%%%% Gaussian
        x1=w'*x+b;
        y1=exp(-x1.^2);
    case {'sig','sigmoid'}
        %%%%%%%% Sigmoid 
        bias_vector = b*ones(1,size(x,2));
        y1=1./(1+exp(-(w'*x+bias_vector)));
    case {'windows'}
        %%%%%%%% windows
        x1=w'*x+b;
        traina = x1<=1;
        trainb = x1>=-1;    
        y1 = traina.*trainb+0.0001;
        %%%%%%%% More activation functions can be added here
        
end

⌨️ 快捷键说明

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