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

📄 nnd3hamc.m

📁 Martin T.Hagan等著,戴葵等译,神经网络设计,机械工业出版社,一书的所有例程
💻 M
📖 第 1 页 / 共 2 页
字号:
      sign = '-';
    end

    set(p_text(j),...
      'color',nndkblue)
    set(p_text(j),...
      'string',[str,sign,num2str(abs(p(j)))],...
      'color',nngreen)
  end

  nnpause(0.25);
  nnsound(bloop,Fs);
  set(data,...
    'color',nndkblue)
  delete(data);

  % CLASSIFY FRUIT
  nnpause(0.25)
  nnsound(blip,Fs);
  set(nnet(1),...
    'edgecolor',[1 1 1],...
    'facecolor',nndkblue)
  set(nnet(2:3),...
    'color',[1 1 1])
  axes(fig_axis);
  nnpause(0.25)
  nnsound(bloop,Fs);
  set(nnet(1),...
    'edgecolor',[1 0 0],...
    'facecolor',nndkblue)
  set(nnet(2:3),...
    'color',[0 1 0])

  % CALL CLASSIFICATION ROUTINE
  t1 = clock;
  nnsound(classify,Fs);
  while etime(clock,t1) < 1, end

  feval(me,'classify')
  nnpause(0.25)

  % MOVE DATA FROM NETWORK TO EXIT
  xx = [252 268];
  yy = [80 80];
  set(fig,'nextplot','add')
  nnsound(blip,Fs);
  data = plot(xx,yy,...
    'color',[1 1 1],...
    'erasemode','none',...
    'linewidth',2);
  set(indicators(fruit_id+1),...
     'color',[1 1 1])
  axes(fig_axis)
  if fruit_id == 1
    box_x = 84;
  else
    box_x = 62;
  end
  box = plot([0 62 62 0 0]+314,[0 0 14 14 0]+box_x,...
    'color',[1 1 1],...
    'erasemode','none');

  nnpause(0.25);
  nnsound(bloop,Fs);
  set(data,...
    'color',nndkblue)
  delete(data);
    
  % MOVE FRUIT TO EXIT
  for dx=70:10:200
    t1 = clock;
    nnsound(blp,Fs);
    while etime(clock,t1) < 0.05, end
    set(fruit, ...
      'facecolor',nnltgray, ...
      'edgecolor',nnltgray);
    set(fruit, ...
      'facecolor',fruit_c, ...
      'edgecolor',fruit_c*0.5, ...
      'xdata',fruit_x + x + dx)
    angle = angle + 20;
    adata = [0 pi]-angle*deg;
    xdata = cos(adata)*8;
    ydata = sin(adata)*8+30;
    set(lines,'color',nnred);
    set(lines(1),...
      'xdata',xdata+50,...
      'ydata',ydata,...
      'color',nndkblue)
    set(lines(2),...
      'xdata',xdata+330,...
      'ydata',ydata,...
      'color',nndkblue)
  end

  % REMOVE FRUIT
  nnpause(0.5)
  nnsound(blip,Fs);
  set(fruit,...
    'facecolor',nnltgray,...
    'edgecolor',nnltgray);
  delete(fruit)
  set(arrows(2),...
    'facecolor',nnred)
  nnsound(wind,Fs);
  set(arrows(2),...
    'facecolor',nndkblue)

  % UNLIGHT APPROPRIATE INDICATOR
  nnpause(0.5)
  nnsound(bloop,Fs);
  set(indicators(fruit_id+1),...
    'color',fruit_c)
  set(box,...
    'color',nndkblue)
  delete(box)
  set(fig,'nextplot','new')

  % SAVE DATA
  set(angle_ptr,'userdata',angle);
  set(fruit_ptr,'userdata',fruit);
  set(id_ptr,'userdata',fruit_id);

%==================================================================
% Remove previous fruit (activated by hitting "Clear" button).
%
% ME('remove')
%==================================================================

elseif strcmp(cmd,'remove') & (fig) & (nargin == 1)
  
  % GET DATA
  wind = get(sounds(1),'userdata');
  blip = get(sounds(5),'userdata');
  bloop = get(sounds(6),'userdata');
  fruit = get(fruit_ptr,'userdata');
  fruit_id = get(id_ptr,'userdata');
  axes(fig_axis);

  % GET RID OF PREVIOUS FRUIT
  if length(fruit) > 0

    % FRUIT COLOR
    if (fruit_id == 1)
      fruit_c = [1 0.5 0];
    else
      fruit_c = [1 0 0];
    end

    % UNLIGHT APPROPRIATE INDICATOR
    nnpause(0.5)
    nnsound(bloop,Fs);
    set(indicators(fruit_id+1),...
      'color',fruit_c)

    % CLEAR VALUES
    set(p_text,...
      'color',nndkblue)
    set(p_text(1),...
      'string','SHAPE: ?',...
      'color',nnltgray)
    set(p_text(2),...
      'string','TEXTURE: ?',...
      'color',nnltgray)
    set(p_text(3),...
      'string','WEIGHT: ?',...
      'color',nnltgray)

    % CLEAR NETWORK DEPENDENT STUFF
    feval(me,'clear')

    % STORE DATA
    set(fruit_ptr,'userdata',[]);
  end

%==================================================================
% Setup network specific parts of window.
%
% ME('setup')
%==================================================================

elseif strcmp(cmd,'setup') & (fig) & (nargin == 1)

  set(fig,'nextplot','add')

  % EXPRESSION
  x = 220;
  text(x,340,'W1 = [1 -1 -1; 1 1 -1]',...
    'color',nndkblue,...
    'fontweight','bold',...
    'fontsize',10)
  text(x,317,'b1 = [3; 3]',...
    'color',nndkblue,...
    'fontweight','bold',...
    'fontsize',10)
  text(x,295,'W2 = [1 -0.5; -0.5 1]',...
    'color',nndkblue,...
    'fontweight','bold',...
    'fontsize',10)
  line1 = text(x,272,'p = [?; ?; ?]',...
    'color',nnltgray,...
    'erasemode','none',...
    'fontweight','bold',...
    'fontsize',10);
  line2 = text(x,250,'a1 = purelin(W1*p+b1)',...
    'color',nnltgray,...
    'erasemode','none',...
    'fontweight','bold',...
    'fontsize',10);
  line3 = text(x,227,'a1 = [?; ?]',...
    'color',nnltgray,...
    'erasemode','none',...
    'fontweight','bold',...
    'fontsize',10);
  line4 = text(x,205,'a2(1) = poslin(W2*a1)',...
    'color',nnltgray,...
    'erasemode','none',...
    'fontweight','bold',...
    'fontsize',10);
  line5 = text(x,182,'a2(1) = [?; ?]',...
    'color',nnltgray,...
    'erasemode','none',...
    'fontweight','bold',...
    'fontsize',10);
  line6 = text(x,160,'Fruit = ?',...
    'color',nnltgray,...
    'erasemode','none',...
    'fontweight','bold',...
    'fontsize',10);

  % INPUT SPACE AXES
  ext = 1.25;
  input_axis = nnsfo('a4','Input Space','shape','texture','weight');
  set(input_axis,...
    'xlim',[-1 1]*ext,'xtick',[-1 1],...
    'ylim',[-1 1]*ext,'ytick',[-1 1],...
    'zlim',[-1 1]*ext,'ztick',[-1 1],...
    'pos',[48 175 140 140],...
    'box','off');
  view(3)
  
  % EDGE
  edge = plot3([-1 -1 -1 1 1 1 -1]*ext,[-1 1 1 1 -1 -1 -1]*ext,...
    [-1 -1 1 1 1 -1 -1]*ext,...
    'color',nndkblue,...
    'erasemode','none');

  % BOX
  box1 = plot3([-1 1 1 -1 -1],[-1 -1 -1 -1 -1],[-1 -1 1 1 -1],':',...
    'color',nndkblue,...
    'erasemode','none');
  box2 = plot3([-1 1 1 -1 -1],[1 1 1 1 1],[-1 -1 1 1 -1],':',...
    'color',nndkblue,...
    'erasemode','none');
  box3 = plot3([-1 -1 -1 -1 -1],[-1 1 1 -1 -1],[-1 -1 1 1 -1],':',...
    'color',nndkblue,...
    'erasemode','none');
  box4 = plot3([1 1 1 1 1],[-1 1 1 -1 -1],[-1 -1 1 1 -1],':',...
    'color',nndkblue,...
    'erasemode','none');

  boundary = plot3([-1 1 1 -1 -1],[0 0 0 0 0],[-1 -1 1 1 -1],...
    'color',nndkblue,...
    'erasemode','none');

  % PROTOTYPE ORANGE
  orange_dot = plot3(1,-1,-1,'.',...
    'markersize',25,...
    'color',[1 0.5 0],...
    'erasemode','none');

  % PROTOTYPE APPLE
  apple_dot = plot3(1,1,-1,'.',...
    'markersize',25,...
    'color',[1 0 0],...
    'erasemode','none');

  % STORE DATA
  p_mark_ptr = uicontrol('visible','off','userdata',[]);
  lines_ptr = uicontrol('visible','off','userdata',...
     [line1 line2 line3 line4 line5 line6]);
  H = [input_axis, ...
       orange_dot apple_dot,...
       edge box1 box2 box3 box4 boundary,...
       lines_ptr ,...
       p_mark_ptr];
  set(net_ptr,'userdata',H);

  set(fig,'nextplot','new')

%==================================================================
% Classify fruit with network.
%
% ME('classify')
%==================================================================

elseif strcmp(cmd,'classify') & (fig) & (nargin == 1)

  % GET DATA
  p = get(p_ptr,'userdata');
  H = get(net_ptr,'userdata');
  input_axis = H(1);           % handle to input space axis
  dots = H(2:3);               % handles to orange & apple dots
  frame = H(4:9);              % frame & boundary
  lines_ptr = H(10);
  p_mark_ptr = H(11);          % pointer to input marks

  lines = get(lines_ptr,'userdata');

  axes(input_axis);
  set(fig,'nextplot','add')

  % SHOW INPUT LINES
  p1 = p(1);
  p2 = p(2);
  p3 = p(3);
  p_marks = plot3([p1 p1 NaN p1 p1 NaN -1 1],[p2 p2 NaN -1 1 NaN p2 p2],...
    [-1 1 NaN p3 p3 NaN p3 p3],'-',...
    'color',nngreen,...
    'erasemode','none');
  
  % FLASH INPUT LINES
  for i=1:8
    nnpause(0.075)
    set(p_marks,...
      'color',nnltyell)
    set(dots(1),...
      'color',[1 0.5 0])
    set(dots(2),...
      'color',[1 0 0])
    set(frame,...
      'color',nndkblue)
    nnpause(0.05)
    set(p_marks,...
      'color',nngreen)
  end

  % DRAW DISTANCES
  dist_line1 = plot3([p1 1],[p2 -1],[p3 -1],...
    'color',[1 0.5 0],...
    'erasemode','none');
  dist_line2 = plot3([p1 1],[p2 1],[p3 -1],...
    'color',[1 0 0],...
    'erasemode','none');
  dist_lines = [dist_line1 dist_line2];

  % FLASH DISTANCES
  for i=1:8
    nnpause(0.075)
    set(dist_lines,...
      'color',nnltyell)
    set(dots(1),...
      'color',[1 0.5 0])
    set(dots(2),...
      'color',[1 0 0])
    set(frame,...
      'color',nndkblue)
    set(p_marks,...
      'color',nngreen)
    nnpause(0.05)
    set(dist_line1,...
      'color',[1 0.5 0])
    set(dist_line2,...
      'color',[1 0 0])
  end
  p_marks = [p_marks dist_lines];

  % PERFORM CALCULATION
  W1 = [1 -1 -1; 1 1 -1];
  b1 = [3; 3];
  W2 = [1 -0.5; -0.5 1];
  iter = 5;

  a1 = purelin(W1*p);
  a2 = a1;

  % SHOW FIRST CALCULATION
  axes(fig_axis)
  nntxtchk;
  for i=1:5
    if i == 1
      str = ['p = [',num2str(p1),';',num2str(p2),';',num2str(p3),']'];
    elseif i == 2
      str = 'a1 = purelin(W1*p+b)';
    elseif i == 3
      str = ['a1 = [' num2str(a1(1)) '; ' num2str(a1(2)) ']'];
    elseif i == 4
      str = ['a2(0) = a1'];
    else
      str = ['a2(0) = [' num2str(a2(1)) '; ' num2str(a2(2)) ']'];
    end
    nnpause(1)

    set(lines(i),...
      'string',str,...
      'color',nndkblue);
  end

  % REPEAT CALCULATION
  i = 1;
  while all(a2 ~= 0)
    nnpause(1)

    set(lines(4),...
      'color',nnltgray);

    str = ['a2(' num2str(i) ') = poslin(W2*a2(' num2str(i-1) '))'];
    set(lines(4),...
      'string',str,...
      'color',nndkblue);

    nnpause(1)
    a2 = max(0,W2*a2);
    a2 = round(a2*100)*0.01;

    set(lines(5),...
      'color',nnltgray);

    str = ['a2(' num2str(i) ') = [' num2str(a2(1)) '; ' num2str(a2(2)) ']'];
    set(lines(5),...
      'string',str,...
      'color',nndkblue);
    i = i + 1;
  end

  if (a2(1) > 0)
    fruit_str = 'Orange';
    fruit_c = [1 0.5 0];
  else
    fruit_str = 'Apple';
    fruit_c = [1 0 0];
  end

  % SHOW FRUIT
  nnpause(1)
  str = ['Fruit = ' fruit_str];
  set(lines(6),...
   'string',str,...
   'color',nndkblue)

  set(p_mark_ptr,'userdata',p_marks)
  set(fig,'nextplot','new')

%==================================================================
% Clear fruit calculations.
%
% ME('clear')
%==================================================================

elseif strcmp(cmd,'clear') & (fig) & (nargin == 1)

  % GET DATA
  p = get(p_ptr,'userdata');
  H = get(net_ptr,'userdata');
  input_axis = H(1);           % handle to input space axis
  dots = H(2:3);               % handles to orange & apple dots
  frame = H(4:9);              % frame & boundary
  lines_ptr = H(10);
  p_mark_ptr = H(11);          % pointer to input marks
  
  lines = get(lines_ptr,'userdata');
  
  % HIDE INPUT LINES
  p_marks = get(p_mark_ptr,'userdata');
  set(p_marks,...
    'color',nnltyell)
  delete(p_marks)
  set(dots(1),...
    'color',[1 0.5 0])
  set(dots(2),...
    'color',[1 0 0])
  set(frame,...
    'color',nndkblue)
  
  % HIDE CALCULATION
  axes(fig_axis)
  set(fig,'nextplot','add')
  set(lines,'color',nnltgray);
  nntxtchk;

  set(fig,'nextplot','new')
  nntxtchk;
end

⌨️ 快捷键说明

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