The ability to set the algorithm to ga in the train function is not currently directly available in Neural Network Toolbox (as of R2017a at least).
To work around this issue, use the steps outlined below to optimize a neural network using a genetic algorithm.
The "ga" function requires a function handle as an input argument to which it passes a 1xN vector, where N is the number of variables in the system to be optimized.
For a neural network, the weights and biases are a Mx1 vector. These may be optimized using "ga".
A function can be written to accept the network, weights and biases, inputs and targets. This function may return the mean squared error based on the outputs and the targets as "ga" requires a function handle that only returns a scalar value.
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)
net = setwb(net, x');
y = net(inputs);
mse_calc = sum((y-targets).^2)/length(y);
The following code example describes a separate 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.\n
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);
Please note that the above example makes use of
, which was first introduced in R2010b.