对 One vs. All Regression 中函数句柄的混淆
Confusion about function handles in One vs. All Regression
我正在 coursera 上学习 Andrew Ng 的机器学习课程,我很困惑为什么一个特定的例子在 One vs. All 分类中有效:
function [all_theta] = oneVsAll(X, y, num_labels, lambda)
%ONEVSALL trains multiple logistic regression classifiers and returns all
%the classifiers in a matrix all_theta, where the i-th row of all_theta
%corresponds to the classifier for label i
% [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels
% logisitc regression classifiers and returns each of these classifiers
% in a matrix all_theta, where the i-th row of all_theta corresponds
% to the classifier for label i
% Some useful variables
m = size(X, 1);
n = size(X, 2);
% You need to return the following variables correctly
all_theta = zeros(num_labels, n + 1);
% Add ones to the X data matrix
X = [ones(m, 1) X];
% ====================== YOUR CODE HERE ======================
% Instructions: You should complete the following code to train num_labels
% logistic regression classifiers with regularization
% parameter lambda.
%
% Hint: theta(:) will return a column vector.
%
% Hint: You can use y == c to obtain a vector of 1's and 0's that tell use
% whether the ground truth is true/false for this class.
%
% Note: For this assignment, we recommend using fmincg to optimize the cost
% function. It is okay to use a for-loop (for c = 1:num_labels) to
% loop over the different classes.
%
% fmincg works similarly to fminunc, but is more efficient when we
% are dealing with large number of parameters.
%
% Example Code for fmincg:
%
% % Set Initial theta
% initial_theta = zeros(n + 1, 1);
%
% % Set options for fminunc
% options = optimset('GradObj', 'on', 'MaxIter', 50);
%
% % Run fmincg to obtain the optimal theta
% % This function will return theta and the cost
% [theta] = ...
% fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ...
% initial_theta, options);
%
initial_theta = zeros(n + 1, 1);
options = optimset('GradObj', 'on', 'MaxIter', 50);
for i = 1:num_labels
c = i * ones(size(y));
fprintf('valores')
[theta] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);
all_theta(i,:) = theta;
end
% =========================================================================
end
我对以下行感到特别困惑:[theta] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);
。 lrCostFunction
被定义为具有参数 theta, X, y, lambda
,所以我不知道 t
在那里做什么。
此外,将 theta
括在方括号中的目的是什么:[theta]
?
任何有关单步执行此代码的帮助都将非常有用。谢谢。
您正在查看定义匿名函数的行。匿名函数就像函数的简写定义,以 @
开头,后跟此函数的参数(在您的例子中为 t
)。这个参数t
作为第一个参数传递给函数lrCostFunction()
,实际上是theta
参数。即,您要求函数 fmincg()
最小化此匿名函数的输出,它是 lrCostFunction()
的包装器,以便您在使用 X
、[=19= 时最小化 theta ] 和 lambda
在匿名函数定义之外定义。
为了更好的理解匿名函数,可以拆分代码:
func_handle = @(t)(lrCostFunction(t, X, (y == c), lambda)) % anonymous function
func_handle(initial_theta); % returns the cost at the initial_theta
[theta] = fmincg(func_handle, initial_theta, options);
查看官方Matlab documentation了解匿名函数的详细信息
theta
两边的括号是多余的。
我正在 coursera 上学习 Andrew Ng 的机器学习课程,我很困惑为什么一个特定的例子在 One vs. All 分类中有效:
function [all_theta] = oneVsAll(X, y, num_labels, lambda)
%ONEVSALL trains multiple logistic regression classifiers and returns all
%the classifiers in a matrix all_theta, where the i-th row of all_theta
%corresponds to the classifier for label i
% [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels
% logisitc regression classifiers and returns each of these classifiers
% in a matrix all_theta, where the i-th row of all_theta corresponds
% to the classifier for label i
% Some useful variables
m = size(X, 1);
n = size(X, 2);
% You need to return the following variables correctly
all_theta = zeros(num_labels, n + 1);
% Add ones to the X data matrix
X = [ones(m, 1) X];
% ====================== YOUR CODE HERE ======================
% Instructions: You should complete the following code to train num_labels
% logistic regression classifiers with regularization
% parameter lambda.
%
% Hint: theta(:) will return a column vector.
%
% Hint: You can use y == c to obtain a vector of 1's and 0's that tell use
% whether the ground truth is true/false for this class.
%
% Note: For this assignment, we recommend using fmincg to optimize the cost
% function. It is okay to use a for-loop (for c = 1:num_labels) to
% loop over the different classes.
%
% fmincg works similarly to fminunc, but is more efficient when we
% are dealing with large number of parameters.
%
% Example Code for fmincg:
%
% % Set Initial theta
% initial_theta = zeros(n + 1, 1);
%
% % Set options for fminunc
% options = optimset('GradObj', 'on', 'MaxIter', 50);
%
% % Run fmincg to obtain the optimal theta
% % This function will return theta and the cost
% [theta] = ...
% fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ...
% initial_theta, options);
%
initial_theta = zeros(n + 1, 1);
options = optimset('GradObj', 'on', 'MaxIter', 50);
for i = 1:num_labels
c = i * ones(size(y));
fprintf('valores')
[theta] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);
all_theta(i,:) = theta;
end
% =========================================================================
end
我对以下行感到特别困惑:[theta] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);
。 lrCostFunction
被定义为具有参数 theta, X, y, lambda
,所以我不知道 t
在那里做什么。
此外,将 theta
括在方括号中的目的是什么:[theta]
?
任何有关单步执行此代码的帮助都将非常有用。谢谢。
您正在查看定义匿名函数的行。匿名函数就像函数的简写定义,以 @
开头,后跟此函数的参数(在您的例子中为 t
)。这个参数t
作为第一个参数传递给函数lrCostFunction()
,实际上是theta
参数。即,您要求函数 fmincg()
最小化此匿名函数的输出,它是 lrCostFunction()
的包装器,以便您在使用 X
、[=19= 时最小化 theta ] 和 lambda
在匿名函数定义之外定义。
为了更好的理解匿名函数,可以拆分代码:
func_handle = @(t)(lrCostFunction(t, X, (y == c), lambda)) % anonymous function
func_handle(initial_theta); % returns the cost at the initial_theta
[theta] = fmincg(func_handle, initial_theta, options);
查看官方Matlab documentation了解匿名函数的详细信息
theta
两边的括号是多余的。