Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: Meaning of @(t)(costFunction(t, X, y)) from Andrew Ng's Machine Learning class

Tags:

matlab

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 argument t, which calls your costFunction. This allows us to wrap the costFunction for use with fminunc.

I really cannot understand what @(t)(costFunction(t, X, y) means. What are the both ts are doing? What kind of expression is that?

like image 227
medi Avatar asked Mar 09 '23 02:03

medi


2 Answers

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

  • As mentioned in the comments, here is a link to generally parameterising functions.
  • Specifically, here is a documentation link about anonymous functions.
like image 194
Wolfie Avatar answered Apr 07 '23 09:04

Wolfie


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

like image 43
heretoinfinity Avatar answered Apr 07 '23 08:04

heretoinfinity