Matlab:来自Andrew Ng的机器学习课程的@(t)(costFunction(t,X,y))的含义 [英] Matlab: Meaning of @(t)(costFunction(t, X, y)) from Andrew Ng's Machine Learning class
问题描述
我在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 argumentt
, which calls yourcostFunction
. This allows us to wrap thecostFunction
for use withfminunc
.
我真的不明白@(t)(costFunction(t, X, y)
是什么意思.这两个t
都在做什么?那是什么样的表情?
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?
推荐答案
在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屋!