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