Optimizing GRNN with Genetic Algorithm?

1 view (last 30 days)
Muhammad Irfan
Muhammad Irfan on 24 Apr 2014
Answered: Alan Weiss on 26 Oct 2016
How to optimize newgrnn function for best sigma (spread) parameter using genetic algorithm? I have an input matrix (x) of size 10x1895 (1895 rows with 10 independent variables) and an output Matrix (y) of size 1x1895 (1895 rows with 1 dependent variable). I create the GRNN as follow:
spread=0.5 nvars=10 net = newgrnn(x,y,spread) % a function handle h = @(sigma)mse_grnn(net, sigma, x, y)
% The function looks like this: function mse_calc = mse_grnn(net, sigma, inputs, targets) net = setwb(net, [sigma ; inputs; targets]); y = net(inputs); mse_calc = sum((y-targets).^2)/length(y); end
% then i am using GA to optimize sigma (spread factor for GRNN) as below:
opts = gaoptimset('PopulationSize', 40, 'Generations', 20, 'EliteCount', 6, 'TolFun', 1e-8, 'PlotFcns', @gaplotbestf);
[xbest, fbest] = ga(h, nvars, opts);
I get an error in the function about the matrix size mismatch. If i change the nvars (number of variables) to 1895 instead of 10, it all works fine. But logically its wrong, because there only 10 variable, 1895 is the number of data points. GA should find a set of 10 spread values, one for each variable.
Can you figure out if i am doing anything wrong. The dimensions of the matrix are according to the newgrnn structure as given in Matlab help. x is R-by-Q matrix of Q input vectors, i.e. 1895 and y is S-by-Q matrix of Q target class vectors.
Thanks Irfan
  1 Comment
Rana S.Chegani
Rana S.Chegani on 24 Oct 2016
Hi there, I know it's an old question, but have you found the question for it?

Sign in to comment.

Answers (1)

Alan Weiss
Alan Weiss on 26 Oct 2016
You are confusing the data and the control variables. Control variables are those variables that you want the solver to move in order to find a local optimum. You need to get all your control variables in one vector, usually called x. See Compute Objective Functions, and also see Passing Extra Parameters.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

Community Treasure Hunt

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

Start Hunting!