How to optimize patternnet using a genetic algorithm

3 views (last 30 days)
This is how to optimize weights of feedforwardnet but I dont know how to do this for about patternnet please help
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);
  2 Comments
Randy Souza
Randy Souza on 11 Mar 2013
I have restored the original text of this question.
Syed, this question has a clear subject and an accepted answer, so it may be valuable to someone else in the future. If you have a good reason why it should be removed from MATLAB Answers, please flag the question, explain why it should be deleted, and an administrator or high-reputation contributor will consider deleting the question. Please do not simply edit your question away.
aleksandar
aleksandar on 8 Oct 2014
Hello, I'm new to this field and I'm sorry if my question is not appropriate, I would like to ask is it possible to modify this mse_calc function, so it can be used for the matrix of inputs not for the row, in that way there is much more weights (neurons *columns)? Best Regards

Sign in to comment.

Accepted Answer

Greg Heath
Greg Heath on 7 Feb 2013
Edited: Randy Souza on 11 Mar 2013
See my three replies to your question in this Newsgroup thread: ga optimization of nn weights
Hope this helps.
Thank you for formally accepting my answer!!!
Greg
  1 Comment
RAJA SEKHAR BATTU
RAJA SEKHAR BATTU on 13 Dec 2022
@Greg Heath Hi Greg, this link doesn't work. can you please provide any other link or answer.
Thanks in advance.
Best wishes,
Raja

Sign in to comment.

More Answers (0)

Categories

Find more on Deep Learning Toolbox 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!