i want to optimize a neural network parameters by genetic algorithm and i get below code from matlab support, but the result is very poor even for a very simple function. how can i improve the result and is there any better code for my porpuse?
조회 수: 5 (최근 30일)
이전 댓글 표시
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(abs(y-targets)/length(y);
end
% INITIALIZE THE NEURAL NETWORK PROBLEM %
% inputs for the neural net
inputs = (-2:.01:2);
% targets for the neural net
targets = ((inputs).^2)-3;
% number of neurons
n = 4;
% 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('PopInitRange', [-1;1], 'TolFun', 1e-10,'display','iter');
ga_opts = gaoptimset(ga_opts, 'StallGenLimit', 100, 'FitnessLimit', 1e-5, 'Generations', 400);
% 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, err_ga] = ga(h, 3*n+1, ga_opts);
net = setwb(net, x');
댓글 수: 0
채택된 답변
Greg Heath
2016년 9월 23일
If you can improve the code please let me know.
Greg
댓글 수: 4
Greg Heath
2016년 9월 25일
편집: Walter Roberson
2016년 9월 25일
Walter,
I don't think so.
I think there is something wrong with the MATLAB code.
I have never seen it successfully demonstrated.
Greg
Darshana Abeyrathna
2016년 10월 9일
편집: Darshana Abeyrathna
2016년 10월 9일
Saeed,
Please share if you have found any solution. Because I have the same problem :(
추가 답변 (1개)
Don Mathis
2017년 6월 7일
편집: Don Mathis
2017년 6월 7일
In the code you posted, mse_test is not calculating mean squared error. Here is a corrected version that seems to work fine. Paste the whole thing into the MATLAB editor and run it:
% INITIALIZE THE NEURAL NETWORK PROBLEM %
% inputs for the neural net
inputs = (-2:.01:2);
% targets for the neural net
targets = ((inputs).^2)-3;
% number of neurons
n = 4;
% 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('PopInitRange', [-1;1], 'TolFun', 1e-10,'display','iter');
ga_opts = gaoptimset(ga_opts, 'StallGenLimit', 100, 'FitnessLimit', 1e-5, 'Generations', 400);
% 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, err_ga] = ga(h, 3*n+1, ga_opts);
net = setwb(net, x');
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
댓글 수: 1
Ammy
2018년 5월 15일
I have a similar problem , I want to add an objective function in the above code that I want to optimize with Particle swarm optimization problem, Is there any way ?
참고 항목
카테고리
Help Center 및 File Exchange에서 Traveling Salesman (TSP)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!