Nonlinear function approximation with the use of neural networks with a GA

8 views (last 30 days)
Caroline
Caroline on 18 Sep 2020
Answered: Nomit Jangid on 21 Sep 2020
I'm just starting to work with neural networks and I think I understand the theory, but I don't know how to apply it in practice. I did this exemplary example and the result does not match the function, why? I would also like to compare different models of neural networks
inputs = (1:10);
targets = cos(inputs.^2);
n = 2;
net = feedforwardnet(n);
net = configure(net, inputs, targets);
h = @(x) mse_test(x, net, inputs, targets);
ga_opts = gaoptimset('TolFun', 1e-8,'display','iter');
[x_ga_opt, err_ga] = ga(h, 3*n+1, ga_opts);
net = setwb(net, x_ga_opt);
% 1 model
net.trainFcn = 'trainlm';
net1 = train(net,inputs,targets);
w1 = net1(targets);
plot (inputs,w1)
hold on
plot (inputs,targets);
% 2 model
net.trainFcn = 'traingda';
net2 = train(net,inputs,targets);
w2 = net2(targets);
plot (inputs,w2)
hold on
plot (inputs,targets);
%%%
function mse_calc = mse_test(x, net, inputs, targets)
net = setwb(net, x');
y = net(inputs);
mse_calc = sum((y-targets).^2)/length(y);
end

Answers (1)

Nomit Jangid
Nomit Jangid on 21 Sep 2020
In my understanding, you're trying to train neural networks with one hidden layer (containing 2 neurons). You’re using genetic algorithm to optimize your neural networks. If you’re using MATLAB version R2018b or later, I'd suggest using "optimoptions" instead of "gaoptimset". The only difference between using “optimoptions” and the other functions is, for “optimoptions”, you can include the solver name as the first argument. For example, to set iterative display in ga,
ga_opts = optimoptions('ga','Display','iter');
%instead of
ga_opts = gaoptimset('Display','iter');
By executing this function, "net" will be be optimized such that the mean squared error is minimized
Also, I am sharing a few links which will help you start practising Deep Learning in MATLAB with a few hands-on session and theory as well.

Community Treasure Hunt

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

Start Hunting!