Calculate global maximum of a neural networks deployed function. Problem with function handle indices
5 views (last 30 days)
Show older comments
I trained a nn with three output nodes and 14 input. Then I want to calculate the maximum of this function, in particular of the first output node using fmincon. When I have 1 output node it works perfect. However i try to get the first element (node) of the deployed function, but could not. The fit function (automatically created by matlab) is of ths form
function [Y,Xf,Af] = netFcn(X,~,~)
In 1-node-output this works with
fun=@netFcn;
fun_max = @(z)-1*fun(z);
and then i minimize fun_max.
For multiple outputs i used this
fun_max3 = @(z)-1*fun{1}(z);
to get he first element but did not work. The error is
Cell contents reference from a non-cell array object.
Error in @(z)-1*fun{1}(z)
I even tried multiobjective optimisaion using
[min,Fval,exitFlag,Output] = gamultiobj(fun_max,14,A,b,Aeq,beq,transpose(lb),transpose(ub),options);
but it returns
Subscripted assignment dimension mismatch.
Any ideas?
Thanks
0 Comments
Answers (1)
Walter Roberson
on 5 Jan 2016
You define fun as an individual function handle. You then try to use fun{1}(z) . That is invalid because fun is not a cell array. It is allowed to have a cell array of function handles and index the cell array and invoke the resulting handle with an argument. For example if
fun = {@sin, @cos, @tan}
then fun{1} would extract an individual function handle from that cell array, ready to be called like fun{1}(x) being equivalent to calling sin(x) . But it requires a cell array of handles, not a scalar function handle.
But I gather that what you are trying to do is invoke f(z) and get a particular output number from the result of that call -- the first output in this case.
The first output turns out to be special: if you use a function that returns multiple outputs in an expression then in nearly all cases the outputs after the first will be discarded, so
fun_max3 = @(z)-1*fun(z);
would have the -1* operating on the first output of fun(z)
If you wanted to select anything other than the first output of fun(z) then the only way to do so is to use an assignment statement to assign the results to a variable that can be referred to. This will require at least one "real" (non-anonymous) function such as
function r = outN(func,N,varargin)
Nout = max(abs(nargout(func)), N);
[outs{1:Nout}] = func(varargin{:});
r = outs{N};
Used such as
fun_max3 @(z) -1*outN(fun, 2, z);
There is no direct way of asking for any particular output of a function.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!