Error of Generated fitness function by Neural Network Fitting Tool for Genetic Algorithm: your fitness function must return a scalar value

1 view (last 30 days)
Hi Guys. I am dummy about Matlab programming and code writing and I just know about toolboxes. I have a problem with the fitness function generated by Neural Network Fitting toolbox for GA Optimization toolbox in MATLAB R2015. I have developed a neural network through nftool and then generated the ANN as a cost function for Genetic Algorithm for being called through Global Optimization Toolbox. I saved this code as a Mat-file and called it with @ in GA toolbox but the GA tool box gives this error: "Your fitness function must return a scalar value."
Please kindly help me as this is very urgent for my PhD. Please find attached the generated code.

Accepted Answer

Walter Roberson
Walter Roberson on 7 Oct 2015
Your code is written to be vectorized, taking a 7 x Q input and returning a 1 x Q result. However, if you examine http://www.mathworks.com/help/gads/vectorizing-the-fitness-function.html you will see that if you turn on the vectorized option you need to pass in a matrix with an arbitrary number of rows and read the samples out of the columns -- in other words it would pass in a Q x 7 matrix rather than a 7 x Q matrix.
You do not show the code that you invoke the function with, but if you currently have
ga(@myNeuralNetworkFunction, 7, something)
then you should change it to
options = gaoptiset('Vectorized', 'on');
ga(@(X) myNeuralNetworkFunction(X.').', 7, something, options);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!