Matlab:来自Andrew Ng的机器学习课程的@(t)(costFunction(t,X,y))的含义 [英] Matlab: Meaning of @(t)(costFunction(t, X, y)) from Andrew Ng's Machine Learning class

查看:576
本文介绍了Matlab:来自Andrew Ng的机器学习课程的@(t)(costFunction(t,X,y))的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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:

要指定我们要最小化的实际功能,我们使用简写" 用于指定功能,例如@(t)(costFunction(t, X, y)).这 创建一个带有参数t的函数,该函数调用您的costFunction.这 允许我们包装costFunction以便与fminunc一起使用.

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.

我真的不明白@(t)(costFunction(t, X, y)是什么意思.这两个t都在做什么?那是什么样的表情?

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

推荐答案

在Matlab中,这称为匿名函数.

In Matlab, this is called an anonymous function.

执行以下行:

f = @(t)( 10*t );

在这里,我们定义一个函数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

在您的情况下,您正在使用fminunc,它将功能作为其第一个参数,并使用一个参数来最大程度地减少误差.可以使用

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); 

可以通过不先定义f而是直接调用

This can be shortened by not defining f first, and just calling

 [theta, cost] = fminunc(@(t)(costFunction(t, X, y)), 0, options); 


进一步阅读

  • As mentioned in the comments, here is a link to generally parameterising functions.
  • Specifically, here is a documentation link about anonymous functions.

这篇关于Matlab:来自Andrew Ng的机器学习课程的@(t)(costFunction(t,X,y))的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆