-
Notifications
You must be signed in to change notification settings - Fork 0
/
lrSGD.m
116 lines (95 loc) · 2.86 KB
/
lrSGD.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
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
function [theta, cost] = lrSGD(x, y, options)
% Logistic Regression Solver: Stochastic Gradient Descent
% http://en.wikipedia.org/wiki/SGD
% http://ufldl.stanford.edu/tutorial/index.php/Optimization:_Stochastic_Gradient_Descent
% x -- input data, size = [m, n], m:samples number, n:feature dimension;
% y -- labels data, size = [m, 1], values=[-1 1], m:samples number;
% options -- options struct
% max_itr: max iterators
% min_eps: min eps
% C: penalty factor
% debug: show debug message
% theta -- parameters, size = [n+1, 1], n:elements nubmer;
% cost -- cost
% author -- amadeuzou AT gmail
% date -- 11/19/2013, Beijing, China
if nargin == 2
options.C = 1;
options.max_itr = 100;
options.min_eps = 1e-3;
options.debug = 1;
end
if ~isfield(options, 'C')
options.C = 1;
end
if ~isfield(options, 'max_itr')
options.max_itr = 100;
end
if ~isfield(options, 'min_eps')
options.min_eps = 1e-3;
end
if ~isfield(options, 'debug')
options.debug = 1;
end
if ~isfield(options, 'epochs')
options.epochs = 3;
end
if ~isfield(options, 'minibatch')
options.minibatch = 50;
end
if ~isfield(options, 'alpha')
options.alpha = 1e-1;
end
if ~isfield(options, 'momentum')
options.momentum = .95;
end
epochs = options.epochs;
minibatch = options.minibatch;
alpha = options.alpha;
momentum = options.momentum;
% Setup for momentum
mom = 0.5;
momIncrease = 20;
[m, n] = size(x);
x = [ones(m, 1), x];
theta = zeros(n+1, 1);
velocity = theta;
cost = 0;
J = [];
itr = 0;
err = 0;
for i = 1:epochs
% randomly permute indices of data for quick minibatch sampling
rp = randperm(m);
for s = 1:minibatch:(m-minibatch+1)
% get next randomly selected minibatch
mb_data = x(rp(s:s+minibatch-1), :);
mb_labels = y(rp(s:s+minibatch-1));
[cost, grad] = lrCostFunc(mb_data, mb_labels, theta, options.C);
velocity = mom*velocity + alpha*grad;
theta = theta - velocity;
% increase momentum after momIncrease iterations
if itr == momIncrease
mom = options.momentum;
end;
% cost record
J = [J; cost];
err = norm(velocity);%theta - theta_k
itr = itr + 1;
if(options.debug)
disp(['itr = ', num2str(itr), ', cost = ', num2str(cost), ', err = ', num2str(err)]);
end
if itr >= options.max_itr || err <= options.min_eps || norm(grad)<=options.min_eps
return;
end
end
% aneal learning rate by factor of two after each epoch
alpha = alpha/2.0;
end
% draw cost cure
if(options.debug)
figure(1024)
plot(1:length(J), J, 'b-');
xlabel('iterators');
ylabel('cost');
end