-
Notifications
You must be signed in to change notification settings - Fork 17
/
classf_lr_tr.m
63 lines (52 loc) · 2.11 KB
/
classf_lr_tr.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function [model] = classf_lr_tr(X,Y,param)
%Train a logistic regression classfier
%
% Weighted regularized logistic regression, one vs. all for multi-class
% problems (this strategy is often better than softmax).
% X : each row is a sample.
% Y : a column vector, class labels for X starting from 1.
% PARAM: struct of parameters. The beginning part of this code (before
% defParam) explains each parameter, and also sets the default parameters.
% You can change parameter p to x by setting PARAM.p = x. For parameters
% that are not set, default values will be used.
% Return:
% MODEL: a struct containing coefficients.
%
% Ke YAN, 2016, Tsinghua Univ. http://yanke23.com, [email protected]
[nSmp,nFt] = size(X);
ftPenal = ones(1,nFt); % penalization weight of each feature. The larger,
% the feature will be less relied in the model. If you do not know the
% importance of features in prior, just set to all ones.
lambda = 0; % regularization parameter
nIter = 100; % number of iteration. Should be larger if nSmp or nFt is large
defParam
X = [ones(nSmp,1),X]; % add constant column
nCls = max(Y);
initTheta = zeros(nFt+1,1);
options = optimset('GradObj','on','MaxIter',nIter);
fmin = @fmincg; % fminunc is very slow
% similar to fmincg
% options = struct('GradObj','on','MaxIter',param.nIter,'Display','off',...
% 'Method','cg','DerivativeCheck','off');
% fmin = @minFunc;
% warning off
if nCls == 2 % only compute one theta, will be faster
f = @(t)lrCostFunction(t,X,(Y==1),lambda*ftPenal); % the 3rd para should be 0 or 1
[thetas,fh] = fmin(f,initTheta,options);
else
thetas = zeros(nFt+1,nCls); % one vs all
for p = 1:nCls
f = @(t)lrCostFunction(t,X,(Y==p),lambda*ftPenal); % the 3rd para should be 0 or 1
thetas(:,p) = fmin(f,initTheta,options);
end
end
model.thetas = thetas;
end
function [J, grad] = lrCostFunction(theta, X, Y, reguTerm)
smpNum = length(Y);
hyp = sigmoid(X*theta); % hypothesis
penalTerm = sum(theta(2:end).^2 .* reguTerm')/2/smpNum;
J = -sum(Y.*log(hyp)+(1-Y).*log(1-hyp)) / smpNum + penalTerm;
penalTermG = theta.*[0;reguTerm']/smpNum; % penalty for theta(1) is 0
grad = X'*(hyp-Y)/smpNum + penalTermG;
end