To optimize a neural network by giving 46*3 inputs from excel and getting 46*1 output using a genetic algorithm

3 ビュー (過去 30 日間)
I have found the answer from the Matlab team but the code is applied for a single input.
I need to get 3 inputs of 43 rows from excel and 1 output of 43 rows from excel should be trained by neural network and should be applied using genetic algorithm to find the mean squared error.
The following code example describes a function that returns the mean squared error for a given input of weights and biases, a network, its inputs and targets.
function mse_calc = mse_test(x, net, inputs, targets)
% 'x' contains the weights and biases vector
% in row vector form as passed to it by the
% genetic algorithm. This must be transposed
% when being set as the weights and biases
% vector for the network.
% To set the weights and biases vector to the
% one given as input
net = setwb(net, x');
% To evaluate the ouputs based on the given
% weights and biases vector
y = net(inputs);
% Calculating the mean squared error
mse_calc = sum((y-targets).^2)/length(y);
end
The following code example describes a script that sets up a basic Neural Network problem and the definition of a function handle to be passed to GA. It uses the above function to calculate the Mean Squared Error.
% INITIALIZE THE NEURAL NETWORK PROBLEM %
% inputs for the neural net
inputs = (1:10);
% targets for the neural net
targets = cos(inputs.^2);
% number of neurons
n = 2;
% create a neural network
net = feedforwardnet(n);
% configure the neural network for this dataset
net = configure(net, inputs, targets);
% create handle to the MSE_TEST function, that
% calculates MSE
h = @(x) mse_test(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('TolFun', 1e-8,'display','iter');
% PLEASE NOTE: For a feed-forward network
% with n neurons, 3n+1 quantities are required
% in the weights and biases column vector.
%
% a. n for the input weights
% b. n for the input biases
% c. n for the output weights
% d. 1 for the output bias
% running the genetic algorithm with desired options
[x_ga_opt, err_ga] = ga(h, 3*n+1, ga_opts);

採用された回答

Greg Heath
Greg Heath 2016 年 4 月 3 日
After you get your data from EXCEL, use my corrected version of the algorithm posted in the NEWSREADER.
By the way, it is not necessary to make multiple posts of the same problem in both the NEWSGROUP and ANSWERS.
Thank you for formally accepting my answer
Greg
  1 件のコメント
Priya Dharshini
Priya Dharshini 2016 年 4 月 4 日
編集済み: Walter Roberson 2016 年 4 月 5 日
After getting input from excel when running my M file i'm getting error like this
Undefined function 'gacommon' for input arguments of type 'function_handle'.
Error in ga (line 322)
[x,fval,exitFlag,output,population,scores,FitnessFcn,nvars,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in genetic_algorithm (line 55)
[x_ga_opt, err_ga] = ga(h, 3*n+1, ga_opts);
Please help me to solve this error

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 4 月 3 日
Use xlsread to get the data from excel. Assign a portion of it to the variable name "inputs" and another portion to "targets" and remove the two assignments to those variables in the sample script .
  1 件のコメント
Priya Dharshini
Priya Dharshini 2016 年 4 月 3 日
編集済み: Walter Roberson 2016 年 4 月 4 日
I have already used xlsread to get the data from excel like this
% XY=xlsread('Book1');
X=xlsread('data_flood','Sheet5','A1:C43');
% targets for the neural net
Y=xlsread('data_flood','Sheet6');
but if i do so i'm getting error like this
Index exceeds matrix dimensions.
Error in setwb (line 32)
net.IW{i,j}(:) = x(inputWeightInd{i,j});
Error in rmse_test (line 17)
net = setwb(net, x');
Error in @(x)rmse_test(x,net,X,Y)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in fcnvectorizer (line 14)
y(i,:) = feval(fun,(pop(i,:)));
Error in makeState (line 47)
Score = fcnvectorizer(state.Population(initScoreProvided+1:end,:),FitnessFcn,1,options.SerialUserFcn);
Error in gaunc (line 41)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 351)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in garefer (line 59)
[x_ga_opt, err_ga] = ga(h, 3*n+1, ga_opts);
Caused by:
Failure in user-supplied fitness function evaluation. GA cannot continue.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGenomics and Next Generation Sequencing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by