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

Viewed 2300

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?

4 Answers

I also had the same question. All thanks to the the link provided by Wolfie to understand paramterized and anonymous functions, I was able to clarify my doubts. Perhaps, you must have already found your answer but am explaining once again, for people who might develop this query in the mere future.

Let's say we want to derive a polynomial, and find its minimum/maximum value. Our code is:

m = 5;
fun = @(x) x^2 + m; % function that takes one input: x, accepts 'm' as constant
x = derive(fun, 0); % fun passed as an argument

As per the above code, 'fun' is a handle that points to our anonymous function, f(x)=x^2 + m. It accepts only one input, i.e. x. The advantage of an anonymous function is, one doesn't need to create a separate program for it. For the constant, 'm', it can accept any values residing in the current workspace.

The above code can be shortened by:

m = 5;
x = derive(@(x) x^2 + m, 0); % passed the anonymous function directly as argument

Our target is to find the global optimal,so i think the function here is to get a bounch of local minimal by change the alpha and compare with each other to see which one is the best.

to achive this you initiate the fminuc with value initial_theta

  1. fminuc set t=initial_theta then compute CostFunction(t,X,y) which is equal to` CostFunction(initial_theta,X,y).you will get the Cost and also the gradient.

  2. fminuc will compute a new_theta with the gradient and a alpha, then set t=new_theta and compute the Cost and gradient again.

  3. it will loop like this until it find the local optimal.

  4. Then it change the length of alpha and repeat above to get another optimal. At the end it will compare the optimals and return with the best one.

Related