Calculate global maximum of a neural networks deployed function. Problem with function handle indices

5 views (last 30 days)
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

Answers (1)

Walter Roberson
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.
  1 Comment
NIKOLAOS BAKAS
NIKOLAOS BAKAS on 5 Jan 2016
Edited: NIKOLAOS BAKAS on 5 Jan 2016
Dear Mr. Roberon thank you for your answer
when I try to invoke the first output of fun3, unfortunately, it returns all three outputs
fun_max3 = @(z)-1*fun(z); Trial>> fun_max3(transpose(x0))
ans =
1.0e+03 *
-1.0664
-0.0058
-0.0601
1)does it mean that fun_max3 is a a cell array of function handles? If so, why i cannot get someway ony the first output? 2)this is the reson why fmincon hit error, as it asks for a scalar n0t a vector
initial function netFcn is like this:
function [Y,Xf,Af] = netFcn(X,~,~) %NETFCN neural network simulation function. % % Generated by Neural Network Toolbox function genFunction, 05-Jan-2016 04:50:36. % % [Y] = netFcn(X,~,~) takes these arguments: % % X = 1xTS cell, 1 inputs over TS timesteps % Each X{1,ts} = 14xQ matrix, input #1 at timestep ts. % % and returns: % Y = 1xTS cell of 1 outputs over TS timesteps. % Each Y{1,ts} = 3xQ matrix, output #1 at timestep ts. % % where Q is number of samples (or series) and TS is the number of timesteps.
also, [outs{1:3}] = fun_max(varargin{:}); returns error: Undefined variable "varargin" or class "varargin".
thank you for your time!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!