Daniel@0: function demprior(action); Daniel@0: %DEMPRIOR Demonstrate sampling from a multi-parameter Gaussian prior. Daniel@0: % Daniel@0: % Description Daniel@0: % This function plots the functions represented by a multi-layer Daniel@0: % perceptron network when the weights are set to values drawn from a Daniel@0: % Gaussian prior distribution. The parameters AW1, AB1 AW2 and AB2 Daniel@0: % control the inverse variances of the first-layer weights, the hidden Daniel@0: % unit biases, the second-layer weights and the output unit biases Daniel@0: % respectively. Their values can be adjusted on a logarithmic scale Daniel@0: % using the sliders, or by typing values into the text boxes and Daniel@0: % pressing the return key. Daniel@0: % Daniel@0: % See also Daniel@0: % MLP Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: if nargin<1, Daniel@0: action='initialize'; Daniel@0: end; Daniel@0: Daniel@0: if strcmp(action,'initialize') Daniel@0: Daniel@0: aw1 = 0.01; Daniel@0: ab1 = 0.1; Daniel@0: aw2 = 1.0; Daniel@0: ab2 = 1.0; Daniel@0: Daniel@0: % Create FIGURE Daniel@0: fig=figure( ... Daniel@0: 'Name','Sampling from a Gaussian prior', ... Daniel@0: 'Position', [50 50 480 380], ... Daniel@0: 'NumberTitle','off', ... Daniel@0: 'Color', [0.8 0.8 0.8], ... Daniel@0: 'Visible','on'); Daniel@0: Daniel@0: % The TITLE BAR frame Daniel@0: uicontrol(fig, ... Daniel@0: 'Style','frame', ... Daniel@0: 'Units','normalized', ... Daniel@0: 'HorizontalAlignment', 'center', ... Daniel@0: 'Position', [0.5 0.82 0.45 0.1], ... Daniel@0: 'BackgroundColor',[0.60 0.60 0.60]); Daniel@0: Daniel@0: % The TITLE BAR text Daniel@0: uicontrol(fig, ... Daniel@0: 'Style', 'text', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position', [0.54 0.85 0.40 0.05], ... Daniel@0: 'HorizontalAlignment', 'left', ... Daniel@0: 'String', 'Sampling from a Gaussian prior'); Daniel@0: Daniel@0: % Frames to enclose sliders Daniel@0: uicontrol(fig, ... Daniel@0: 'Style', 'frame', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position', [0.05 0.08 0.35 0.18]); Daniel@0: Daniel@0: uicontrol(fig, ... Daniel@0: 'Style', 'frame', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position', [0.05 0.3 0.35 0.18]); Daniel@0: Daniel@0: uicontrol(fig, ... Daniel@0: 'Style', 'frame', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position', [0.05 0.52 0.35 0.18]); Daniel@0: Daniel@0: uicontrol(fig, ... Daniel@0: 'Style', 'frame', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position', [0.05 0.74 0.35 0.18]); Daniel@0: Daniel@0: % Frame text Daniel@0: uicontrol(fig, ... Daniel@0: 'Style', 'text', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'HorizontalAlignment', 'left', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position', [0.07 0.17 0.06 0.07], ... Daniel@0: 'String', 'aw1'); Daniel@0: Daniel@0: % Frame text Daniel@0: uicontrol(fig, ... Daniel@0: 'Style', 'text', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'HorizontalAlignment', 'left', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position', [0.07 0.39 0.06 0.07], ... Daniel@0: 'String', 'ab1'); Daniel@0: Daniel@0: % Frame text Daniel@0: uicontrol(fig, ... Daniel@0: 'Style', 'text', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'HorizontalAlignment', 'left', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position', [0.07 0.61 0.06 0.07], ... Daniel@0: 'String', 'aw2'); Daniel@0: Daniel@0: % Frame text Daniel@0: uicontrol(fig, ... Daniel@0: 'Style', 'text', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'HorizontalAlignment', 'left', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position', [0.07 0.83 0.06 0.07], ... Daniel@0: 'String', 'ab2'); Daniel@0: Daniel@0: % Slider Daniel@0: minval = -5; maxval = 5; Daniel@0: aw1slide = uicontrol(fig, ... Daniel@0: 'Style', 'slider', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'Value', log10(aw1), ... Daniel@0: 'BackgroundColor', [0.8 0.8 0.8], ... Daniel@0: 'Position', [0.07 0.1 0.31 0.05], ... Daniel@0: 'Min', minval, 'Max', maxval, ... Daniel@0: 'Callback', 'demprior update'); Daniel@0: Daniel@0: % Slider Daniel@0: ab1slide = uicontrol(fig, ... Daniel@0: 'Style', 'slider', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'Value', log10(ab1), ... Daniel@0: 'BackgroundColor', [0.8 0.8 0.8], ... Daniel@0: 'Position', [0.07 0.32 0.31 0.05], ... Daniel@0: 'Min', minval, 'Max', maxval, ... Daniel@0: 'Callback', 'demprior update'); Daniel@0: Daniel@0: % Slider Daniel@0: aw2slide = uicontrol(fig, ... Daniel@0: 'Style', 'slider', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'Value', log10(aw2), ... Daniel@0: 'BackgroundColor', [0.8 0.8 0.8], ... Daniel@0: 'Position', [0.07 0.54 0.31 0.05], ... Daniel@0: 'Min', minval, 'Max', maxval, ... Daniel@0: 'Callback', 'demprior update'); Daniel@0: Daniel@0: % Slider Daniel@0: ab2slide = uicontrol(fig, ... Daniel@0: 'Style', 'slider', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'Value', log10(ab2), ... Daniel@0: 'BackgroundColor', [0.8 0.8 0.8], ... Daniel@0: 'Position', [0.07 0.76 0.31 0.05], ... Daniel@0: 'Min', minval, 'Max', maxval, ... Daniel@0: 'Callback', 'demprior update'); Daniel@0: Daniel@0: % The graph box Daniel@0: haxes = axes('Position', [0.5 0.28 0.45 0.45], ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'Visible', 'on'); Daniel@0: Daniel@0: % Text display of hyper-parameter values Daniel@0: Daniel@0: format = '%8f'; Daniel@0: Daniel@0: aw1val = uicontrol(fig, ... Daniel@0: 'Style', 'edit', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'Position', [0.15 0.17 0.23 0.07], ... Daniel@0: 'String', sprintf(format, aw1), ... Daniel@0: 'Callback', 'demprior newval'); Daniel@0: Daniel@0: ab1val = uicontrol(fig, ... Daniel@0: 'Style', 'edit', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'Position', [0.15 0.39 0.23 0.07], ... Daniel@0: 'String', sprintf(format, ab1), ... Daniel@0: 'Callback', 'demprior newval'); Daniel@0: Daniel@0: aw2val = uicontrol(fig, ... Daniel@0: 'Style', 'edit', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'Position', [0.15 0.61 0.23 0.07], ... Daniel@0: 'String', sprintf(format, aw2), ... Daniel@0: 'Callback', 'demprior newval'); Daniel@0: Daniel@0: ab2val = uicontrol(fig, ... Daniel@0: 'Style', 'edit', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'Position', [0.15 0.83 0.23 0.07], ... Daniel@0: 'String', sprintf(format, ab2), ... Daniel@0: 'Callback', 'demprior newval'); Daniel@0: Daniel@0: % The SAMPLE button Daniel@0: uicontrol(fig, ... Daniel@0: 'Style','push', ... Daniel@0: 'Units','normalized', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position',[0.5 0.08 0.13 0.1], ... Daniel@0: 'String','Sample', ... Daniel@0: 'Callback','demprior replot'); Daniel@0: Daniel@0: % The CLOSE button Daniel@0: uicontrol(fig, ... Daniel@0: 'Style','push', ... Daniel@0: 'Units','normalized', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position',[0.82 0.08 0.13 0.1], ... Daniel@0: 'String','Close', ... Daniel@0: 'Callback','close(gcf)'); Daniel@0: Daniel@0: % The HELP button Daniel@0: uicontrol(fig, ... Daniel@0: 'Style','push', ... Daniel@0: 'Units','normalized', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position',[0.66 0.08 0.13 0.1], ... Daniel@0: 'String','Help', ... Daniel@0: 'Callback','demprior help'); Daniel@0: Daniel@0: % Save handles to objects Daniel@0: Daniel@0: hndlList=[fig aw1slide ab1slide aw2slide ab2slide aw1val ab1val aw2val ... Daniel@0: ab2val haxes]; Daniel@0: set(fig, 'UserData', hndlList); Daniel@0: Daniel@0: demprior('replot') Daniel@0: Daniel@0: Daniel@0: elseif strcmp(action, 'update'), Daniel@0: Daniel@0: % Update when a slider is moved. Daniel@0: Daniel@0: hndlList = get(gcf, 'UserData'); Daniel@0: aw1slide = hndlList(2); Daniel@0: ab1slide = hndlList(3); Daniel@0: aw2slide = hndlList(4); Daniel@0: ab2slide = hndlList(5); Daniel@0: aw1val = hndlList(6); Daniel@0: ab1val = hndlList(7); Daniel@0: aw2val = hndlList(8); Daniel@0: ab2val = hndlList(9); Daniel@0: haxes = hndlList(10); Daniel@0: Daniel@0: aw1 = 10^get(aw1slide, 'Value'); Daniel@0: ab1 = 10^get(ab1slide, 'Value'); Daniel@0: aw2 = 10^get(aw2slide, 'Value'); Daniel@0: ab2 = 10^get(ab2slide, 'Value'); Daniel@0: Daniel@0: format = '%8f'; Daniel@0: set(aw1val, 'String', sprintf(format, aw1)); Daniel@0: set(ab1val, 'String', sprintf(format, ab1)); Daniel@0: set(aw2val, 'String', sprintf(format, aw2)); Daniel@0: set(ab2val, 'String', sprintf(format, ab2)); Daniel@0: Daniel@0: demprior('replot'); Daniel@0: Daniel@0: elseif strcmp(action, 'newval'), Daniel@0: Daniel@0: % Update when text is changed. Daniel@0: Daniel@0: hndlList = get(gcf, 'UserData'); Daniel@0: aw1slide = hndlList(2); Daniel@0: ab1slide = hndlList(3); Daniel@0: aw2slide = hndlList(4); Daniel@0: ab2slide = hndlList(5); Daniel@0: aw1val = hndlList(6); Daniel@0: ab1val = hndlList(7); Daniel@0: aw2val = hndlList(8); Daniel@0: ab2val = hndlList(9); Daniel@0: haxes = hndlList(10); Daniel@0: Daniel@0: aw1 = sscanf(get(aw1val, 'String'), '%f'); Daniel@0: ab1 = sscanf(get(ab1val, 'String'), '%f'); Daniel@0: aw2 = sscanf(get(aw2val, 'String'), '%f'); Daniel@0: ab2 = sscanf(get(ab2val, 'String'), '%f'); Daniel@0: Daniel@0: set(aw1slide, 'Value', log10(aw1)); Daniel@0: set(ab1slide, 'Value', log10(ab1)); Daniel@0: set(aw2slide, 'Value', log10(aw2)); Daniel@0: set(ab2slide, 'Value', log10(ab2)); Daniel@0: Daniel@0: demprior('replot'); Daniel@0: Daniel@0: elseif strcmp(action, 'replot'), Daniel@0: Daniel@0: % Re-sample from the prior and plot graphs. Daniel@0: Daniel@0: oldFigNumber=watchon; Daniel@0: Daniel@0: hndlList = get(gcf, 'UserData'); Daniel@0: aw1slide = hndlList(2); Daniel@0: ab1slide = hndlList(3); Daniel@0: aw2slide = hndlList(4); Daniel@0: ab2slide = hndlList(5); Daniel@0: haxes = hndlList(10); Daniel@0: Daniel@0: aw1 = 10^get(aw1slide, 'Value'); Daniel@0: ab1 = 10^get(ab1slide, 'Value'); Daniel@0: aw2 = 10^get(aw2slide, 'Value'); Daniel@0: ab2 = 10^get(ab2slide, 'Value'); Daniel@0: Daniel@0: axes(haxes); Daniel@0: cla Daniel@0: set(gca, ... Daniel@0: 'Box', 'on', ... Daniel@0: 'Color', [0 0 0], ... Daniel@0: 'XColor', [0 0 0], ... Daniel@0: 'YColor', [0 0 0], ... Daniel@0: 'FontSize', 14); Daniel@0: axis([-1 1 -10 10]); Daniel@0: set(gca,'DefaultLineLineWidth', 2); Daniel@0: Daniel@0: nhidden = 12; Daniel@0: prior = mlpprior(1, nhidden, 1, aw1, ab1, aw2, ab2); Daniel@0: xvals = -1:0.005:1; Daniel@0: nsample = 10; % Number of samples from prior. Daniel@0: hold on Daniel@0: plot([-1 0; 1 0], [0 -10; 0 10], 'b--'); Daniel@0: net = mlp(1, nhidden, 1, 'linear', prior); Daniel@0: for i = 1:nsample Daniel@0: net = mlpinit(net, prior); Daniel@0: yvals = mlpfwd(net, xvals'); Daniel@0: plot(xvals', yvals, 'y'); Daniel@0: end Daniel@0: Daniel@0: watchoff(oldFigNumber); Daniel@0: Daniel@0: elseif strcmp(action, 'help'), Daniel@0: Daniel@0: % Provide help to user. Daniel@0: Daniel@0: oldFigNumber=watchon; Daniel@0: Daniel@0: helpfig = figure('Position', [100 100 480 400], ... Daniel@0: 'Name', 'Help', ... Daniel@0: 'NumberTitle', 'off', ... Daniel@0: 'Color', [0.8 0.8 0.8], ... Daniel@0: 'Visible','on'); Daniel@0: Daniel@0: % The HELP TITLE BAR frame Daniel@0: uicontrol(helpfig, ... Daniel@0: 'Style','frame', ... Daniel@0: 'Units','normalized', ... Daniel@0: 'HorizontalAlignment', 'center', ... Daniel@0: 'Position', [0.05 0.82 0.9 0.1], ... Daniel@0: 'BackgroundColor',[0.60 0.60 0.60]); Daniel@0: Daniel@0: % The HELP TITLE BAR text Daniel@0: uicontrol(helpfig, ... Daniel@0: 'Style', 'text', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position', [0.26 0.85 0.6 0.05], ... Daniel@0: 'HorizontalAlignment', 'left', ... Daniel@0: 'String', 'Help: Sampling from a Gaussian Prior'); Daniel@0: Daniel@0: helpstr1 = strcat( ... Daniel@0: 'This demonstration shows the effects of sampling from a Gaussian', ... Daniel@0: ' prior over weights for a two-layer feed-forward network. The', ... Daniel@0: ' parameters aw1, ab1, aw2 and ab2 control the inverse variances of', ... Daniel@0: ' the first-layer weights, the hidden unit biases, the second-layer', ... Daniel@0: ' weights and the output unit biases respectively. Their values can', ... Daniel@0: ' be adjusted on a logarithmic scale using the sliders, or by', ... Daniel@0: ' typing values into the text boxes and pressing the return key.', ... Daniel@0: ' After setting these values, press the ''Sample'' button to see a', ... Daniel@0: ' new sample from the prior. '); Daniel@0: helpstr2 = strcat( ... Daniel@0: 'Observe how aw1 controls the horizontal length-scale of the', ... Daniel@0: ' variation in the functions, ab1 controls the input range over', ... Daniel@0: ' such variations occur, aw2 sets the vertical scale of the output', ... Daniel@0: ' and ab2 sets the vertical off-set of the output. The network has', ... Daniel@0: ' 12 hidden units. '); Daniel@0: hstr(1) = {helpstr1}; Daniel@0: hstr(2) = {''}; Daniel@0: hstr(3) = {helpstr2}; Daniel@0: Daniel@0: % The HELP text Daniel@0: helpui = uicontrol(helpfig, ... Daniel@0: 'Style', 'edit', ... Daniel@0: 'Units', 'normalized', ... Daniel@0: 'ForegroundColor', [0 0 0], ... Daniel@0: 'HorizontalAlignment', 'left', ... Daniel@0: 'BackgroundColor', [1 1 1], ... Daniel@0: 'Min', 0, ... Daniel@0: 'Max', 2, ... Daniel@0: 'Position', [0.05 0.2 0.9 0.8]); Daniel@0: Daniel@0: [hstrw , newpos] = textwrap(helpui, hstr, 70); Daniel@0: set(helpui, 'String', hstrw, 'Position', [0.05, 0.2, 0.9, newpos(4)]); Daniel@0: Daniel@0: Daniel@0: % The CLOSE button Daniel@0: uicontrol(helpfig, ... Daniel@0: 'Style','push', ... Daniel@0: 'Units','normalized', ... Daniel@0: 'BackgroundColor', [0.6 0.6 0.6], ... Daniel@0: 'Position',[0.4 0.05 0.2 0.1], ... Daniel@0: 'String','Close', ... Daniel@0: 'Callback','close(gcf)'); Daniel@0: Daniel@0: watchoff(oldFigNumber); Daniel@0: Daniel@0: end; Daniel@0: