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

📄 nnd10lc.m

📁 《神经网络设计》(neural networks design)的源代码
💻 M
📖 第 1 页 / 共 2 页
字号:
  indicator = line(cos(angle)*[0.07 0.85],sin(angle)*[0.07 0.85],...
    'color',nnred,...
    'linewidth',2,...
    'erasemode','none');

  % BUTTONS
  drawnow;
  if (exist('hintonwb'))
    uicontrol(...
      'units','points',...
      'position',[280 300 60 20],...
      'string','Weights',...
      'callback',[me '(''weight'')'])
  end
  uicontrol(...
    'units','points',...
    'position',[410 146 60 20],...
    'string','Train',...
    'callback',[me '(''train'')'])
  uicontrol(...
    'units','points',...
    'position',[410 110 60 20],...
    'string','Contents',...
    'callback','nndtoc')
  uicontrol(...
    'units','points',...
    'position',[410 110-36 60 20],...
    'string','Close',...
    'callback','delete(gcf)')

  % DATA POINTERS
  P_ptr = uicontrol('visible','off'); set(P_ptr,'userdata',P);
  w_ptr = uicontrol('visible','off'); set(w_ptr,'userdata',w);
  b_ptr = uicontrol('visible','off'); set(b_ptr,'userdata',b);
  a_ptr = uicontrol('visible','off'); set(a_ptr,'userdata',a);
  T_ptr = uicontrol('visible','off'); set(T_ptr,'userdata',T);
  p_ptr = uicontrol('visible','off'); set(p_ptr,'userdata',P(:,1));
  
  % SAVE WINDOW DATA AND LOCK
  H = [fig_axis desc_text pattern P_ptr w_ptr select indicator center ...
    b_ptr a_ptr T_ptr error_line p_ptr];
  set(fig,'userdata',H,'nextplot','new','color',nnltgray)

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

  nnchkfs;

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

elseif strcmp(cmd,'instr') & (fig)
  nnsettxt(desc_text,...
    'Edit the RED grids',...
    'and watch the output',...
    'meter respond to the',...
    'new inputs.',...
    '',...
    'Edit the GREEN grid',...
    'and then click [Train]',...
    'to study a different',...
    'problem.',...
    '',...
    'Use GREEN patterns',...
    'as inputs by clicking',...
    'the arrow buttons.')

%==================================================================
% Respond to mouse down.
%
% ME('down')
%==================================================================

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

  q = 0;
  for i=1:7
    pt = get(pattern(i),'currentpoint');
    x = pt(1);
    y = pt(3);
    if (x > 0) & (x < p_size) & (y > 0) & (y < p_size)
      q = i;
      break;
    end
  end

  if (q)
    % GET DATA
    x = floor(x)+1;
    y = floor(y)+1;
    ltyell = nnltyell;
    green = nngreen;
    red = nnred;
    squares = get(pattern(i),'userdata');
  
    % TOGGLE SQUARE
    ind = (x-1)*p_size+y;
    if (q<7)
      P = get(P_ptr,'userdata');
      P(ind,q) = -P(ind,q);
      if (P(ind,q) == 1)
        set(squares(x,y),'facecolor',green);
      else
        set(squares(x,y),'facecolor',ltyell);
      end
      set(P_ptr,'userdata',P);
    else
      p = get(p_ptr,'userdata');
      p(ind) = -p(ind);
      if (p(ind) == 1)
        set(squares(x,y),'facecolor',red);
      else
        set(squares(x,y),'facecolor',ltyell);
      end
      set(p_ptr,'userdata',p);
      cmd = 'meter';
    end
  end

%==================================================================
% Select pattern.
%
% ME('select',i)
%==================================================================

elseif strcmp(cmd,'select') & (fig) & (nargin == 2)

  % GET DATA
  i = arg1;
  P = get(P_ptr,'userdata');
  p = get(p_ptr,'userdata');
  w = get(w_ptr,'userdata');
  b = get(b_ptr,'userdata');
  old_a = get(a_ptr,'userdata');
  ltyell = nnltyell;
  red = nnred;
  dkblue = nndkblue;


  % UPDATE TEST INPUT
  p = P(:,i);
  squares = get(pattern(7),'userdata');
  for x=1:p_size,for y=1:p_size
    ind = (x-1)*p_size+y;
    if (p(ind) == 1)
      set(squares(x,y),'facecolor',red);
    else
      set(squares(x,y),'facecolor',ltyell);
    end
  end,end
  set(p_ptr,'userdata',p);
  cmd = 'meter';

%==================================================================
% Show weights.
%
% ME('weight')
%==================================================================

elseif strcmp(cmd,'weight') & (fig)

  % GET DATA
  w = get(w_ptr,'userdata');
  b = get(b_ptr,'userdata');

  f = figure;
  feval('hintonwb',w,b);
  axis('equal');
  set(f,'name','Linear Classifier Bias & Weights')
end

%==================================================================
% Train network.
%
% ME('train')
%==================================================================

if strcmp(cmd,'train') & (fig)

  % CONSTANTS
  lr = 0.03;
  me = 100;

  % GET DATA
  P = get(P_ptr,'userdata');
  T = get(T_ptr,'userdata');

  % TRAINING
  w = zeros(1,16);
  b = 0;
  sse = zeros(epochs+1,1);
  sse(1) = sum(sum((T-w*P-b).^2));
  for i=1:epochs
    q = fix(rand*6)+1;
    p = P(:,q);
    t = T(:,q);
    a = w*p+b;
    e = t-a;
    w = w + 2*lr*e*p';
    b = b + 2*lr*e;
    
    sse(i+1) = sum(sum((T-w*P-b).^2));
  end

  % NEW ERRORS
  set(error_line,'color',nnltyell);
  set(error_line,...
    'ydata',sse,...
    'color',nnred,'erasemode','normal')
  
  % SAVE DATA
  set(w_ptr,'userdata',w);
  set(b_ptr,'userdata',b);
  cmd = 'meter';

end

%==================================================================
% Update the meter.
%
% ME('meter')
%==================================================================

if strcmp(cmd,'meter') & (fig)

  % GET DATA
  p = get(p_ptr,'userdata');
  old_a = get(a_ptr,'userdata');
  w = get(w_ptr,'userdata');
  b = get(b_ptr,'userdata');
  dkblue = nndkblue;
  red = nnred;
  ltyell = nnltyell;

  % UPDATE METER
  old_angle = (90-old_a);
  a = w*p+b;
  new_angle = (90-a);

  if (abs(old_a-a) > 0.5)
    angles = [old_angle:5*sign(new_angle-old_angle):new_angle, new_angle];
    sounds = (180-angles)/180*2000+400;
    angles = min(pi,max(0,angles*pi/180));
    for i=1:length(angles)
      angle = angles(i);

      if (angle == pi | angle == 0)
        color = dkblue;
      else
        color = red;
      end

      t1 = clock;
      set(indicator, ...
        'color',ltyell)
      set(indicator, ...
        'color',color,...
        'xdata',cos(angle)*[0.07 0.85],...
        'ydata',sin(angle)*[0.07 0.85])
      drawnow
      time = [0:(1/Fs):0.015];
      w = time*2*pi;
      nnsound([0 0 0 0 sin(w*sounds(i)) 0 0 0 0],Fs);
      while etime(clock,t1) < 0.015,end
    end
  end

  % STORE DATA
  set(a_ptr,'userdata',a);
end

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

⌨️ 快捷键说明

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