问题 一对一回归


我一直在回顾一下我发现的机器学习中Andrew Ng的一个例子 https://github.com/jcgillespie/Coursera-Machine-Learning/tree/master/ex3。该示例处理逻辑回归和一对一分类。我对这个功能有疑问:

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

我知道lrCostFunction作为参数:theta,X,y和lambda,但是我无法从中找出它的值 Ť 来自我上面发布的代码;特别是在这部分:

[theta] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);

任何帮助?


10714
2018-05-16 21:21


起源



答案:


fmincg 将目标函数的句柄作为第一个参数,在本例中是一个句柄 lrCostFunction

如果你进去 fmincg.m,你会发现以下几行:

argstr = ['feval(f, X'];                      % compose string used to call function

%---Code will not enter the following loop---%
for i = 1:(nargin - 3) %this will go from 1 to 0, thus the loop is skipped
   argstr = [argstr, ',P', int2str(i)];
end
% following will be executed
argstr = [argstr, ')'];

在上面的代码片段结尾处,结果将是,

argstr=feval(f,X');

如果你领先一点,你会看到,

[f1 df1] = eval(argstr);                      % get function value and gradient

因此,功能句柄 f 将以参数运行 X'。因此, t=X',这也是有道理的。最初的 theta 将收敛以给出逻辑回归的最终参数向量。


4
2018-05-16 21:52





你实际上可以替代。

for i=1 : num_labels

    [theta]= fmincg (@(t)(lrCostFunction(t, X, (y == i), lambda)),initial_theta, options);

all_theta(i,:)=theta;

4
2018-06-11 07:20



使用正确的格式 - Hamza Zafeer
使用正确的格式。 - Sandeep
这有什么作用?为什么不theta(i)?或者如果只是为了分配,为什么括号呢? - nights
我想如果我们写的话会没问题 THETA 在...的地方 的2θ - X_Stark


答案:


fmincg 将目标函数的句柄作为第一个参数,在本例中是一个句柄 lrCostFunction

如果你进去 fmincg.m,你会发现以下几行:

argstr = ['feval(f, X'];                      % compose string used to call function

%---Code will not enter the following loop---%
for i = 1:(nargin - 3) %this will go from 1 to 0, thus the loop is skipped
   argstr = [argstr, ',P', int2str(i)];
end
% following will be executed
argstr = [argstr, ')'];

在上面的代码片段结尾处,结果将是,

argstr=feval(f,X');

如果你领先一点,你会看到,

[f1 df1] = eval(argstr);                      % get function value and gradient

因此,功能句柄 f 将以参数运行 X'。因此, t=X',这也是有道理的。最初的 theta 将收敛以给出逻辑回归的最终参数向量。


4
2018-05-16 21:52





你实际上可以替代。

for i=1 : num_labels

    [theta]= fmincg (@(t)(lrCostFunction(t, X, (y == i), lambda)),initial_theta, options);

all_theta(i,:)=theta;

4
2018-06-11 07:20



使用正确的格式 - Hamza Zafeer
使用正确的格式。 - Sandeep
这有什么作用?为什么不theta(i)?或者如果只是为了分配,为什么括号呢? - nights
我想如果我们写的话会没问题 THETA 在...的地方 的2θ - X_Stark


尝试这个

for i = 1:num_labels,
    [all_theta(i,:)] = fmincg (@(t)(lrCostFunction(t, X, (y == i), lambda)), initial_theta, options);
end;

你也不需要在开始时初始化all_theta


3
2017-08-05 06:05