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

时间:2016-03-09 09:40:24

标签: matlab neural-network genetic-algorithm

我通过编辑ANN工具箱中的代码创建了NN模型。在我的工作中,我需要研究改变各种ANN拓扑结构对其性能的影响。该模型的目的是使用来自汽轮机的运行数据来训练网络。对数据进行归一化,然后根据标记为“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);

如何使用GA优化三种训练算法中的哪一种提供最佳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。如何从此开始创建健身功能?请告知:)

1 个答案:

答案 0 :(得分:0)

您可以使用图形邻居矩阵表示ANN中的所有连接。如果你在ANN中有N个神经元,那么你将拥有NxN邻居矩阵。此矩阵可以作为GA优化的对象。

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