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

📄 nnd11bc.m

📁 Martin T.Hagan等著,戴葵等译,神经网络设计,机械工业出版社,一书的所有例程
💻 M
📖 第 1 页 / 共 3 页
字号:
function [ret1,ret2,ret3,ret4]=nnd11bc(cmd,arg1,arg2,arg3,arg4,arg5)
%NND11BC Backpropagation calculation demonstration.
%
%	This demonstration requires the Neural Network Toolbox.
%
%	NND11BC runs this demo.
%
%	NND11BC('set',W1,b1,W2,b2)
%	  sets the network's weight and bias values.
%
%	[W1,b1,W2,b2] = NND11BC('get')
%	  gets the network's weight and bias values.

% First Version, 8-31-95.

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

% CONSTANTS
me = 'nnd11bc';
max_t = 0.5;
w_max = 10;
p_max = 2;
box_len = 40;
box_x = [0 1 1 0 0]*box_len;
box_y = [-1 -1 1 1 -1]*10;
pause_time = 1;

% FLAGS
change_func = 0;

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

% FIND WINDOW IF IT EXISTS
fig = nnfgflag(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
  W1_ptr = H(3);
  b1_ptr = H(4);
  W2_ptr = H(5);
  b2_ptr = H(6);
  p_name = H(7);
  W11_box = H(8);
  W11_text = H(9);
  W11_name = H(10);
  W12_box = H(11);
  W12_text = H(12);
  W12_name= H(13);
  b11_box = H(14);
  b11_text = H(15);
  b11_name = H(16);
  b12_box = H(17);
  b12_text = H(18);
  b12_name = H(19);
  a11_name = H(20);
  a12_name = H(21);
  W21_box = H(22);
  W21_text = H(23);
  W21_name = H(24);
  W22_box = H(25);
  W22_text = H(26);
  W22_name = H(27);
  b2_box = H(28);
  b2_text = H(29);
  b2_name = H(30);
  a2_name = H(31);
  t_name = H(32);
  e_name = H(33);
  vars = H(34+[0:10]);
  vals = H(45+[0:10]);
  fp1_marker = H(56);
  fp2_marker = H(57);
  fp3_marker = H(58);
  bp1_marker = H(59);
  bp2_marker = H(60);
  W1_marker = H(61);
  b1_marker = H(62);
  W2_marker = H(63);
  b2_marker = H(64);
  p_marker = H(65);
  t_marker = H(66);
  state_ptr = H(67);
  p_ptr = H(68);
  a1_ptr = H(69);
  a2_ptr = H(70);
  e_ptr = H(71);
  s1_ptr = H(72);
  s2_ptr = H(73);
  t_ptr = H(74);
  go_button = H(75);
  s11_name = H(76);
  s12_name = H(77);
  s2_name = H(78);
  blip_ptr = H(79);
  bloop_ptr = H(80);
  blp_ptr = H(81);
  state1 = H(82);
  state2 = H(83);
  state3 = H(84);
  state4 = H(85);
  go_box = H(86);
  last_text = H(87);
  p_edit = H(88);

  state = get(state_ptr,'userdata');
  blip = get(blip_ptr,'userdata');
  bloop = get(bloop_ptr,'userdata');
  blp = get(blp_ptr,'userdata');
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
  if ~nntexist(me), return, end

  % CONSTANTS
  W1 = [-0.27; -0.41];
  b1 = [-0.48; -0.13];
  W2 = [0.09 -0.17];
  b2 = [0.48];

%%%%%%%% Copied from NNDEMOF2

s2 = 'DESIGN';
s3 = 'Backpropagation Calculation';
s4 = '';
s5 = 'Chapter 11';

fig = nnbg(me);
set(fig,'nextplot','add')
H = get(fig,'userdata');
h1 = H(1);
text(25,380,'Neural Network', ...
  'color',nnblack, ...
  'fontname','times', ...
  'fontsize',16, ...
  'fontangle','italic', ...
  'fontweight','bold');
text(135,380,s2, ...
  'color',nnblack, ...
  'fontname','times', ...
  'fontsize',16, ...
  'fontweight','bold');
text(415,380,s3,...
  'color',nnblack, ...
  'fontname','times', ...
  'fontsize',16, ...
  'fontweight','bold',...
  'HorizontalAlignment','right');
nndrwlin([0 415],[365 365],4,nndkblue);
h2 = text(390,315,'',...
  'color',nnblack, ...
  'fontname','helvetica', ...
  'fontsize',10);
text1 = h2;
for i=1:30
  text2 = text(390,315-6*i,'',...
    'color',nnblack, ...
    'fontname','helvetica', ...
    'fontsize',10);
  set(text1,'userdata',text2);
  text1 = text2;
end
set(text1,'userdata','end');
text(410,54,s4, ...
  'color',nnblack, ...
  'fontname','times', ...
  'fontsize',12, ...
  'fontweight','bold');
text(410,38,s5, ...
  'color',nnblack, ...
  'fontname','times', ...
  'fontsize',12, ...
  'fontweight','bold');
nndrwlin([410 501],[24 24],4,nndkblue);
set(fig,'userdata',[h1 h2])
set(fig,'color',nndkgray,'color',nnltgray)

%%%%%%%%

  set(fig, ...
    'windowbuttondownfcn',nncallbk(me,'down'), ...
    'BackingStore','off');
  H = get(fig,'userdata');
  fig_axis = H(1);
  desc_text = H(2);

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

  % NETWORK POSITIONS
  x1 = 30;     % input
  x2 = x1+85;  % 1st layer sum
  x3 = x2+70;  % 1st layer transfer function
  x4 = x3+125; % 2nd layer sum
  x5 = x4+55;  % 2nd layer transfer function
  x6 = x5+50;  % output
  y1 = 305;    % top neuron
  y2 = y1-20;  % input & output neuron
  y3 = y1-40;  % bottom neuron
  sz = 15;     % size of icons
  wx = 50;     % weight horizontal offset (from 1st layer)
  wy = 40;     % weight vertical offset (from middle)

  % NETWORK INPUT
  p_name = nndtext(x1-10,y2,'p');
  set(p_name,'fontsize',10);

  % TOP NEURON: WEIGHT
  plot([x1 x1+20],[y2 y1],'linewidth',2,'color',nnred);
  W11_box = fill(x1+20+box_x,y1+box_y,nnltgray,...
    'linewidth',2,...
    'edgecolor',nnred,...
    'erasemode','none');
  W11_text = nndtext(x1+20+box_len/2,y1,sprintf('%5.3f',W1(1)));
  set(W11_text,'fontsize',10);
  plot([x1+20+box_len x2-sz],[y1 y1],'linewidth',2,'color',nnred);
  W11_name = nndtext(x2-wx,y2+wy,'W1(1,1)');
  set(W11_name,'fontsize',10);

  % TOP NEURON: BIAS
  plot([x2 x2 x3],[y1+sz*2 y1 y1],'linewidth',2,'color',nnred);
  b11_box = fill(x2-box_len/2+box_x,y1+sz*2+10+box_y,nnltgray,...
    'linewidth',2,...
    'edgecolor',nnred,...
    'erasemode','none');
  b11_text = nndtext(x2,y1+sz*2+10,sprintf('%5.3f',b1(1)));
  set(b11_text,'fontsize',10);
  b11_name = nndtext(x2+25,y1+sz*2+10,'b1(1)','left');
  set(b11_name,'fontsize',10);

  % TOP NEURON: BODY
  nndsicon('sum',x2,y1,sz)
  n11_name = nndtext(x2+sz+20,y1+10,'n1(1)');
  set(n11_name,'fontsize',10);
  nndsicon('logsig',x3,y1,sz)
  s11_name = nndtext(x2+sz+75,y1+40,'s1(1)');
  set(s11_name,'fontsize',10);
  plot(x2+sz+[30 60],y1+[18 32],'--',...
    'color',nnred,...
    'linewidth',2,...
    'erasemode','none')
  a11_name = nndtext(x3+sz+20,y1+10,'a1(1)');
  set(a11_name,'fontsize',10);

  % BOTTOM NEURON: WEIGHT
  plot([x1 x1+20],[y2 y3],'linewidth',2,'color',nnred);
  W12_box = fill(x1+20+box_x,y3+box_y,nnltgray,...
    'linewidth',2,...
    'edgecolor',nnred,...
    'erasemode','none');
  W12_text = nndtext(x1+20+box_len/2,y3,sprintf('%5.3f',W1(2)));
  set(W12_text,'fontsize',10);
  plot([x1+20+box_len x2-sz],[y3 y3],'linewidth',2,'color',nnred);
  W12_name = nndtext(x2-wx,y2-wy,'W1(2,1)');
  set(W12_name,'fontsize',10);

  % BOTTOM NEURON: BIAS
  plot([x2 x2 x3],[y3-sz*2 y3 y3],'linewidth',2,'color',nnred);
  b12_box = fill(x2-box_len/2+box_x,y3-sz*2-10+box_y,nnltgray,...
    'linewidth',2,...
    'edgecolor',nnred,...
    'erasemode','none');
  b12_text = nndtext(x2,y3-sz*2-10,sprintf('%5.3f',b1(2)));
  set(b12_text,'fontsize',10);
  b12_name = nndtext(x2+25,y3-sz*2-10,'b1(2)','left');
  set(b12_name,'fontsize',10);

  % BOTTOM NEURON: BODY
  nndsicon('sum',x2,y3,sz)
  n12_name = nndtext(x2+sz+20,y3-10,'n1(2)');
  set(n12_name,'fontsize',10);
  nndsicon('logsig',x3,y3,sz)
  s12_name = nndtext(x2+sz+75,y3-40,'s1(2)');
  set(s12_name,'fontsize',10);
  plot(x2+sz+[30 60],y3-[18 32],'--',...
    'color',nnred,...
    'linewidth',2,...
    'erasemode','none')
  a12_name = nndtext(x3+sz+20,y3-10,'a1(2)');
  set(a12_name,'fontsize',10);

  % OUTPUT NEURON: TOP WEIGHT
  plot([x3+sz+1 x3+sz+40],[y1 y1],'linewidth',2,'color',nnred);
  W21_box = fill(x3+sz+40+box_x,y1+box_y,nnltgray,...
    'linewidth',2,...
    'edgecolor',nnred,...
    'erasemode','none');
  W21_text = nndtext(x3+sz+40+box_len/2,y1,sprintf('%5.3f',W2(1)));
  set(W21_text,'fontsize',10);
  plot([x3+sz+40+box_len x4-10],[y1 y2],'linewidth',2,'color',nnred);
  W21_name = nndtext(x3+4+wx+20,y2+wy,'W2(1,1)');
  set(W21_name,'fontsize',10);

  % OUTPUT NEURON: BOTTOM WEIGHT
  plot([x3+sz+1 x3+sz+40],[y3 y3],'linewidth',2,'color',nnred);
  W22_box = fill(x3+sz+40+box_x,y3+box_y,nnltgray,...
    'linewidth',2,...
    'edgecolor',nnred,...
    'erasemode','none');
  W22_text = nndtext(x3+sz+40+box_len/2,y3,sprintf('%5.3f',W2(2)));
  set(W22_text,'fontsize',10);
  plot([x3+sz+40+box_len x4-10],[y3 y2],'linewidth',2,'color',nnred);
  W22_name = nndtext(x3+4+wx+20,y2-wy,'W2(1,2)');
  set(W22_name,'fontsize',10);

  % OUTPUT NEURON: BIAS
  plot([x4 x4 x6],[y2-sz*2 y2 y2],'linewidth',2,'color',nnred);
  b2_box = fill(x4-box_len/2+box_x,y2-sz*2-10+box_y,nnltgray,...
    'linewidth',2,...
    'edgecolor',nnred,...
    'erasemode','none');
  b2_text = nndtext(x4,y2-sz*2-10,sprintf('%5.3f',b2));
  set(b2_text,'fontsize',10);
  b2_name = nndtext(x4+25,y2-sz*2-10,'b2','left');
  set(b2_name,'fontsize',10);

  % OUTPUT
  plot([x6-10 x6 x6-10],[y2-7 y2 y2+7],'linewidth',2,'color',nnred);
  nndsicon('sum',x4,y2,sz)
  temp = nndtext(x4+sz+13,y2+10,'n2');
  set(temp,'fontsize',10);
  tf_icon = nndsicon('purelin',x5,y2,sz);
  s2_name = nndtext(x4+sz+60,y2+40,'s2');
  set(s2_name,'fontsize',10);
  plot(x4+sz+[20 50],y2+[18 32],'--',...
    'color',nnred,...
    'linewidth',2,...
    'erasemode','none')

  a2_name = nndtext(x5+sz+5,y2+10,'a2','left');
  set(a2_name,'fontsize',10);

  % DIFFERENCE
  angle = [0:5:360]*pi/180;
  circle_x = cos(angle)*15;
  circle_y = sin(angle)*15;
  fill(x6+sz+circle_x,y2+circle_y,nndkblue,...
    'edgecolor',nnred,...
    'linewidth',1);
  plot(x6+sz-[10 5],[y2 y2],...
    'color',nnyellow,...
    'linewidth',2)
  plot(x6+sz+1+4*[-1 1 NaN 0 0],y2-8+4*[0 0 NaN -1 1],...
    'color',nnyellow,...
    'linewidth',2)

  % ERROR
  plot(30+2*sz+[x6-10 x6 x6-10],[y2-7 y2 y2+7],'linewidth',2,'color',nnred);
  plot(2*sz+x6+[1 30],[y2 y2],'linewidth',2,'color',nnred);
  e_name = nndtext(x6+2*sz+5,y2+10,'e','left');
  set(e_name,'fontsize',10);

  % TARGET
  plot(x6+sz+8*[-1 0 1],y2-sz-[10 0 10],'linewidth',2,'color',nnred);
  plot(x6+sz+[0 0],y2-sz-[0 20],'linewidth',2,'color',nnred);
  t_name = nndtext(x6+sz,y2-sz-30,'t');

  % SEPERATION BAR
  fill([0 0 480 480],193+[0 4 4 0],nndkblue,...
    'edgecolor','none')

  % EQUATIONS
  y = 181;
  dy = 17;
  init_str = str2mat('p','t','a1','a2','e','s2','s1','W1','b1','W2','b2');
  vars = zeros(1,11);
  vals = vars;
  for i=1:11
    yy = y-dy*(i-1);
    vars(i) = nndtext(140,yy,deblank(init_str(i,:)),'right');
    set(vars(i),'fontsize',10);
    eq_sign = nndtext(148,yy,'=');
    set(eq_sign,'fontsize',10);
	if i==1
	  vals(i) = nndtext(1000,1000,' ','left');
	else
      vals(i) = nndtext(156,yy,'?','left');
	end
    set(vals(i),'fontsize',10);
  end
  nndtext(15,y,'Input:','left');
  state1 = nndtext(15,y-dy,'Target:','left');
  state2 = nndtext(15,y-2*dy,'Simulate:','left');
  state3 = nndtext(15,y-5*dy,'Backpropagate:','left');
  state4 = nndtext(15,y-7*dy,'Update:','left');
  p_edit = uicontrol(...
    'units','points',...
	'position',[156 173 60 18],...
    'style','edit',...
	'string','1.0',...
    'callback',[me '(''p'')']);

  % MARKERS
  y = 204;
  wdth = 3;
  p_marker = plot(x1+[-10 -5 0],y+[-5 5 -5],...
    'color',nnltgray,...
    'linewidth',wdth,...
    'erasemode','none');
  t_marker = plot(x6+sz+[-5 0 5],y+[-5 5 -5],...
    'color',nnltgray,...
    'linewidth',wdth,...
    'erasemode','none');
  fp1_marker = plot([x1 (x3+sz-[0 10 0 10])],y+[0 0 5 0 -5],...
    'color',nnltgray,...
    'linewidth',wdth,...
    'erasemode','none');
  fp2_marker = plot([x3+sz+5 (x5+sz-[0 10 0 10])],y+[0 0 5 0 -5],...
    'color',nnltgray,...
    'linewidth',wdth,...
    'erasemode','none');
  fp3_marker = plot([x5+sz+5 (x6+sz*2-[0 10 0 10])],y+[0 0 5 0 -5],...
    'color',nnltgray,...
    'linewidth',wdth,...
    'erasemode','none');
  bp1_marker = plot([x6+sz*2 (x5-sz+[0 10 0 10])],y+[0 0 5 0 -5],...

⌨️ 快捷键说明

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