-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdaline.m
315 lines (266 loc) · 11.5 KB
/
Adaline.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
classdef Adaline < matlab.graphics.chartcontainer.ChartContainer
properties(Access=public)
alpha;
maxSteps;
theshold;
batchSize;
showBatch;
showMiniBatch;
showStochastic;
pauseSec;
outfile; % animation gif
showHighlight; % highlight dots used by minibatch/stochatic
end
properties(Access=protected)
TrainingData;
Class1Scatter;
Class2Scatter;
MiniBHighlight;
StochHighlight;
BatchDecision;
StochDecision;
MiniBDecision;
CostBatchPlot;
CostStochPlot;
CostMiniBPlot;
TopAxes;
BotAxes;
% weights
Wbatch;
W0batch;
Wstoch;
W0stoch;
WminiB;
W0miniB;
end
properties(Access=protected)
Xmin;
Xmax;
Ymin;
Ymax;
Xlabel;
Ylabel;
DoneGraDes;
end
methods(Access=protected)
function setup(obj)
tcl = getLayout(obj);
tcl.GridSize = [2 1];
obj.TopAxes = nexttile(tcl, 1);
obj.BotAxes = nexttile(tcl, 2);
obj.Class1Scatter = scatter(obj.TopAxes, NaN, NaN, 'c*');
hold(obj.TopAxes, 'on');
obj.Class2Scatter = scatter(obj.TopAxes, NaN, NaN, 'mo');
obj.MiniBHighlight = scatter(obj.TopAxes, NaN, NaN, 'MarkerFaceColor', 'b', 'MarkerFaceAlpha', 0.1, 'MarkerEdgeColor', 'b');
obj.StochHighlight = scatter(obj.TopAxes, NaN, NaN, 'MarkerFaceColor', 'g', 'MarkerFaceAlpha', 0.1, 'MarkerEdgeColor', 'g');
obj.BatchDecision = plot(obj.TopAxes, NaN, NaN, 'r-');
obj.StochDecision = plot(obj.TopAxes, NaN, NaN, 'g-');
obj.MiniBDecision = plot(obj.TopAxes, NaN, NaN, 'b-');
hold(obj.TopAxes, 'off');
obj.CostBatchPlot = plot(obj.BotAxes, NaN, NaN, 'r-');
hold(obj.BotAxes, 'on');
obj.CostStochPlot = plot(obj.BotAxes, NaN, NaN, 'g-');
obj.CostMiniBPlot = plot(obj.BotAxes, NaN, NaN, 'b-');
hold(obj.BotAxes, 'off');
end
function update(obj)
if ~isempty(obj.Wbatch) && obj.showBatch
obj.updateDecisionBoundary(obj.BatchDecision, obj.Wbatch, obj.W0batch);
end
if ~isempty(obj.Wstoch) && obj.showStochastic
obj.updateDecisionBoundary(obj.StochDecision, obj.Wstoch, obj.W0stoch);
end
if ~isempty(obj.WminiB) && obj.showMiniBatch
obj.updateDecisionBoundary(obj.MiniBDecision, obj.WminiB, obj.W0miniB);
end
end
function initDecisionBound(obj)
w = rand(2,1);
w0 = rand(1);
if obj.showBatch
obj.Wbatch = w;
obj.W0batch = w0;
end
if obj.showStochastic
obj.Wstoch = w;
obj.W0stoch = w0;
end
if obj.showMiniBatch
obj.WminiB = w;
obj.W0miniB = w0;
end
end
function updateDecisionBoundary(obj, DecisionBoundary, W, W0)
if ~isempty(obj.Ymax)
[X, Y] = Adaline.getline(W(1), W(2), W0, [obj.Xmin obj.Xmax obj.Ymin obj.Ymax]);
DecisionBoundary.XData = X;
DecisionBoundary.YData = Y;
end
end
function ret = substitute(~, X, W, W0)
ret = X*W + W0;
end
function [Wret, W0ret] = graDescent(obj, X, y, W, W0, CostPlot)
ret = obj.substitute(X{:, :}, W, W0);
errors = (y{:, :} - ret);
Wret = W + obj.alpha*transpose(X{:, :})*errors;
W0ret= W0 + obj.alpha*sum(errors);
% scale it by lenght of y so as to get a fair comparison
cost = transpose(errors)*errors/2/length(y{:, :});
if isnan(CostPlot.XData)
CostPlot.YData = cost;
CostPlot.XData = 1;
else
CostPlot.YData = [CostPlot.YData, cost];
CostPlot.XData = [CostPlot.XData, CostPlot.XData(end) + 1];
end
end
end
methods(Static)
% Utility functions
function [X, Y] = getline(a, b, c, rng)
% a line of ax+by+c=0
XMIN=1;XMAX=2;YMIN=3;YMAX=4;
gety = @(x) (-c-a*x)/b;
xtmp = gety(rng(YMIN));
if xtmp < rng(XMIN)
X(1) = xtmp;
Y(1) = gety(xtmp);
else
X(1) = rng(XMIN);
Y(1) = gety(X(1));
end
xtmp = gety(rng(YMAX));
if xtmp > rng(XMAX)
X(2) = xtmp;
Y(2) = gety(xtmp);
else
X(2) = rng(XMAX);
Y(2) = gety(X(2));
end
end
end
methods(Access=public)
function obj = Adaline(csvFile)
obj.alpha = 0.005;
obj.maxSteps = 200;
obj.theshold = 0.00001;
obj.batchSize = 20;
obj.pauseSec = 0.2;
% Preprocess data
obj.preprocess(csvFile);
% axis labels
xlabel(obj.TopAxes, [obj.Xlabel '(scaled)']);
ylabel(obj.TopAxes, [obj.Ylabel '(scaled)']);
xlabel(obj.BotAxes, 'Epochs');
ylabel(obj.BotAxes, 'Sum-squared-error');
class1Data = obj.TrainingData(obj.TrainingData.cls==1, :);
class2Data = obj.TrainingData(obj.TrainingData.cls==-1, :);
obj.Class1Scatter.XData = class1Data.Var1s;
obj.Class1Scatter.YData = class1Data.Var2s;
obj.Class2Scatter.XData = class2Data.Var1s;
obj.Class2Scatter.YData = class2Data.Var2s;
obj.Xmin = min(obj.TrainingData.Var1s);
obj.Xmax = max(obj.TrainingData.Var1s);
obj.Ymin = min(obj.TrainingData.Var2s);
obj.Ymax = max(obj.TrainingData.Var2s);
xlim(obj.TopAxes, [obj.Xmin obj.Xmax]);
ylim(obj.TopAxes, [obj.Ymin obj.Ymax]);
% Default settings. Allow user to overwrite later
obj.showBatch = true;
obj.showStochastic = false;
obj.showMiniBatch = false;
obj.showHighlight = false;
obj.outfile = []; % no animation output by default
end
function preprocess(obj, csvFile)
obj.TrainingData = readtable(csvFile);
obj.Xlabel = obj.TrainingData.Properties.VariableNames{1};
obj.Ylabel = obj.TrainingData.Properties.VariableNames{2};
var1 = obj.TrainingData(:, obj.Xlabel).Variables;
var1mean = mean(var1);
var1std = std(var1);
obj.TrainingData.Var1s = (var1 - var1mean)/var1std;
var2 = obj.TrainingData(:, obj.Ylabel).Variables;
var2mean = mean(var2);
var2std = std(var2);
obj.TrainingData.Var2s = (var2 - var2mean)/var2std;
end
function setLegend(obj)
plots = [];
legends = {};
if obj.showBatch
plots(end+1) = obj.CostBatchPlot;
legends{end+1} = 'Batch';
end
if obj.showMiniBatch
plots(end+1) = obj.CostMiniBPlot;
legends{end+1} = 'Mini Batch';
end
if obj.showStochastic
plots(end+1) = obj.CostStochPlot;
legends{end+1} = 'Stochastic';
end
legend(obj.BotAxes, plots, legends);
end
function animate(obj)
% init decision boundary
obj.initDecisionBound();
xlim(obj.BotAxes, [0 obj.maxSteps]);
obj.setLegend();
obj.DoneGraDes = [~obj.showBatch ~obj.showStochastic ~obj.showMiniBatch];
numSamples = height(obj.TrainingData);
firstFrame = true;
for i=1:obj.maxSteps
if obj.showBatch && ~obj.DoneGraDes(1)
[obj.Wbatch, obj.W0batch] = obj.graDescent(obj.TrainingData(:, {'Var1s', 'Var2s'}), obj.TrainingData(:, 'cls'), obj.Wbatch, obj.W0batch, obj.CostBatchPlot);
if length(obj.CostBatchPlot.YData) > 1
obj.DoneGraDes(1) = abs(obj.CostBatchPlot.YData(end) - obj.CostBatchPlot.YData(end-1)) < obj.theshold;
end
end
% minibatch gradient
if obj.showMiniBatch && ~obj.DoneGraDes(3)
% randomly pick mini batch training data
idx = randsample([1:numSamples], obj.batchSize);
batchTable = obj.TrainingData(idx, :);
[obj.WminiB, obj.W0miniB] = obj.graDescent(batchTable(:, {'Var1s', 'Var2s'}), batchTable(:, 'cls'), obj.WminiB, obj.W0miniB, obj.CostMiniBPlot);
% highlight the dots being used in gradient descent
if obj.showHighlight
obj.MiniBHighlight.XData = batchTable.Var1s;
obj.MiniBHighlight.YData = batchTable.Var2s;
end
if length(obj.CostMiniBPlot.YData) > 1
obj.DoneGraDes(3) = abs(obj.CostMiniBPlot.YData(end) - obj.CostMiniBPlot.YData(end-1)) < obj.theshold;
end
end
if obj.showStochastic && ~obj.DoneGraDes(2)
% randomly pick one for stochastic batch
j = randi(100, 1);
stochTable = obj.TrainingData(j, :);
[obj.Wstoch, obj.W0stoch] = obj.graDescent(stochTable(:, {'Var1s', 'Var2s'}), stochTable(:, 'cls'), obj.Wstoch, obj.W0stoch, obj.CostStochPlot);
if obj.showHighlight
obj.StochHighlight.XData = stochTable.Var1s;
obj.StochHighlight.YData = stochTable.Var2s;
end
if length(obj.CostStochPlot.YData) > 1
obj.DoneGraDes(2) = abs(obj.CostStochPlot.YData(end) - obj.CostStochPlot.YData(end-1)) < obj.theshold;
end
end
if all(obj.DoneGraDes)
break;
end
if ~isempty(obj.outfile)
[img, map] = rgb2ind(frame2im( getframe(gcf)),256);
if firstFrame
imwrite(img,map,obj.outfile,'gif','DelayTime',0.5);
firstFrame = false;
else
imwrite(img,map,obj.outfile,'gif','writemode', 'append','delaytime', obj.pauseSec);
end
else
pause(obj.pauseSec);
end
end
end
end
end