-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdemprior.m
406 lines (339 loc) · 11.1 KB
/
demprior.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
function demprior(action);
%DEMPRIOR Demonstrate sampling from a multi-parameter Gaussian prior.
%
% Description
% This function plots the functions represented by a multi-layer
% perceptron network when the weights are set to values drawn from a
% Gaussian prior distribution. The parameters AW1, AB1 AW2 and AB2
% control the inverse variances of the first-layer weights, the hidden
% unit biases, the second-layer weights and the output unit biases
% respectively. Their values can be adjusted on a logarithmic scale
% using the sliders, or by typing values into the text boxes and
% pressing the return key.
%
% See also
% MLP
%
% Copyright (c) Ian T Nabney (1996-2001)
if nargin<1,
action='initialize';
end;
if strcmp(action,'initialize')
aw1 = 0.01;
ab1 = 0.1;
aw2 = 1.0;
ab2 = 1.0;
% Create FIGURE
fig=figure( ...
'Name','Sampling from a Gaussian prior', ...
'Position', [50 50 480 380], ...
'NumberTitle','off', ...
'Color', [0.8 0.8 0.8], ...
'Visible','on');
% The TITLE BAR frame
uicontrol(fig, ...
'Style','frame', ...
'Units','normalized', ...
'HorizontalAlignment', 'center', ...
'Position', [0.5 0.82 0.45 0.1], ...
'BackgroundColor',[0.60 0.60 0.60]);
% The TITLE BAR text
uicontrol(fig, ...
'Style', 'text', ...
'Units', 'normalized', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position', [0.54 0.85 0.40 0.05], ...
'HorizontalAlignment', 'left', ...
'String', 'Sampling from a Gaussian prior');
% Frames to enclose sliders
uicontrol(fig, ...
'Style', 'frame', ...
'Units', 'normalized', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position', [0.05 0.08 0.35 0.18]);
uicontrol(fig, ...
'Style', 'frame', ...
'Units', 'normalized', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position', [0.05 0.3 0.35 0.18]);
uicontrol(fig, ...
'Style', 'frame', ...
'Units', 'normalized', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position', [0.05 0.52 0.35 0.18]);
uicontrol(fig, ...
'Style', 'frame', ...
'Units', 'normalized', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position', [0.05 0.74 0.35 0.18]);
% Frame text
uicontrol(fig, ...
'Style', 'text', ...
'Units', 'normalized', ...
'HorizontalAlignment', 'left', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position', [0.07 0.17 0.06 0.07], ...
'String', 'aw1');
% Frame text
uicontrol(fig, ...
'Style', 'text', ...
'Units', 'normalized', ...
'HorizontalAlignment', 'left', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position', [0.07 0.39 0.06 0.07], ...
'String', 'ab1');
% Frame text
uicontrol(fig, ...
'Style', 'text', ...
'Units', 'normalized', ...
'HorizontalAlignment', 'left', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position', [0.07 0.61 0.06 0.07], ...
'String', 'aw2');
% Frame text
uicontrol(fig, ...
'Style', 'text', ...
'Units', 'normalized', ...
'HorizontalAlignment', 'left', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position', [0.07 0.83 0.06 0.07], ...
'String', 'ab2');
% Slider
minval = -5; maxval = 5;
aw1slide = uicontrol(fig, ...
'Style', 'slider', ...
'Units', 'normalized', ...
'Value', log10(aw1), ...
'BackgroundColor', [0.8 0.8 0.8], ...
'Position', [0.07 0.1 0.31 0.05], ...
'Min', minval, 'Max', maxval, ...
'Callback', 'demprior update');
% Slider
ab1slide = uicontrol(fig, ...
'Style', 'slider', ...
'Units', 'normalized', ...
'Value', log10(ab1), ...
'BackgroundColor', [0.8 0.8 0.8], ...
'Position', [0.07 0.32 0.31 0.05], ...
'Min', minval, 'Max', maxval, ...
'Callback', 'demprior update');
% Slider
aw2slide = uicontrol(fig, ...
'Style', 'slider', ...
'Units', 'normalized', ...
'Value', log10(aw2), ...
'BackgroundColor', [0.8 0.8 0.8], ...
'Position', [0.07 0.54 0.31 0.05], ...
'Min', minval, 'Max', maxval, ...
'Callback', 'demprior update');
% Slider
ab2slide = uicontrol(fig, ...
'Style', 'slider', ...
'Units', 'normalized', ...
'Value', log10(ab2), ...
'BackgroundColor', [0.8 0.8 0.8], ...
'Position', [0.07 0.76 0.31 0.05], ...
'Min', minval, 'Max', maxval, ...
'Callback', 'demprior update');
% The graph box
haxes = axes('Position', [0.5 0.28 0.45 0.45], ...
'Units', 'normalized', ...
'Visible', 'on');
% Text display of hyper-parameter values
format = '%8f';
aw1val = uicontrol(fig, ...
'Style', 'edit', ...
'Units', 'normalized', ...
'Position', [0.15 0.17 0.23 0.07], ...
'String', sprintf(format, aw1), ...
'Callback', 'demprior newval');
ab1val = uicontrol(fig, ...
'Style', 'edit', ...
'Units', 'normalized', ...
'Position', [0.15 0.39 0.23 0.07], ...
'String', sprintf(format, ab1), ...
'Callback', 'demprior newval');
aw2val = uicontrol(fig, ...
'Style', 'edit', ...
'Units', 'normalized', ...
'Position', [0.15 0.61 0.23 0.07], ...
'String', sprintf(format, aw2), ...
'Callback', 'demprior newval');
ab2val = uicontrol(fig, ...
'Style', 'edit', ...
'Units', 'normalized', ...
'Position', [0.15 0.83 0.23 0.07], ...
'String', sprintf(format, ab2), ...
'Callback', 'demprior newval');
% The SAMPLE button
uicontrol(fig, ...
'Style','push', ...
'Units','normalized', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position',[0.5 0.08 0.13 0.1], ...
'String','Sample', ...
'Callback','demprior replot');
% The CLOSE button
uicontrol(fig, ...
'Style','push', ...
'Units','normalized', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position',[0.82 0.08 0.13 0.1], ...
'String','Close', ...
'Callback','close(gcf)');
% The HELP button
uicontrol(fig, ...
'Style','push', ...
'Units','normalized', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position',[0.66 0.08 0.13 0.1], ...
'String','Help', ...
'Callback','demprior help');
% Save handles to objects
hndlList=[fig aw1slide ab1slide aw2slide ab2slide aw1val ab1val aw2val ...
ab2val haxes];
set(fig, 'UserData', hndlList);
demprior('replot')
elseif strcmp(action, 'update'),
% Update when a slider is moved.
hndlList = get(gcf, 'UserData');
aw1slide = hndlList(2);
ab1slide = hndlList(3);
aw2slide = hndlList(4);
ab2slide = hndlList(5);
aw1val = hndlList(6);
ab1val = hndlList(7);
aw2val = hndlList(8);
ab2val = hndlList(9);
haxes = hndlList(10);
aw1 = 10^get(aw1slide, 'Value');
ab1 = 10^get(ab1slide, 'Value');
aw2 = 10^get(aw2slide, 'Value');
ab2 = 10^get(ab2slide, 'Value');
format = '%8f';
set(aw1val, 'String', sprintf(format, aw1));
set(ab1val, 'String', sprintf(format, ab1));
set(aw2val, 'String', sprintf(format, aw2));
set(ab2val, 'String', sprintf(format, ab2));
demprior('replot');
elseif strcmp(action, 'newval'),
% Update when text is changed.
hndlList = get(gcf, 'UserData');
aw1slide = hndlList(2);
ab1slide = hndlList(3);
aw2slide = hndlList(4);
ab2slide = hndlList(5);
aw1val = hndlList(6);
ab1val = hndlList(7);
aw2val = hndlList(8);
ab2val = hndlList(9);
haxes = hndlList(10);
aw1 = sscanf(get(aw1val, 'String'), '%f');
ab1 = sscanf(get(ab1val, 'String'), '%f');
aw2 = sscanf(get(aw2val, 'String'), '%f');
ab2 = sscanf(get(ab2val, 'String'), '%f');
set(aw1slide, 'Value', log10(aw1));
set(ab1slide, 'Value', log10(ab1));
set(aw2slide, 'Value', log10(aw2));
set(ab2slide, 'Value', log10(ab2));
demprior('replot');
elseif strcmp(action, 'replot'),
% Re-sample from the prior and plot graphs.
oldFigNumber=watchon;
hndlList = get(gcf, 'UserData');
aw1slide = hndlList(2);
ab1slide = hndlList(3);
aw2slide = hndlList(4);
ab2slide = hndlList(5);
haxes = hndlList(10);
aw1 = 10^get(aw1slide, 'Value');
ab1 = 10^get(ab1slide, 'Value');
aw2 = 10^get(aw2slide, 'Value');
ab2 = 10^get(ab2slide, 'Value');
axes(haxes);
cla
set(gca, ...
'Box', 'on', ...
'Color', [0 0 0], ...
'XColor', [0 0 0], ...
'YColor', [0 0 0], ...
'FontSize', 14);
axis([-1 1 -10 10]);
set(gca,'DefaultLineLineWidth', 2);
nhidden = 12;
prior = mlpprior(1, nhidden, 1, aw1, ab1, aw2, ab2);
xvals = -1:0.005:1;
nsample = 10; % Number of samples from prior.
hold on
plot([-1 0; 1 0], [0 -10; 0 10], 'b--');
net = mlp(1, nhidden, 1, 'linear', prior);
for i = 1:nsample
net = mlpinit(net, prior);
yvals = mlpfwd(net, xvals');
plot(xvals', yvals, 'y');
end
watchoff(oldFigNumber);
elseif strcmp(action, 'help'),
% Provide help to user.
oldFigNumber=watchon;
helpfig = figure('Position', [100 100 480 400], ...
'Name', 'Help', ...
'NumberTitle', 'off', ...
'Color', [0.8 0.8 0.8], ...
'Visible','on');
% The HELP TITLE BAR frame
uicontrol(helpfig, ...
'Style','frame', ...
'Units','normalized', ...
'HorizontalAlignment', 'center', ...
'Position', [0.05 0.82 0.9 0.1], ...
'BackgroundColor',[0.60 0.60 0.60]);
% The HELP TITLE BAR text
uicontrol(helpfig, ...
'Style', 'text', ...
'Units', 'normalized', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position', [0.26 0.85 0.6 0.05], ...
'HorizontalAlignment', 'left', ...
'String', 'Help: Sampling from a Gaussian Prior');
helpstr1 = strcat( ...
'This demonstration shows the effects of sampling from a Gaussian', ...
' prior over weights for a two-layer feed-forward network. The', ...
' parameters aw1, ab1, aw2 and ab2 control the inverse variances of', ...
' the first-layer weights, the hidden unit biases, the second-layer', ...
' weights and the output unit biases respectively. Their values can', ...
' be adjusted on a logarithmic scale using the sliders, or by', ...
' typing values into the text boxes and pressing the return key.', ...
' After setting these values, press the ''Sample'' button to see a', ...
' new sample from the prior. ');
helpstr2 = strcat( ...
'Observe how aw1 controls the horizontal length-scale of the', ...
' variation in the functions, ab1 controls the input range over', ...
' such variations occur, aw2 sets the vertical scale of the output', ...
' and ab2 sets the vertical off-set of the output. The network has', ...
' 12 hidden units. ');
hstr(1) = {helpstr1};
hstr(2) = {''};
hstr(3) = {helpstr2};
% The HELP text
helpui = uicontrol(helpfig, ...
'Style', 'edit', ...
'Units', 'normalized', ...
'ForegroundColor', [0 0 0], ...
'HorizontalAlignment', 'left', ...
'BackgroundColor', [1 1 1], ...
'Min', 0, ...
'Max', 2, ...
'Position', [0.05 0.2 0.9 0.8]);
[hstrw , newpos] = textwrap(helpui, hstr, 70);
set(helpui, 'String', hstrw, 'Position', [0.05, 0.2, 0.9, newpos(4)]);
% The CLOSE button
uicontrol(helpfig, ...
'Style','push', ...
'Units','normalized', ...
'BackgroundColor', [0.6 0.6 0.6], ...
'Position',[0.4 0.05 0.2 0.1], ...
'String','Close', ...
'Callback','close(gcf)');
watchoff(oldFigNumber);
end;