I am trying to run ‘fminunc’ multiple times in for loop.
Part of code:
P = 0.70 ; idx = randperm(m); X_Training = X(idx(1:round(P*m)),:); X_Testing = X(idx(round(P*m)+1:end),:); y_Traning = y(idx(1:round(P*m)),:); y_Testing = y(idx(round(P*m)+1:end),:); s = 100; border_all = zeros( 2, s ); lambda = 1; accuracy = 0; initial_theta = zeros( n + 1, 1 ); options = optimset( 'GradObj', 'on', 'MaxIter', 400 ); for i = 1 : 1 : s [theta, J, exit_flag] = fminunc( @(t) costFunctionReg( t, X_Training, y_Traning, lambda ), initial_theta, options ); for border = 0.01 : 0.01 : 1 p = predict( theta, X_Testing, border ); accuracy_t = mean( double( p == y_Testing ) ) * 100; if accuracy_t > accuracy accuracy = accuracy_t; border_result = border; endif endfor border_all(1, i) = border_result; border_all(2, i) = accuracy; endfor
I use the output values of “theta” later to check different changing variable “i” (for i=1:1:s..).
Every time I run the script “theta” is calculated, but it does not change in other “for loop” iterations. However, it should be a bit different in every iteration.
At the same time, when I rerun the script, I get new values of “theta”. So, it seems that it is calculated from scratch only at the first iteration and not at others. Perhaps some memory issue. Have tried ‘clear’ function, but the result is the same.
Has anyone faced the same problem?