0

I tried to perform an optimization using a neural network and the genetic algorithm. I trained a neural network with input p (4x72 matrix) and target t (2x72 matrix). Regrading the optimization using genetic algorithm, I used the sim function of the neural network as the fitness function. The code I used for it is as follows:

objFcn=@(p) sim(net,p');
%'net' is the neural network I created with p as input and t as target
[xOpt,fVal,exitflag,target]=ga(objFcn,4,[],[],[],[],LB,UB,[],options);

I have provided the LB and UB which are lower bound and upper bounds, respectively. And options, I tried it with

options = gaoptimset('Vectorized','on');
% even vectorized off doesnt solve the problem

Logically, as I used p' in the sim command, the resultant matrix would be 72x2 which is the same as the population for GA. But for some reason, I always get the error saying 'Your fitness function must return a scalar value'.

Please guide me to solve this problem.

1 Answers1

0

The "sim" function returns a matrix with all outputs from your network. You need to compute the squared error from that in order to provide a scalar value to minimize with the GA.

rcpinto
  • 4,166
  • 2
  • 24
  • 25
  • do you mean by calculating mean squared error, i end up in a 1x1 matrix which obviously becomes a scalar? I tried it with just1 output and the method I said works perfect. Since I am considering 3 outputs, I am not quite sure how to proceed. – Bharathwaj Krishnamoorthi Apr 05 '16 at 10:55
  • Icreated a function mse_c with the code as follows: function mse_calc = mse_c(net, p, t) y=sim(net,p); mse_calc = sum((y-t).^2)/length(y); end This function returns a 72x1 matrix. But to feed it as a fitness function in GA, it has to be 1x72 as far as i know. The transpose sign would render some error in the function. – Bharathwaj Krishnamoorthi Apr 05 '16 at 13:28