Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue.
Show older comments
Hey there,
after reading a few other questions and answers I tried to solve my problem but I didn't get really far. I am trying to optimise the weights of a neural network with the use of fminunc. I have this code which gives me the function which should be minimised and it's gradient:
function [C,nabla] = Backpropagation1(X,W)
tic
A=reshape(W(1:42),6,7); B=reshape(W(43:60),3,6); D=W(61:63)'; a0=W(64:69); b0=W(70:72); d0=W(73);
%--------------------------------------------------------------------------
%Evaluate the cost function:
C=Cost_B(X,A,B,D,a0,b0,d0);
%--------------------------------------------------------------------------
%Generate the derivatives for each weight: %The structure was chosen as follows: the partial derivative of the cost %function in the direction of A is given by delta_A and so on. %The derivatives are already reshaped into a vector if necessary and given %by:
if nargout > 1
e=size(A); g=size(B);
delta_D = Derivative_D(X,A,B,D,a0,b0,d0);
delta_d0 = Derivative_d0(X,A,B,D,a0,b0,d0);
delta_B = reshape(Derivative_B(X,A,B,D,a0,b0,d0),1,g(1)*g(2));
delta_b0 = Derivative_b0(X,A,B,D,a0,b0,d0)';
delta_A = reshape(Derivative_A(X,A,B,D,a0,b0,d0),1,e(1)*e(2));
delta_a0 = Derivative_a0(X,A,B,D,a0,b0,d0)';
%Then the gradient of the cost function is given by:
nabla =[delta_D, delta_d0, delta_B, delta_b0, delta_A, delta_a0];
end
toc
end
This individually works with every function included. If i now try to use fminunc with doing the following:
x0=ones(1,73); fun=@(W)Backpropagation1(X,W); options = optimoptions('fminunc','Algorithm','trust-region','GradObj','on'); x=fminunc(fun,x0,options)
I get the following error: Error using horzcat Dimensions of matrices being concatenated are not consistent.
Error in Backpropagation1 (line 35) nabla =[delta_D, delta_d0, delta_B, delta_b0, delta_A, delta_a0];
Error in @(W)Backpropagation1(X,W)
Error in fminunc (line 271) [f,GRAD] = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue.
Does maybe anyone have an idea what I am doing wrong? Thanks in Advance!
1 Comment
Alan Weiss
on 16 Mar 2017
I don't know anything about your application, but it seems that the gradient matrices do not have the same number of rows, so the concatenation fails. I'd set a breakpoint in the code at the failing line
nabla =[delta_D, delta_d0, delta_B, delta_b0, delta_A, delta_a0];
and check the sizes of all the matrices.
Alan Weiss
MATLAB mathematical toolbox documentation
Answers (0)
Categories
Find more on Solver-Based Nonlinear Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!