2015-05-16 17 views
8

मैं मशीन लर्निंग में एंड्रयू एनजी के पाठ्यक्रम से एक उदाहरण की समीक्षा कर रहा हूं जिसे मैंने 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] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options); 

कोई मदद?

उत्तर

3

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 

इसलिए, फ़ंक्शन हैंडल fX' पर तर्क के साथ चलाएगा। इसलिए, t=X', जो भी समझ में आता है। शुरुआती theta आपको लॉजिस्टिक रिग्रेशन के लिए अंतिम पैरामीटर वेक्टर देने के लिए अभिसरण करेगा।

3

आप वास्तव में प्रतिस्थापित कर सकते हैं।

for i=1 : num_labels 

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

all_theta(i,:)=theta; 
+1

उचित स्वरूपण –

+2

उपयोग उचित स्वरूपण उपयोग में all_theta प्रारंभ करने की जरूरत नहीं है की कोशिश करो। – Sandeep

+1

[theta] क्या करता है? क्यों नहीं थे (i)? या अगर यह सिर्फ असाइनमेंट के लिए है, तो ब्रैकेट बिल्कुल क्यों? – nights

1

इस

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

आप भी शुरुआत

संबंधित मुद्दे