I have a problem with a step, in the ANN-Genetic Algorithm optimisation.
Show older comments
% INITIALIZE THE NEURAL NETWORK PROBLEM %
% inputs for the neural net(AND gate example== 2 inputs && 4 samples)
clc
clear
close all
data1=xlsread('base');
data1=data1' ;
inputs = data1(1:4,:); % Les entres
targets =data1 (5:7,:); % Les sorties
% number of neurons
n = 8;
% create a neural network
net = feedforwardnet(n);
% configure the neural network for this dataset
net = configure(net, inputs, targets);
% get the normal NN weights and bias
getwb(net)
% error MSE normal NN
error = targets - net(inputs);
calc = mean(error.^2)/mean(var(targets',1))
% create handle to the MSE_TEST function, that
% calculates MSE
h = @(x) NMSE(x, net, inputs, targets);
% Setting the Genetic Algorithms tolerance for
% minimum change in fitness function before
% terminating algorithm to 1e-8 and displaying
% each iteration's results.
ga_opts = gaoptimset('PopInitRange', [-1;1], 'TolFun', 1e-10,'display','iter');
ga_opts = gaoptimset(ga_opts, 'StallGenLimit', 100, 'FitnessLimit', 1e-5, 'Generations', 50);
% PLEASE NOTE: For a feed-forward network
% with n hidden neurons, 3n+n+1 quantities are required
% in the weights and biases column vector.
% a. n for the input weights=(features*n)=2*n
% b. n for the input biases=(n bias)=n
% c. n for the output weights=(n weights)=n
% d. 1 for the output bias=(1 bias)=1
% running the genetic algorithm with desired optionsb
[x, err_ga] = ga(h, 2*n+n+n+1, ga_opts); %(the broblem is here)
net = setwb(net, x');
% get the GA optimized NN weights and bias
getwb(net)
% error MSE GA optimized NN
error = targets - net(inputs);
calc = mean(error.^2)/mean(var(targets',1))
---------------------------------------------------------------------------------
Command Window
Index exceeds matrix dimensions.
Error in separatewb (line 34)
iw{i,j} = reshape(wb(hints.iwInd{i,j}),...
Error in setwb (line 23)
[b,IW,LW] = separatewb(net,wb,hints);
Error in NMSE (line 7)
net = setwb(net, wb');
Error in @(x)NMSE(x,net,inputs,targets)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 356)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in main (line 50)
[x, err_ga] = ga(h, 2*n+n+n+1, ga_opts);
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
1 Comment
ikeuwanuakwa
on 8 Dec 2022
[x, err_ga] = ga(h, 2*n+n+n+1, ga_opts); %(the broblem is here)
change the 2 in 2*n+n+n+1 to be greater than the number of input parameter.
For example 8*n+n+n+1.... for input less than or equal to 8.
Answers (0)
Categories
Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!