I have the following code in MATLAB:
% Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
My instructor has explained the minimising function like so:
To specify the actual function we are minimizing, we use a "short-hand" for specifying functions, like
@(t)(costFunction(t, X, y))
. This creates a function, with argumentt
, which calls yourcostFunction
. This allows us to wrap thecostFunction
for use withfminunc
.
I really cannot understand what @(t)(costFunction(t, X, y)
means. What are the both t
s are doing? What kind of expression is that?
In Matlab, this is called an anonymous function.
Take the following line:
f = @(t)( 10*t );
Here, we are defining a function f
, which takes one argument t
, and returns 10*t
. It can be used by
f(5) % returns 50
In your case, you are using fminunc
which takes a function as its first argument, with one parameter to minimise over. This could be called using
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);
This can be shortened by not defining f
first, and just calling
[theta, cost] = fminunc(@(t)(costFunction(t, X, y)), 0, options);
Further reading
Just adding to Wolfie's response. I was confused as well and asked a similar question here: Understanding fminunc arguments and anonymous functions, function handlers
The approach here is one of 3. The problem the anonymous function (1 of the 3 approaches in the link below) solves is that the solver, fminunc
only optimizes one argument in the function passed to it. The anonymous function @(t)(costFunction(t, X, y)
is a new function that takes in only one argument, t, and later passes this value to costFunction. You will notice that in the video lecture what was entered was just @costFunction
and this worked because costFunction
only took one argument, theta
.
https://www.mathworks.com/help/optim/ug/passing-extra-parameters.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With