How does [theta, cost] = fminunc(@(​t)(costFun​ction(t, X, y, lambda)), startingTheta, options) work?

I have inserted a snippet of my code. I'm trying to understand exactly how this function works. I'm a beginner in machine learning so please try to walk me through it.
%My code
% Set options for fminunc, maximum iterations allowed is 500
options = optimset('MaxIter', 500);
% fminunc()
% Optimization function that finds the min point of cost function J
fprintf('Run fminunc');
[theta, cost] = fminunc(@(t)(costFunction(t, X, y, lambda)), startingTheta, options);
fprintf('Done \n');
fprintf('Theta: %f \n', theta);
fprintf('Cost at theta found by fminunc: %f\n', cost);
On stack exchange, I found an answer that stated
X = 1; y = 1; % Defining variables which aren't passed into the costFunction
% but which must exist for the next line to pass them as anything!
f = @(t)(costFunction(t, X, y)); % Explicitly define costFunction as a function of t alone
[theta, cost] = fminunc(f, 0, options);
which confused me even more. My code works, but i want to completely understand the usage of this function. My exact question is, what values are being passed into t, X, and y? Nowhere in my code do i define t to equal anything and the code runs fine, how? The user i quoted above also stated the X and y variables arent passed into the costFunction... How does this function run without any values being passed into its parameters? Any help for a beginner would be greatly appreciated, thanks!

1 Comment

"On stack exchange, I found an answer that stated ..." more half-baked MATLAB-related advice on stack exchange. Whilst interesting to read and for getting ideas, stack exchange is not a particularly good resource for accurate descriptions of how MATLAB works. For that you should read the documentation, the blogs, and this forum.

Sign in to comment.

 Accepted Answer

fminunc is an optimization function; it takes a function handle as input and returns an optimal vector, which minimizes the function. The function handle should have this property: It should be accepted only a single vector, and output a scalar.
Now, considering your code, since you didn't explicitly specified how is costFunction() defined, so I am going to guess that 't' is the optimization variable, while X and y are known variables. Since 't' is an optimization variable, it is considered unknown. You don't need to define it in your code. In this line
f = @(t)(costFunction(t, X, y));
you are defining a function handle that takes 't' as input. The variable 't' only holds meaning on this line. As far as function handle is concerned, you can use any variable name, and MATLAB does not care
f = @(a)(costFunction(a, X, y));
is equivalent to the first function handle.
Now coming to your questions.
"My exact question is, what values are being passed into t, X, and y?"
X and y are constant, and its value is the same as you define in your code. The value of 't' is passed by the fminunc itself. fminunc will pass different values to costFunction, according to its internal logic, until it finds the optimal vector 't'.
" The user i quoted above also stated the X and y variables arent passed into the costFunction"
I am sure that is not true. The value of X and y are passed into the costFunction(). They take the constant value, as you defined in your code.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!