1
0
Fork 0

Train num_labels one-vs-all logistic regression classifiers

master
neingeist 10 years ago
parent 9117809537
commit cf0d25440c

@ -1,17 +1,17 @@
function [all_theta] = oneVsAll(X, y, num_labels, lambda) function [all_theta] = oneVsAll(X, y, num_labels, lambda)
%ONEVSALL trains multiple logistic regression classifiers and returns all %ONEVSALL trains multiple logistic regression classifiers and returns all
%the classifiers in a matrix all_theta, where the i-th row of all_theta %the classifiers in a matrix all_theta, where the i-th row of all_theta
%corresponds to the classifier for label i %corresponds to the classifier for label i
% [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels % [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels
% logisitc regression classifiers and returns each of these classifiers % logisitc regression classifiers and returns each of these classifiers
% in a matrix all_theta, where the i-th row of all_theta corresponds % in a matrix all_theta, where the i-th row of all_theta corresponds
% to the classifier for label i % to the classifier for label i
% Some useful variables % Some useful variables
m = size(X, 1); m = size(X, 1);
n = size(X, 2); n = size(X, 2);
% You need to return the following variables correctly % You need to return the following variables correctly
all_theta = zeros(num_labels, n + 1); all_theta = zeros(num_labels, n + 1);
% Add ones to the X data matrix % Add ones to the X data matrix
@ -20,11 +20,11 @@ X = [ones(m, 1) X];
% ====================== YOUR CODE HERE ====================== % ====================== YOUR CODE HERE ======================
% Instructions: You should complete the following code to train num_labels % Instructions: You should complete the following code to train num_labels
% logistic regression classifiers with regularization % logistic regression classifiers with regularization
% parameter lambda. % parameter lambda.
% %
% Hint: theta(:) will return a column vector. % 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 % 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. % whether the ground truth is true/false for this class.
% %
% Note: For this assignment, we recommend using fmincg to optimize the cost % Note: For this assignment, we recommend using fmincg to optimize the cost
@ -38,29 +38,31 @@ X = [ones(m, 1) X];
% %
% % Set Initial theta % % Set Initial theta
% initial_theta = zeros(n + 1, 1); % initial_theta = zeros(n + 1, 1);
% %
% % Set options for fminunc % % Set options for fminunc
% options = optimset('GradObj', 'on', 'MaxIter', 50); % options = optimset('GradObj', 'on', 'MaxIter', 50);
% %
% % Run fmincg to obtain the optimal theta % % Run fmincg to obtain the optimal theta
% % This function will return theta and the cost % % This function will return theta and the cost
% [theta] = ... % [theta] = ...
% fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ... % fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ...
% initial_theta, options); % initial_theta, options);
% %
for c = 1:num_labels
% Train a one-vs all classifier for this class c
initial_theta = zeros(n + 1, 1);
options = optimset('GradObj', 'on', 'MaxIter', 50);
[theta] = fmincg(@(t)(lrCostFunction(t, X, (y == c), lambda)),
initial_theta, options);
all_theta(c,:) = theta';
end
% ========================================================================= % =========================================================================
end end