⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nnd17no.m

📁 《神经网络设计》英文版的配套源代码
💻 M
📖 第 1 页 / 共 2 页
字号:
function nnd17no(cmd,arg1)
% nnd17no Function approximation demonstration.
% This demonstration requires the Neural Network Toolbox.

% Copyright 1994-2002 PWS Publishing Company and The MathWorks, Inc.
% $Revision: 1.8 $
% First Version, 8-31-95.

%==================================================================

% CONSTANTS
me = 'nnd17no';
max_t = 0.5;
w_max = 10;
p_max = 2;
circle_size = 6;

% FLAGS
change_func = 0;

% DEFAULTS
if nargin == 0, cmd = ''; else cmd = lower(cmd); end

% FIND WINDOW IF IT EXISTS
fig = nndfgflg(me);
if length(get(fig,'children')) == 0, fig = 0; end
  
% GET WINDOW DATA IF IT EXISTS
if fig
  H = get(fig,'userdata');
  fig_axis = H(1);            % window axis
  desc_text = H(2);           % handle to first line of text sequence
  s1_bar = H(3);              % lr slider bar
  s1_text = H(4);             % lr text
  fa_axis = H(5);             % function approximation axis
  fa_ptr = H(6);              % function approximation plot handles
  i_bar = H(7);               % Difficulty slider bar
  i_text = H(8);              % Difficuly text
  lls_radiob = H(9);
  ols_radiob = H(10);
  rand_radiob = H(11);
  W1_ptr = H(12);
  B1_ptr = H(13);
  W2_ptr = H(14);
  B2_ptr = H(15);
  but_train = H(16);
  fb_axis = H(17);
end

%==================================================================
% Activate the window.
%
% ME() or ME('')
%==================================================================

if strcmp(cmd,'')
  if fig
    figure(fig)
    set(fig,'visible','on')
  else
    feval(me,'init')
  end

%==================================================================
% Close the window.
%
% ME() or ME('')
%==================================================================

elseif strcmp(cmd,'close') & (fig)
  delete(fig)

%==================================================================
% Initialize the window.
%
% ME('init')
%==================================================================

elseif strcmp(cmd,'init') & (~fig)

  % CHECK FOR NNT
  % ODJ 12/23/07 Check for NN Toolbox removed
  %if ~nntexist(me), return, end

  % CONSTANTS
  W1 = [10; 10];
  b1 = [-5;5];
  W2 = [1 1];
  b2 = [-1];

  % NEW DEMO FIGURE
  fig = nndemof2(me,'DESIGN','Nonlinear Optimization','','Chapter 17');
  set(fig, ...
    'windowbuttondownfcn',nncallbk(me,'down'), ...
    'BackingStore','off',...
    'nextplot','add');
  H = get(fig,'userdata');
  fig_axis = H(1);
  desc_text = H(2);

  % ICON
  nndicon(17,458,363,'shadow')

  % HIDDEN NEURONS SLIDER BAR
  s1 = 4;
  x = 20;
  y = 55;
  len = 150;
  text(x,y,'Number of Hidden Neurons:',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',10,...
    'horizontalalignment','left')
  s1_text = text(x+len,y,num2str(s1),...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  text(x,y-38,'2',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','left')
  text(x+len,y-38,'9',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  s1_bar = uicontrol(...
    'units','points',...
    'position',[x y-25 len 16],...
    'style','slider',...
    'backg',nnltgray,...
    'callback',[me '(''s1'')'],...
    'min',2,...
    'max',9,...
    'value',s1);

  % PROBLEM DIFFICULTY SLIDER BAR
  i = 1;
  x = 200;
  y = 55;
  len = 150;
  text(x,y,'Difficulty Index:',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',10,...
    'horizontalalignment','left')
  i_text = text(x+len,y,num2str(i),...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  text(x,y-38,'1',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','left')
  text(x+len,y-38,'9',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  i_bar = uicontrol(...
    'units','points',...
    'position',[x y-25 len 16],...
    'style','slider',...
    'backg',nnltgray,...
    'callback',[me '(''i'')'],...
    'min',1,...
    'max',9,...
    'value',i);



  lls_radiob = uicontrol(...
    'units','points',...
    'position',[20 80 100 16],...
    'style','radiobutton',...
    'backg',nnltgray,...
    'string','Linear Least Squares',...
    'callback',[me '(''lls'')'],...
    'value',0);
  ols_radiob = uicontrol(...
    'units','points',...
    'position',[130 80 120 16],...
    'style','radiobutton',...
    'backg',nnltgray,...
    'string','Orthogonal Least Squares',...
    'callback',[me '(''ols'')'],...
    'value',0);
  rand_radiob = uicontrol(...
    'units','points',...
    'position',[260 80 80 16],...
    'style','radiobutton',...
    'backg',nnltgray,...
    'string','random weights',...
    'callback',[me '(''rand'')'],...
    'value',0);
  uipanel(...
    'Units','points',...
    'Title',{  'Select Initialization Method' },...
    'backg',nnltgray,...
    'Tag','uipanel1',...
    'Clipping','on',... 
    'Position',[15 70 340 40]);

  W1_ptr = uicontrol('visible','off','userdata',[]);
  B1_ptr = uicontrol('visible','off','userdata',[]);
  W2_ptr = uicontrol('visible','off','userdata',[]);
  B2_ptr = uicontrol('visible','off','userdata',[]);

  % FUNCTION APPROXIMATION
  i = 1;
  P = -2:(.4/i):2;
  T = 1 + sin(pi*P/4);

  fa_axis = nnsfo('a2','Function Approximation','','Target','');
  set(fa_axis,...
    'position',[50 220 270 120],...
    'ylim',[0 2])
  %fa_plot =  plot(P,T,'color',nndkblue,'linewidth',3);
  fa_plot =  plot(P,T,'color',nnblue,'linewidth',3);
  set(fa_axis,'ylim',[-2 4])

  fb_axis = nnsfo('a2','','Input','a^1','');
  set(fb_axis,...
    'position',[50 160 270 40],...
    'ylim',[-0.1 1.1])
  fb_plot = plot([P(1) P(end)],[0 0],'k');
    
  % BUTTONS
  but_train = uicontrol(...
    'units','points',...
    'position',[400 120 60 20],...
    'string','Train',...
    'callback',[me '(''train'')'],...
    'enable','off');
  uicontrol(...
    'units','points',...
    'position',[400 90 60 20],...
    'string','Contents',...
    'callback','nndtoc')
  uicontrol(...
    'units','points',...
    'position',[400 60 60 20],...
    'string','Close',...
    'callback',[me '(''close'')'])

  % DATA POINTERS
  fa_ptr = uicontrol('visible','off','userdata',fa_plot);

  % SAVE WINDOW DATA AND LOCK
  H = [fig_axis desc_text s1_bar s1_text fa_axis fa_ptr i_bar i_text ...
      lls_radiob ols_radiob rand_radiob W1_ptr B1_ptr W2_ptr B2_ptr ...
      but_train fb_axis];
  set(fig,'userdata',H,'nextplot','new')

  % INSTRUCTION TEXT
  feval(me,'instr');

  % LOCK WINDOW
  set(fig,'nextplot','new','color',nnltgray);

  nnchkfs;

%==================================================================
% Display the instructions.
%
% ME('instr')
%==================================================================

elseif strcmp(cmd,'instr') & (fig)
  nnsettxt(desc_text,...
    'Use the slide bars',...
    'to choose the',...
    'number of neurons',...
    'in the hidden layer',...
    'and the difficulty',...
    'of the function.',...
    '',...
    'Select a Weight',...
    'Initialization Method',...
    '',...
    'Click the [Train]',...
    'button to train the',...
    'Radial-basis',...
    'network on the',...
    'function at left.',...
    ''...
    );
    

%==================================================================
% Respond to linear least squares Initialization.
%
% ME('lls')
%==================================================================

elseif strcmp(cmd,'lls')
  set(lls_radiob,'value',1);
  set(ols_radiob,'value',0);
  set(rand_radiob,'value',0);

  range=[-2 2];
  S1 = round(get(s1_bar,'value'));
  i = round(get(i_bar,'value'));
  delta = (range(2)-range(1))/(S1-1);
  %b=sqrt(S1)/delta;
  b=1.6652/delta;
  W10 = (range(1):delta:range(2))+0.01;
  W10 = W10(1:(end))';
  B10 = b*ones(size(W10));
  npts=S1;
  d1 = (range(2)-range(1))/(npts-1);
  p = range(1):d1:range(2);
  t = 1+sin(i*pi*p/4);
  QQ = length(p);
  pp = repmat(p,S1,1); 
  n1 = abs(pp-W10*ones(1,QQ)).*(B10*ones(1,QQ));
  a1 = exp(-n1.^2);
  Z = [a1;ones(1,QQ)];
  ro=0;
  x=pinv(Z*Z'+ro*eye(S1+1))*Z*t';
  W20 = x(1:end-1)';
  B20 = x(end);
  
  set(W1_ptr,'userdata',W10);
  set(B1_ptr,'userdata',B10);
  set(W2_ptr,'userdata',W20);
  set(B2_ptr,'userdata',B20);
  
  cmd='update_plot';
  
  
%==================================================================
% Respond to linear Orthogonal squares Initialization.
%
% ME('ols')
%==================================================================

elseif strcmp(cmd,'ols')
  set(lls_radiob,'value',0);
  set(ols_radiob,'value',1);
  set(rand_radiob,'value',0);

  range=[-2 2];
  S1 = round(get(s1_bar,'value'));
  i = round(get(i_bar,'value'));
  range=[-2 2];
  npts=S1;
  d1 = (range(2)-range(1))/(npts-1);
  p = range(1):d1:range(2);
  t = 1+sin(i*pi*p/4);
  c=p;
  n=S1;
  b = ones(size(c));
  [W10,B10,W20,B20] = rb_ols(p,t,c,b,n,0);
  
  set(W1_ptr,'userdata',W10);
  set(B1_ptr,'userdata',B10);
  set(W2_ptr,'userdata',W20);
  set(B2_ptr,'userdata',B20);
  
  cmd='update_plot';
  
%==================================================================
% Respond to random weights Initialization.
%
% ME('rand')
%==================================================================

elseif strcmp(cmd,'rand')
  set(lls_radiob,'value',0);
  set(ols_radiob,'value',0);
  set(rand_radiob,'value',1);

  S1 = round(get(s1_bar,'value'));
  i = round(get(i_bar,'value'));
  W10 = 2*rand(S1,1)-1;  %rands(S1,1);
  B10 = 2*rand(S1,1)-1;  %rands(S1,1);
  W20 = 2*rand(1,S1)-1;  %rands(1,S1);
  B20 = 2*rand(1,1)-1;   %rands(1,1);
    
  set(W1_ptr,'userdata',W10);
  set(B1_ptr,'userdata',B10);
  set(W2_ptr,'userdata',W20);
  set(B2_ptr,'userdata',B20);
  
  cmd='update_plot';
  
    
%==================================================================
% Respond to hidden neuron slider.
%
% ME('i')
%==================================================================

elseif strcmp(cmd,'s1')
  
  i = get(i_bar,'value');
  s1 = get(s1_bar,'value');
  s1 = round(s1);
  set(s1_text,'string',sprintf('%g',s1))

  set(fig,'nextplot','add')
  delete(get(fa_axis,'children'))
  P = -2:(.4/i):2;
  T = 1 + sin(i*pi*P/4);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -