comparison toolboxes/FullBNT-1.0.7/netlab3.3/demprior.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function demprior(action);
2 %DEMPRIOR Demonstrate sampling from a multi-parameter Gaussian prior.
3 %
4 % Description
5 % This function plots the functions represented by a multi-layer
6 % perceptron network when the weights are set to values drawn from a
7 % Gaussian prior distribution. The parameters AW1, AB1 AW2 and AB2
8 % control the inverse variances of the first-layer weights, the hidden
9 % unit biases, the second-layer weights and the output unit biases
10 % respectively. Their values can be adjusted on a logarithmic scale
11 % using the sliders, or by typing values into the text boxes and
12 % pressing the return key.
13 %
14 % See also
15 % MLP
16 %
17
18 % Copyright (c) Ian T Nabney (1996-2001)
19
20 if nargin<1,
21 action='initialize';
22 end;
23
24 if strcmp(action,'initialize')
25
26 aw1 = 0.01;
27 ab1 = 0.1;
28 aw2 = 1.0;
29 ab2 = 1.0;
30
31 % Create FIGURE
32 fig=figure( ...
33 'Name','Sampling from a Gaussian prior', ...
34 'Position', [50 50 480 380], ...
35 'NumberTitle','off', ...
36 'Color', [0.8 0.8 0.8], ...
37 'Visible','on');
38
39 % The TITLE BAR frame
40 uicontrol(fig, ...
41 'Style','frame', ...
42 'Units','normalized', ...
43 'HorizontalAlignment', 'center', ...
44 'Position', [0.5 0.82 0.45 0.1], ...
45 'BackgroundColor',[0.60 0.60 0.60]);
46
47 % The TITLE BAR text
48 uicontrol(fig, ...
49 'Style', 'text', ...
50 'Units', 'normalized', ...
51 'BackgroundColor', [0.6 0.6 0.6], ...
52 'Position', [0.54 0.85 0.40 0.05], ...
53 'HorizontalAlignment', 'left', ...
54 'String', 'Sampling from a Gaussian prior');
55
56 % Frames to enclose sliders
57 uicontrol(fig, ...
58 'Style', 'frame', ...
59 'Units', 'normalized', ...
60 'BackgroundColor', [0.6 0.6 0.6], ...
61 'Position', [0.05 0.08 0.35 0.18]);
62
63 uicontrol(fig, ...
64 'Style', 'frame', ...
65 'Units', 'normalized', ...
66 'BackgroundColor', [0.6 0.6 0.6], ...
67 'Position', [0.05 0.3 0.35 0.18]);
68
69 uicontrol(fig, ...
70 'Style', 'frame', ...
71 'Units', 'normalized', ...
72 'BackgroundColor', [0.6 0.6 0.6], ...
73 'Position', [0.05 0.52 0.35 0.18]);
74
75 uicontrol(fig, ...
76 'Style', 'frame', ...
77 'Units', 'normalized', ...
78 'BackgroundColor', [0.6 0.6 0.6], ...
79 'Position', [0.05 0.74 0.35 0.18]);
80
81 % Frame text
82 uicontrol(fig, ...
83 'Style', 'text', ...
84 'Units', 'normalized', ...
85 'HorizontalAlignment', 'left', ...
86 'BackgroundColor', [0.6 0.6 0.6], ...
87 'Position', [0.07 0.17 0.06 0.07], ...
88 'String', 'aw1');
89
90 % Frame text
91 uicontrol(fig, ...
92 'Style', 'text', ...
93 'Units', 'normalized', ...
94 'HorizontalAlignment', 'left', ...
95 'BackgroundColor', [0.6 0.6 0.6], ...
96 'Position', [0.07 0.39 0.06 0.07], ...
97 'String', 'ab1');
98
99 % Frame text
100 uicontrol(fig, ...
101 'Style', 'text', ...
102 'Units', 'normalized', ...
103 'HorizontalAlignment', 'left', ...
104 'BackgroundColor', [0.6 0.6 0.6], ...
105 'Position', [0.07 0.61 0.06 0.07], ...
106 'String', 'aw2');
107
108 % Frame text
109 uicontrol(fig, ...
110 'Style', 'text', ...
111 'Units', 'normalized', ...
112 'HorizontalAlignment', 'left', ...
113 'BackgroundColor', [0.6 0.6 0.6], ...
114 'Position', [0.07 0.83 0.06 0.07], ...
115 'String', 'ab2');
116
117 % Slider
118 minval = -5; maxval = 5;
119 aw1slide = uicontrol(fig, ...
120 'Style', 'slider', ...
121 'Units', 'normalized', ...
122 'Value', log10(aw1), ...
123 'BackgroundColor', [0.8 0.8 0.8], ...
124 'Position', [0.07 0.1 0.31 0.05], ...
125 'Min', minval, 'Max', maxval, ...
126 'Callback', 'demprior update');
127
128 % Slider
129 ab1slide = uicontrol(fig, ...
130 'Style', 'slider', ...
131 'Units', 'normalized', ...
132 'Value', log10(ab1), ...
133 'BackgroundColor', [0.8 0.8 0.8], ...
134 'Position', [0.07 0.32 0.31 0.05], ...
135 'Min', minval, 'Max', maxval, ...
136 'Callback', 'demprior update');
137
138 % Slider
139 aw2slide = uicontrol(fig, ...
140 'Style', 'slider', ...
141 'Units', 'normalized', ...
142 'Value', log10(aw2), ...
143 'BackgroundColor', [0.8 0.8 0.8], ...
144 'Position', [0.07 0.54 0.31 0.05], ...
145 'Min', minval, 'Max', maxval, ...
146 'Callback', 'demprior update');
147
148 % Slider
149 ab2slide = uicontrol(fig, ...
150 'Style', 'slider', ...
151 'Units', 'normalized', ...
152 'Value', log10(ab2), ...
153 'BackgroundColor', [0.8 0.8 0.8], ...
154 'Position', [0.07 0.76 0.31 0.05], ...
155 'Min', minval, 'Max', maxval, ...
156 'Callback', 'demprior update');
157
158 % The graph box
159 haxes = axes('Position', [0.5 0.28 0.45 0.45], ...
160 'Units', 'normalized', ...
161 'Visible', 'on');
162
163 % Text display of hyper-parameter values
164
165 format = '%8f';
166
167 aw1val = uicontrol(fig, ...
168 'Style', 'edit', ...
169 'Units', 'normalized', ...
170 'Position', [0.15 0.17 0.23 0.07], ...
171 'String', sprintf(format, aw1), ...
172 'Callback', 'demprior newval');
173
174 ab1val = uicontrol(fig, ...
175 'Style', 'edit', ...
176 'Units', 'normalized', ...
177 'Position', [0.15 0.39 0.23 0.07], ...
178 'String', sprintf(format, ab1), ...
179 'Callback', 'demprior newval');
180
181 aw2val = uicontrol(fig, ...
182 'Style', 'edit', ...
183 'Units', 'normalized', ...
184 'Position', [0.15 0.61 0.23 0.07], ...
185 'String', sprintf(format, aw2), ...
186 'Callback', 'demprior newval');
187
188 ab2val = uicontrol(fig, ...
189 'Style', 'edit', ...
190 'Units', 'normalized', ...
191 'Position', [0.15 0.83 0.23 0.07], ...
192 'String', sprintf(format, ab2), ...
193 'Callback', 'demprior newval');
194
195 % The SAMPLE button
196 uicontrol(fig, ...
197 'Style','push', ...
198 'Units','normalized', ...
199 'BackgroundColor', [0.6 0.6 0.6], ...
200 'Position',[0.5 0.08 0.13 0.1], ...
201 'String','Sample', ...
202 'Callback','demprior replot');
203
204 % The CLOSE button
205 uicontrol(fig, ...
206 'Style','push', ...
207 'Units','normalized', ...
208 'BackgroundColor', [0.6 0.6 0.6], ...
209 'Position',[0.82 0.08 0.13 0.1], ...
210 'String','Close', ...
211 'Callback','close(gcf)');
212
213 % The HELP button
214 uicontrol(fig, ...
215 'Style','push', ...
216 'Units','normalized', ...
217 'BackgroundColor', [0.6 0.6 0.6], ...
218 'Position',[0.66 0.08 0.13 0.1], ...
219 'String','Help', ...
220 'Callback','demprior help');
221
222 % Save handles to objects
223
224 hndlList=[fig aw1slide ab1slide aw2slide ab2slide aw1val ab1val aw2val ...
225 ab2val haxes];
226 set(fig, 'UserData', hndlList);
227
228 demprior('replot')
229
230
231 elseif strcmp(action, 'update'),
232
233 % Update when a slider is moved.
234
235 hndlList = get(gcf, 'UserData');
236 aw1slide = hndlList(2);
237 ab1slide = hndlList(3);
238 aw2slide = hndlList(4);
239 ab2slide = hndlList(5);
240 aw1val = hndlList(6);
241 ab1val = hndlList(7);
242 aw2val = hndlList(8);
243 ab2val = hndlList(9);
244 haxes = hndlList(10);
245
246 aw1 = 10^get(aw1slide, 'Value');
247 ab1 = 10^get(ab1slide, 'Value');
248 aw2 = 10^get(aw2slide, 'Value');
249 ab2 = 10^get(ab2slide, 'Value');
250
251 format = '%8f';
252 set(aw1val, 'String', sprintf(format, aw1));
253 set(ab1val, 'String', sprintf(format, ab1));
254 set(aw2val, 'String', sprintf(format, aw2));
255 set(ab2val, 'String', sprintf(format, ab2));
256
257 demprior('replot');
258
259 elseif strcmp(action, 'newval'),
260
261 % Update when text is changed.
262
263 hndlList = get(gcf, 'UserData');
264 aw1slide = hndlList(2);
265 ab1slide = hndlList(3);
266 aw2slide = hndlList(4);
267 ab2slide = hndlList(5);
268 aw1val = hndlList(6);
269 ab1val = hndlList(7);
270 aw2val = hndlList(8);
271 ab2val = hndlList(9);
272 haxes = hndlList(10);
273
274 aw1 = sscanf(get(aw1val, 'String'), '%f');
275 ab1 = sscanf(get(ab1val, 'String'), '%f');
276 aw2 = sscanf(get(aw2val, 'String'), '%f');
277 ab2 = sscanf(get(ab2val, 'String'), '%f');
278
279 set(aw1slide, 'Value', log10(aw1));
280 set(ab1slide, 'Value', log10(ab1));
281 set(aw2slide, 'Value', log10(aw2));
282 set(ab2slide, 'Value', log10(ab2));
283
284 demprior('replot');
285
286 elseif strcmp(action, 'replot'),
287
288 % Re-sample from the prior and plot graphs.
289
290 oldFigNumber=watchon;
291
292 hndlList = get(gcf, 'UserData');
293 aw1slide = hndlList(2);
294 ab1slide = hndlList(3);
295 aw2slide = hndlList(4);
296 ab2slide = hndlList(5);
297 haxes = hndlList(10);
298
299 aw1 = 10^get(aw1slide, 'Value');
300 ab1 = 10^get(ab1slide, 'Value');
301 aw2 = 10^get(aw2slide, 'Value');
302 ab2 = 10^get(ab2slide, 'Value');
303
304 axes(haxes);
305 cla
306 set(gca, ...
307 'Box', 'on', ...
308 'Color', [0 0 0], ...
309 'XColor', [0 0 0], ...
310 'YColor', [0 0 0], ...
311 'FontSize', 14);
312 axis([-1 1 -10 10]);
313 set(gca,'DefaultLineLineWidth', 2);
314
315 nhidden = 12;
316 prior = mlpprior(1, nhidden, 1, aw1, ab1, aw2, ab2);
317 xvals = -1:0.005:1;
318 nsample = 10; % Number of samples from prior.
319 hold on
320 plot([-1 0; 1 0], [0 -10; 0 10], 'b--');
321 net = mlp(1, nhidden, 1, 'linear', prior);
322 for i = 1:nsample
323 net = mlpinit(net, prior);
324 yvals = mlpfwd(net, xvals');
325 plot(xvals', yvals, 'y');
326 end
327
328 watchoff(oldFigNumber);
329
330 elseif strcmp(action, 'help'),
331
332 % Provide help to user.
333
334 oldFigNumber=watchon;
335
336 helpfig = figure('Position', [100 100 480 400], ...
337 'Name', 'Help', ...
338 'NumberTitle', 'off', ...
339 'Color', [0.8 0.8 0.8], ...
340 'Visible','on');
341
342 % The HELP TITLE BAR frame
343 uicontrol(helpfig, ...
344 'Style','frame', ...
345 'Units','normalized', ...
346 'HorizontalAlignment', 'center', ...
347 'Position', [0.05 0.82 0.9 0.1], ...
348 'BackgroundColor',[0.60 0.60 0.60]);
349
350 % The HELP TITLE BAR text
351 uicontrol(helpfig, ...
352 'Style', 'text', ...
353 'Units', 'normalized', ...
354 'BackgroundColor', [0.6 0.6 0.6], ...
355 'Position', [0.26 0.85 0.6 0.05], ...
356 'HorizontalAlignment', 'left', ...
357 'String', 'Help: Sampling from a Gaussian Prior');
358
359 helpstr1 = strcat( ...
360 'This demonstration shows the effects of sampling from a Gaussian', ...
361 ' prior over weights for a two-layer feed-forward network. The', ...
362 ' parameters aw1, ab1, aw2 and ab2 control the inverse variances of', ...
363 ' the first-layer weights, the hidden unit biases, the second-layer', ...
364 ' weights and the output unit biases respectively. Their values can', ...
365 ' be adjusted on a logarithmic scale using the sliders, or by', ...
366 ' typing values into the text boxes and pressing the return key.', ...
367 ' After setting these values, press the ''Sample'' button to see a', ...
368 ' new sample from the prior. ');
369 helpstr2 = strcat( ...
370 'Observe how aw1 controls the horizontal length-scale of the', ...
371 ' variation in the functions, ab1 controls the input range over', ...
372 ' such variations occur, aw2 sets the vertical scale of the output', ...
373 ' and ab2 sets the vertical off-set of the output. The network has', ...
374 ' 12 hidden units. ');
375 hstr(1) = {helpstr1};
376 hstr(2) = {''};
377 hstr(3) = {helpstr2};
378
379 % The HELP text
380 helpui = uicontrol(helpfig, ...
381 'Style', 'edit', ...
382 'Units', 'normalized', ...
383 'ForegroundColor', [0 0 0], ...
384 'HorizontalAlignment', 'left', ...
385 'BackgroundColor', [1 1 1], ...
386 'Min', 0, ...
387 'Max', 2, ...
388 'Position', [0.05 0.2 0.9 0.8]);
389
390 [hstrw , newpos] = textwrap(helpui, hstr, 70);
391 set(helpui, 'String', hstrw, 'Position', [0.05, 0.2, 0.9, newpos(4)]);
392
393
394 % The CLOSE button
395 uicontrol(helpfig, ...
396 'Style','push', ...
397 'Units','normalized', ...
398 'BackgroundColor', [0.6 0.6 0.6], ...
399 'Position',[0.4 0.05 0.2 0.1], ...
400 'String','Close', ...
401 'Callback','close(gcf)');
402
403 watchoff(oldFigNumber);
404
405 end;
406