📄 nnd4db.m
字号:
function nnd4db(cmd,arg1,arg2,arg3)
%NND4DB Decision boundaries demonstration.
%
% This demonstration requires either the MININNET functions
% on the NND disk or the Neural Network Toolbox.
% First Version, 8-31-95.
%==================================================================
% CONSTANTS
me = 'nnd4db';
p_max = 3;
% 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
v_axis = H(3); % training and weight vector axis
t1_axis = H(4); % black target marker axis
cross = H(5); % dotted origin lines
w_arrow_ptr = H(6); % pointer to weight vector & name
db_line = H(7); % decision boundary
db1 = H(8); % decision boundary point #1
db2 = H(9); % decision boundary point #2
w_ptr = H(10); % pointer to weights
b_ptr = H(11); % pointer to bias
pt_ptr = H(12); % pointer to point positions
w1_text = H(13); % text for w(1) value
w2_text = H(14); % text for w(2) value
b_text = H(15); % text for b value
P_ptr = H(16); % pointer to input vectors
T_ptr = H(17); % pointer to targets
dots_ptr = H(18); % pointer to input/target dots
t2_axis = H(19); % white target marker axis
dot_ptr = H(20); % pointer to dot being edited
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 ~nnfexist(me), return, end
% CONSTANTS
pt1 = [-1 0];
pt2 = [0 -2];
pt = [pt1; pt2];
w_dir = [pt(1,2)-pt(2,2),pt(2,1)-pt(1,1)];
w = w_dir/sqrt(sum(sum(w_dir.^2)))*2;
b = -w*pt(1,:)';
P = [0 0; 0 1; 1 0; 1 1]';
T = [0 0 0 1];
% NEW DEMO FIGURE
fig = nndemof2(me,'DESIGN','Decision Boundaries','','Chapter 4');
set(fig, ...
'windowbuttondownfcn',nncallbk(me,'down'), ...
'BackingStore','off',...
'nextplot','add');
H = get(fig,'userdata');
fig_axis = H(1);
desc_text = H(2);
% ICON
nndicon(4,458,363,'shadow')
% VALUES
bracket_x = [1 0 0 1]*10;
bracket_y = [0 0 1 1]*10;
text(55,40,'W=',...
'fontsize',20,...
'fontweight','bold',...
'color',nndkblue,...
'horiz','right')
plot(bracket_x+60,bracket_y*4+20,'color',nndkblue,'linewidth',3);
plot(-bracket_x+190,bracket_y*4+20,'color',nndkblue,'linewidth',3);
w1_text = text(95,40,sprintf('%5.3g',w(1)),...
'fontsize',16,...
'fontweight','normal',...
'color',nndkblue,...
'horiz','center',...
'erasemode','none');
w2_text = text(150,40,sprintf('%5.3g',w(2)),...
'fontsize',16,...
'fontweight','normal',...
'color',nndkblue,...
'horiz','center',...
'erasemode','none');
text(255,40,'b=',...
'fontsize',20,...
'fontweight','bold',...
'color',nndkblue,...
'horiz','right')
plot(bracket_x+270,bracket_y*4+20,'color',nndkblue,'linewidth',3);
plot(-bracket_x+340,bracket_y*4+20,'color',nndkblue,'linewidth',3);
b_text = text(305,40,sprintf('%5.3g',b),...
'fontsize',16,...
'fontweight','normal',...
'color',nndkblue,...
'horiz','center',...
'erasemode','none');
% VECTOR DIAGRAM
v_axis = nnsfo('a2','','','');
set(v_axis,...
'position',[90 90 240*1.05 240*1.05],...
'xlim',[-p_max p_max]*1.05,...
'xtick',-p_max:p_max,...
'ylim',[-p_max p_max]*1.05,...
'ytick',-p_max:p_max)
% OBJECTS
zz1 = [-p_max:0.5:p_max];
zz2 = zz1*0;
cross = plot([zz1 NaN zz2],[zz2 NaN zz1],'.',...
'color',nndkblue,...
'erasemode','none');
A = hardlim(w*P,b);
if all(A == T)
col = nndkgray;
else
col = nnred;
end
w_arrow = nndrwvec(w(1),w(2),2,0.2,col,'W','none');
if (w(1) ~= 0)
pp2 = [-p_max p_max];
pp1 = -(w(2)*pp2+b)/w(1);
elseif (w(2) ~= 0)
pp1 = [-p_max p_max];
pp2 = -(w(1)*pp1+b)/w(2);
else
pp1 = [0 0];
pp2 = [0 0];
end
db_line = plot(pp1,pp2,...
'linewidth',2,...
'color',nndkblue,...
'erasemode','none');
db1 = plot(pt1(1),pt1(2),'o',...
'color',nndkblue,...
'markersize',10,...
'erasemode','none');
db2 = plot(pt2(1),pt2(2),'o',...
'color',nndkblue,...
'markersize',10,...
'erasemode','none');
q = length(T);
deg = pi/180;
angle = [0:5:360]*deg;
cx = cos(angle)*0.15;
cy = sin(angle)*0.15;
dots = zeros(1,q);
for i=1:q
a = hardlim(w*P(:,i)+b);
if (a == T(i))
col = nndkgray;
else
col = nnred;
end
dots(i) = fill(cx+P(1,i),cy+P(2,i),[1 1 1]-T(i),...
'edgecolor',col,...
'erasemode','none',...
'linewidth',2);
end
% TARGET AXES
t1_axis = nnsfo('a2','','','');
set(t1_axis,...
'position',[30 230 20*1.05 20*1.05],...
'xlim',[-.16 .16],...
'ylim',[-.16 .16])
axis('off')
fill(cx,cy,[0 0 0],...
'edgecolor',nndkgray,...
'linewidth',2)
t2_axis = nnsfo('a2','','','');
set(t2_axis,...
'position',[30 180 20*1.05 20*1.05],...
'xlim',[-.16 .16],...
'ylim',[-.16 .16])
axis('off')
fill(cx,cy,[1 1 1],...
'edgecolor',nndkgray,...
'linewidth',2)
% BUTTONS
drawnow % Let everything else appear before buttons
uicontrol(...
'units','points',...
'position',[400 110 60 20],...
'string','Contents',...
'callback','nndtoc')
uicontrol(...
'units','points',...
'position',[400 75 60 20],...
'string','Close',...
'callback',[me '(''close'')'])
% DATA POINTERS
w_ptr = uicontrol('visible','off'); set(w_ptr,'userdata',w);
b_ptr = uicontrol('visible','off'); set(b_ptr,'userdata',b);
pt_ptr = uicontrol('visible','off'); set(pt_ptr,'userdata',pt);
w_arrow_ptr = uicontrol('visible','off'); set(w_arrow_ptr,'userdata',w_arrow);
P_ptr = uicontrol('visible','off'); set(P_ptr,'userdata',P);
T_ptr = uicontrol('visible','off'); set(T_ptr,'userdata',T);
dots_ptr = uicontrol('visible','off'); set(dots_ptr,'userdata',dots);
dot_ptr = uicontrol('visible','off'); set(dot_ptr,'userdata',[]);
% SAVE WINDOW DATA AND LOCK
H = [fig_axis desc_text v_axis t1_axis cross w_arrow_ptr db_line...
db1 db2 w_ptr b_ptr pt_ptr w1_text w2_text b_text P_ptr T_ptr ...
dots_ptr t2_axis dot_ptr];
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,...
'Move the perceptron',...
'decision boundary by',...
'dragging its handles.',...
'',...
'Try to divide the',...
'circles so that none',...
'of their edges are red.',...
'',...
'The weights and bias',...
'will take on values',...
'associated with the',...
'chosen boundary.',...
'',...
'Drag the white and',...
'black dots to define',...
'different problems.')
%==================================================================
% Respond to mouse down.
%
% ME('down')
%==================================================================
elseif strcmp(cmd,'down') & (fig) & (nargin == 1)
pt = get(v_axis,'currentpoint');
x = pt(1);
y = pt(3);
% EDIT VECTOR AXIS
if (x >= -p_max) & (x <= p_max) & (y >= -p_max) & (y <= p_max)
% GET DATA
pt = get(pt_ptr,'userdata');
w_arrow = get(w_arrow_ptr,'userdata');
% EDIT DECISION BOUNDARY PT #1
if sum(sum((pt(1,:)-[x y]).^2)) < 0.2
set(db1,...
'color',nnltyell)
set(db_line,...
'color',nnltyell)
set(w_arrow,...
'color',nnltyell)
set(w1_text,...
'color',nnltgray)
set(w2_text,...
'color',nnltgray)
set(b_text,...
'color',nnltgray)
nntxtchk;
set(db2,...
'color',nndkblue)
set(cross,...
'color',nndkblue)
dots = get(dots_ptr,'userdata');
T = get(T_ptr,'userdata');
for i=1:length(T)
set(dots(i),...
'facecolor',[1 1 1]-T(i))
end
set(fig,...
'pointer','circle',...
'WindowButtonMotionFcn',[me '(''movept1'')'],...
'WindowButtonUpFcn',[me '(''setpt1'')'])
% EDIT DECISION BOUNDARY PT #1
elseif sqrt(sum(sum((pt(2,:)-[x y]).^2))) < 0.2
set(db2,...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -