如何利用遗传算法优化神经网络结构



我通过编辑ANN工具箱中的代码创建了NN模型。在我的工作中,我需要研究改变各种神经网络拓扑结构对其性能的影响。该模型的目的是利用汽轮机的运行数据来训练网络。数据被标准化,然后将根据标记为"1"的实际故障发生和正常操作期间的"0"来设置目标。在我的下一项工作中,我试图通过使用GA.来优化ANN拓扑

% load data
load data.mat;

x = data;
t = target;
% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. NFTOOL falls back to this in low memory situations.
trainFcn = 'trainscg';  % Bayesian Regularization
% Create a Feedforward Network
hiddenLayerSize = 6;
net = feedforwardnet (hiddenLayerSize,trainFcn);
% Setup Division of Data for Training, Validation, Testing
RandStream.setGlobalStream(RandStream('mt19937ar','seed',1)); % to make the weight constant
net.divideFcn = 'divideblock'; % Divide targets into three sets using blocks of indices
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
%TRAINING PARAMETERS
net.trainParam.show=50;  %# of ephocs in display
net.trainParam.lr=0.05;  %learning rate
net.trainParam.epochs=10000;  %max epochs
net.trainParam.goal=0.05^2;  %training goal
net.performFcn='mse';  %Name of a network performance function %type help nnperformance
% Setup of activation/transfer function
net.layers{1}.transferFcn = 'tansig';
net.layers{2}.transferFcn = 'tansig';
% Train the Network
[net,tr] = train(net,x,t);
% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y)
% View the Network
%view(net)
plot(1:length(t), t, 1:length(y), y);

如何使用遗传算法来优化三种训练算法中哪种算法的RMSE最好。在我的工作中,我还需要考虑神经元的数量、隐藏层的数量和激活函数。我计划使用二进制编码来做到这一点,例如:

% Preliminary function decoding:
            function [algo, archit, activf1, activf2] = decoding(X)
            %
            % function that decodes a binary string into information
            % about the NN structure and training tralgorithm
            %
            %   [algo, archit, activf1 activf2] = decoding(X)
            %
            % "X" is a population of binary strings of size 10 (excluding 32param)
            % eg:[00,0000,00 00]
            %
            % algo  = for training algorithm trainscg, trainlm, trainbr
            %       
            % archit = hidden layer neurons from 0000 to 1001 
            %
            % activf1 = type of activation functions of hidden nodes
            % activf2 = type of activation functions of output nodes
            %           = 00 for logsig
            %           = 01 for tansig
            %           = 10 for purelin
            %M=Pz (population size)
            M = size(X,1);
            %initializations:
            a = zeros(M,2); %size for algo (training algorithm)
            b = zeros(M,4); %size for archit (hidden layer neuron)
            c = zeros(M,4); %size for activf and activf2
            %d = zeros(Pz,32);
            for i=1:M
            a = X(i,1:2);
            b = X(i,3:8);
            c = X(i,9:10);
            % for algo (training algorithm)
            if (a == [0 0])
            algo(i) = ['trainscg'];
            elseif (a == [0 1])
            algo(i) = ['trainlm'];
            elseif (a == [1 0])
            algo(i) = ['trainbr'];
            end
            % for archit (hidden layer neuron)
            if (b == [0 0 0 0])
            archit(i,1) = 1;
            elseif (b == [0 0 0 1])
            archit(i,1) = 2;
            elseif (b == [0 0 1 0])
            archit(i,1) = 3;
            elseif (b == [0 0 1 1])
            archit(i,1) = 4;
            elseif (b == [0 1 0 0])
            archit(i,1) = 5;
            elseif (b == [0 1 0 1])
            archit(i,1) = 6;
            elseif (b == [0 1 1 0])
            archit(i,1) = 7;
            elseif (b == [0 1 1 1])
            archit(i,1) = 8;
            elseif (b == [1 0 0 0])
            archit(i,1) = 9;
            elseif (b == [1 0 0 1])
            archit(i,1) = 10;
            end
            % for activf and activf2
            if (c == [0 0 0 0])
            activf1(i,:) = ['logsig'];
            activf2(i,:) = ['logsig'];
            elseif (c == [0 0 0 1])
            activf1(i,:) = ['logsig'];
            activf2(i,:) = ['tansig'];
            elseif (c == [0 0 1 0])
            activf1(i,:) = ['logsig'];
            activf2(i,:) = ['purelin'];
            elseif (c == [0 0 1 1])
            activf1(i,:) = ['tansig'];
            activf2(i,:) = ['logsig'];
            elseif (c == [0 1 0 0])
            activf1(i,:) = ['tansig'];
            activf2(i,:) = ['tansig'];
            elseif (c == [0 1 0 1])
            activf1(i,:) = ['tansig'];
            activf2(i,:) = ['purelin'];
            elseif (c == [0 1 1 0])
            activf1(i,:) = ['purelin'];
            activf2(i,:) = ['logsig'];
            elseif (c == [0 1 1 1])
            activf1(i,:) = ['purelin'];
            activf2(i,:) = ['tansig'];
            elseif (c == [1 0 0 0])
            activf1(i,:) = ['purelin'];
            activf2(i,:) = ['purelin'];
            end
            end %for
            'Population was decoded!' 

基于我在GA方面的有限知识,我需要创建一个适应度函数和一个函数句柄。这个网络的性能指标是RMSE,所以RMSE应该是GA的适应度。从现在开始,如何创建适应度函数?请告知:(

您可以用图邻居矩阵表示ANN中的所有连接。如果你在ANN中总共有N个神经元,你就会有NxN个邻居矩阵。此矩阵可以是GA优化的对象。

n(i(=1/(1-exp(-sum(a(i,j(*w(i,j(*n(j(((

最新更新