une vs toute régression
J'ai revu un exemple du parcours de Andrew Ng dans l'Apprentissage de la Machine que j'ai trouvé dans https://github.com/jcgillespie/Coursera-Machine-Learning/tree/master/ex3. L'exemple traite de la régression logistique et l'un-vs-toute classification. J'ai un doute à propos de cette fonction:
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
Je sais que le lrCostFunction prend comme paramètres: thêta, X, y et lambda, mais je ne peux pas le comprendre, à partir d'où la valeur de t vient de dans le code que j'ai posté ci-dessus; plus précisément dans cette partie:
[theta] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);
toute aide?
source d'informationauteur Little
Vous devez vous connecter pour publier un commentaire.
fmincg
prend la poignée de l'objectif de la fonction en tant que premier argument, qui dans ce cas est une poignée delrCostFunction
.Si vous allez à l'intérieur
fmincg.m
vous trouverez les lignes suivantes:À la fin de l'extrait de code ci-dessus, le résultat sera,
Si vous un peu à l'avance, vous le verrez,
Par conséquent, la fonction de poignée
f
fonctionnera avec un argumentX'
. Par conséquent,t=X'
ce qui est logique aussi. La premièretheta
convergent pour vous donner le dernier paramètre vecteur de régression logistique.Vous pouvez effectivement remplacer.
essayer cette
vous n'avez pas besoin d'initialiser all_theta au début