-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanimateGraDes.m
286 lines (258 loc) · 10.4 KB
/
animateGraDes.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
classdef animateGraDes < matlab.graphics.chartcontainer.ChartContainer
% Copyright 2021 The MathWorks, Inc.
properties (Access=public)
funcStr; % Function of x, y in String
alpha; % alpha(s) for gradient descent
startPoint; % start point for gradient descent
maxStepCount; % Stop after # steps even if min is smaller than threshold
stopThreshold; % when distance is smaller than this, stop
drawContour; % true for drawing 2D contour, false (default) for 3D
fillContour; % use contourf instead of contour for contour plot
xrange; % x range for showing funcStr
yrange; % y range for showing funStr
showAnnotation; % show annotation or not
stepsPerSecond; % advance # steps each second
outfile; % output an animation GIF if set to a filename
end
% protected properties for internal use only
properties (Access=protected)
func; % function handler of funcStr
xPartial; % x partial derivative of func
yPartial; % y partial derivative of func
doneGraDes; % gradient descent finished
readyUpdate; % All inited, ready for update
FuncPlot; % 3D surface or 2D Contour
PathPlots; % Gradient Descent path plot
Annot; % Annotation
end
methods(Access=protected)
function setup(~)
% noop
end
function update(~)
% noop
end
function setupChartContainer(obj)
newcolors = [0.99 0.00 0.00 % red
0.48 0.77 0.18 % green
0.99 0.00 0.99 % magenta
0.00 0.99 0.99]; % cyan
colororder(newcolors);
ax = getAxes(obj);
pathCount = length(obj.alpha);
obj.doneGraDes = false(1, pathCount);
if obj.drawContour
% 2D contour and 2D path plot
if obj.fillContour
[~, obj.FuncPlot] = contourf(ax, [], [], [], 'ShowText', 'on');
else
[~, obj.FuncPlot] = contour(ax, [], [], [], 'ShowText', 'on');
end
hold(ax, 'on');
for i=1:pathCount
obj.PathPlots = [obj.PathPlots plot(ax, NaN, NaN, '-*')];
end
hold(ax, 'off');
else
% 3D surface and 3D path plot
obj.FuncPlot = surf(ax, [], [], []);
alpha(obj.FuncPlot, 0.5);
hold(ax, 'on');
for i=1:pathCount
obj.PathPlots = [obj.PathPlots, plot3(ax, NaN, NaN, NaN, '-*')];
end
hold(ax, 'off');
end
end
function moveOneStep(obj)
if ~isempty(obj.func) && obj.readyUpdate
if isscalar(obj.PathPlots)
obj.doneGraDes = obj.updateApathPlot(obj.PathPlots, obj.alpha);
else
numPaths = length(obj.alpha);
for i=1:numPaths
if ~obj.doneGraDes(i)
obj.doneGraDes(i) = obj.updateApathPlot(obj.PathPlots(i), obj.alpha(i));
end
end
end
% update animation gif if necessary
obj.updateGIF();
else
% Not ready to update yet
end
end
function initApathPlot(obj, plot)
xStart = obj.startPoint(1);
yStart = obj.startPoint(2);
zStart = obj.func(xStart, yStart);
plot.XData = xStart;
plot.YData = yStart;
if ~obj.drawContour
plot.ZData = zStart;
end
end
function done = updateApathPlot(obj, plot, alpha)
done = false;
xStart = plot.XData(end);
yStart = plot.YData(end);
zStart = obj.func(xStart, yStart);
xEnd = double(xStart - obj.getXpartial(xStart, yStart, alpha));
yEnd = double(yStart - obj.getYpartial(xStart, yStart, alpha));
zEnd = double(obj.func(xEnd, yEnd));
plot.XData = [plot.XData xEnd];
plot.YData = [plot.YData yEnd];
if ~obj.drawContour
plot.ZData = [plot.ZData zEnd];
end
if obj.checkStop(xStart, xEnd, yStart, yEnd, zStart, zEnd)
done = true;
end
end
function initGIF(obj)
if ~isempty(obj.outfile)
[img, map] = rgb2ind(frame2im( getframe(gcf)),256);
imwrite(img,map,obj.outfile,'gif','DelayTime',0.5);
end
end
function updateGIF(obj)
pauseInSec = 1/obj.stepsPerSecond;
if ~isempty(obj.outfile)
[img, map] = rgb2ind(frame2im( getframe(gcf)),256);
imwrite(img,map,obj.outfile,'gif','writemode', 'append','delaytime',pauseInSec);
else
pause(pauseInSec);
end
end
function showResult(obj, i)
pauseInSec = 1/obj.stepsPerSecond;
if obj.showAnnotation
if ~isempty(obj.Annot)
if length(obj.PathPlots) == 1
xEnd = obj.PathPlots.XData(end);
yEnd = obj.PathPlots.YData(end);
zEnd = double(obj.func(xEnd, yEnd));
strDisplay = {['\alpha : ' num2str(obj.alpha)], ...
['step count: ' num2str(i)], ...
['Min: (' num2str(xEnd) ', ' num2str(yEnd) ', ' num2str(zEnd) ')']};
else
strDisplay = {};
colors = {'red', 'green', 'magenta', 'cyan'};
numColors = length(colors);
for i=1:length(obj.PathPlots)
color = colors{mod(i-1, numColors)+1};
xEnd = obj.PathPlots(i).XData(end);
yEnd = obj.PathPlots(i).YData(end);
zEnd = double(obj.func(xEnd, yEnd));
strDisplay{end+1} = ['\color{' color '}\alpha: ' num2str(obj.alpha(i)) '; min: ' num2str(zEnd)]; %#ok<AGROW>
end
end
obj.Annot.String = strDisplay;
end
if ~isempty(obj.outfile)
[img, map] = rgb2ind(frame2im( getframe(gcf)),256);
imwrite(img,map,obj.outfile,'gif','writemode', 'append','delaytime',pauseInSec);
end
end
end
% template method design pattern for subclass
function preGraDes(obj)
% no op
end
end
methods(Access=public)
function agd = animateGraDes()
clf
% set default values. User can overwrite after instantiation
agd.drawContour = false;
agd.stepsPerSecond = 5;
agd.alpha = 0.1;
agd.startPoint = [5 5];
agd.xrange = -10:1:10;
agd.yrange = -10:1:10;
agd.maxStepCount = 100;
agd.stopThreshold = 1E-10;
agd.showAnnotation = true;
agd.outfile = [];
agd.doneGraDes = false;
agd.Annot = [];
agd.fillContour = false;
end
function animate(obj)
obj.setupChartContainer();
obj.initGIF();
try
obj.func = str2func(['@(x, y)' obj.funcStr]);
symFunc = sym(obj.func);
syms x y
obj.xPartial = diff(symFunc, x);
obj.yPartial = diff(symFunc, y);
catch ME
disp(ME);
return;
end
[X, Y] = meshgrid(obj.xrange, obj.yrange);
Z = obj.computeZ(X, Y);
obj.FuncPlot.XData = X;
obj.FuncPlot.YData = Y;
obj.FuncPlot.ZData = Z;
if isscalar(obj.PathPlots)
obj.initApathPlot(obj.PathPlots);
else
numPaths = length(obj.alpha);
for i=1:numPaths
obj.initApathPlot(obj.PathPlots(i));
end
end
if obj.showAnnotation
dim = [0.05 0.81 0.38 0.13];
strDisplay = 'Running ...';
obj.Annot = annotation('textbox', dim, ...
'String', strDisplay,'BackgroundColor','white', ...
'FitBoxToText', 'on',...
'interpreter', 'tex');
end
obj.preGraDes();
obj.readyUpdate = true;
for i=0:obj.maxStepCount
obj.moveOneStep();
if all(obj.doneGraDes)
break;
end
if obj.showAnnotation
obj.Annot.String = ['Running ' num2str(i) '/' num2str(obj.maxStepCount)];
end
end
obj.showResult(i);
end
% Utility functions
function zValue = getYpartial(obj, xIn, yIn, alpha)
syms x y
x = xIn;
y = yIn;
zValue = alpha*subs(obj.yPartial);
end
function zValue = getXpartial(obj, xIn, yIn, alpha)
syms x y
x = xIn;
y = yIn;
zValue = alpha*subs(obj.xPartial);
end
function Z = computeZ(obj, X, Y)
sz = size(X);
Z = zeros(sz(1), sz(2));
for i=1:sz(1)
for j=1:sz(2)
Z(i, j) = obj.func(X(i, j), Y(i, j));
end
end
end
function done = checkStop(obj, xStart, xEnd, yStart, yEnd, zStart, zEnd)
done = false;
dis = (xStart-xEnd)^2+(yStart-yEnd)^2+(zStart-zEnd)^2;
if dis <obj.stopThreshold
done = true;
end
end
end
end