[95bb1e]: / SLEP_package_4.1 / Examples / L1 / example_LogisticR.m

Download this file

130 lines (107 with data), 3.8 kB

  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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
clear, clc;
% This is an example for running the function LogisticR
%
% Problem:
%
% min f(x,c) = - weight_i * log (p_i) + 1/2 * rsL2 * ||x||_2^2
% + rho * \|x\|_1
%
% a_i denotes a training sample,
% and a_i' corresponds to the i-th row of the data matrix A
%
% y_i (either 1 or -1) is the response
%
% p_i= 1/ (1+ exp(-y_i (x' * a_i + c) ) ) denotes the probability
%
% weight_i denotes the weight for the i-th sample
%
% For detailed description of the function, please refer to the Manual.
%
%% Related papers
%
% [1] Jun Liu and Jieping Ye, Efficient Euclidean Projections
% in Linear Time, ICML 2009.
%
% [2] Jun Liu and Jieping Ye, Sparse Learning with Efficient Euclidean
% Projections onto the L1 Ball, Technical Report ASU, 2008.
%
% [3] Jun Liu, Jianhui Chen, and Jieping Ye,
% Large-Scale Sparse Logistic Regression, KDD, 2009.
%
%% ------------ History --------------------
%
% First version on August 10, 2009.
%
% September 5, 2009: adaptive line search is added
%
% For any problem, please contact Jun Liu (j.liu@asu.edu)
cd ..
cd ..
root=cd;
addpath(genpath([root '/SLEP']));
% add the functions in the folder SLEP to the path
% change to the original folder
cd Examples/L1;
m=1000; n=1000; % The data matrix is of size m x n
randNum=2; % a random number
% ---------------------- generate random data ----------------------
randn('state',(randNum-1)*3+1);
A=randn(m,n); % the data matrix
randn('state',(randNum-1)*3+2);
xOrin=randn(n,1);
randn('state',(randNum-1)*3+3);
y=[ones(n/2,1);...
-ones(n/2, 1)]; % the response
rho=0.1; % the regularization parameter
% it is a ratio between (0,1), if .rFlag=1
%----------------------- Set optional items -----------------------
opts=[];
% Starting point
opts.init=2; % starting from a zero point
% Termination
opts.tFlag=5; % run .maxIter iterations
opts.maxIter=100; % maximum number of iterations
% Normalization
opts.nFlag=0; % without normalization
% Regularization
opts.rFlag=1; % the input parameter 'rho' is a ratio in (0, 1)
%opts.rsL2=0.01; % the squared two norm term
% Group Property
opts.sWeight=[1,1]; % set the weight for positive and negative samples
%----------------------- Run the code LeastR -----------------------
fprintf('\n mFlag=0, lFlag=0 \n');
opts.mFlag=0; % treating it as compositive function
opts.lFlag=0; % Nemirovski's line search
tic;
[x1, c1, funVal1, ValueL1]= LogisticR(A, y, rho, opts);
toc;
opts.maxIter=1000;
fprintf('\n mFlag=1, lFlag=0 \n');
opts.mFlag=1; % smooth reformulation
opts.lFlag=0; % Nemirovski's line search
opts.tFlag=2; opts.tol= funVal1(end);
tic;
[x2, c2, funVal2, ValueL2]= LogisticR(A, y, rho, opts);
toc;
fprintf('\n mFlag=1, lFlag=1 \n');
opts.mFlag=1; % smooth reformulation
opts.lFlag=1; % adaptive line search
opts.tFlag=2; opts.tol= funVal1(end);
tic;
[x3, c3, funVal3, ValueL3]= LogisticR(A, y, rho, opts);
toc;
figure;
plot(funVal1,'-r');
hold on;
plot(funVal2,'--b');
hold on;
plot(funVal3,':g');
legend('mFlag=0, lFlag=0', 'mFlag=1, lFlag=0', 'mFlag=1, lFlag=1');
xlabel('Iteration (i)');
ylabel('The objective function value');
% --------------------- compute the pathwise solutions ----------------
opts.fName='LogisticR'; % set the function name to 'LogisticR'
Z=[0.5, 0.2, 0.1, 0.01]; % set the parameters
% run the function pathSolutionLogistic
fprintf('\n Compute the pathwise solutions, please wait...');
[X,C]=pathSolutionLogistic(A, y, Z, opts);