Vous êtes sur la page 1sur 46

simple neuron and transmission function

function nnd2n1(cmd,arg1,arg2,arg3)
%NND2N1 One-input neuron demonstration.
%
% This demonstration requires either the MININNET functions
% on the NND disk or the Neural Network Toolbox.

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

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

% CONSTANTS
me = 'nnd2n1';
max_t = 0.5;
w_max = 2;
p_max = 4;

% 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
meters = H(3:6); % input and output meters (axes)
indicators = H(7:9); % input and output indicators (patches)
lines = H(10:12); % lines in neuron function axis
w_ptr = H(13); % pointer to weight vector
b_ptr = H(14); % pointer to bias value
f_ptr = H(15); % pointer to transfer function
p_ptr = H(16); % pointer to input vector
f_menu = H(17); % transfer function menu
f_text = H(18); % neuron heading text
cross = H(19); % origon cross lines
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 TRANSFER FUNCTIONS


if ~nnfexist(me), return, end

% CONSTANTS
w = 1;
b = 0;
p = 0;
f = 'purelin';
n = w*p+b;
a = feval(f,n);
title_str = 'Neuron Model Demonstration';
chapter_str = 'Chapter 2';
a_lim = [-2 2];
P = [-p_max:0.1:p_max];
[R,Q] = size(P);
A = feval(f,w*P+b*ones(1,Q));
ind = find(A >= -p_max & A <= p_max);
P = P(ind);
A = A(ind);

% NEW DEMO FIGURE


fig = nndemof2(me,'DESIGN','One-Input Neuron','','Chapter 2');
set(fig, ...
'windowbuttondownfcn',nncallbk(me,'down'), ...
'BackingStore','off',...
'nextplot','add');
H = get(fig,'userdata');
fig_axis = H(1);
desc_text = H(2);

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

% NEURON DIAGRAM
x = 60;
y = 340;
plot(x+[0 100],y-[50 50],...
'linewidth',4,...
'color',nnred);
plot(x+[100 100],y-[49 91],...
'linewidth',4,...
'color',nnred);
nndicon(100,x+100,y-50)
plot(x+[125 185],y-[50 50],...
'linewidth',4,...
'color',nnred);
plot(x+[200 250],y-[50 50],...
'linewidth',4,...
'color',nnred);
plot(x+[240 250 240],y-[40 50 60],...
'linewidth',4,...
'color',nnred);
nndicon(101,x+200,y-50)
fill(x+[90 110 110 90],y-[115 115 95 95],nnltyell,...
'edgecolor',nndkblue)
text(x+101,y-106,'1',...
'color',nndkblue,...
'fontweight','bold',...
'horiz','center');

text(x-10,y-50,'p',...
'color',nndkblue,...
'fontweight','bold',...
'horiz','center',...
'erasemode','none');
text(x+40,y-40,'w',...
'color',nndkblue,...
'fontweight','bold',...
'horiz','center',...
'erasemode','none');
text(x+115,y-85,'b',...
'color',nndkblue,...
'fontweight','bold',...
'horiz','center',...
'erasemode','none');
text(x+265,y-50,'a',...
'color',nndkblue,...
'fontweight','bold',...
'horiz','center',...
'erasemode','none');

deg = pi/180;
angle = [0:5:90]*deg;
xc = cos(angle)*10;
yc = sin(angle)*10;

plot(x-20-xc,y-20+yc,...
'linewidth',3,...
'color',nndkblue);
plot(x+[-20 0],y-[10 10],...
'linewidth',3,...
'color',nndkblue);
plot(x+xc,y-20+yc,...
'linewidth',3,...
'color',nndkblue);
plot(x-20-xc,y-115-yc,...
'linewidth',3,...
'color',nndkblue);
plot(x+[-20 0],y-[125 125],...
'linewidth',3,...
'color',nndkblue);
plot(x+xc,y-115-yc,...
'linewidth',3,...
'color',nndkblue);
text(x-10,y+5,'Input',...
'color',nndkblue,...
'fontweight','bold',...
'horiz','center');

plot(x+30-xc,y-20+yc,...
'linewidth',3,...
'color',nndkblue);
plot(x+[30 280],y-[10 10],...
'linewidth',3,...
'color',nndkblue);
plot(x+280+xc,y-20+yc,...
'linewidth',3,...
'color',nndkblue);
plot(x+30-xc,y-115-yc,...
'linewidth',3,...
'color',nndkblue);
plot(x+[30 280],y-[125 125],...
'linewidth',3,...
'color',nndkblue);
plot(x+280+xc,y-115-yc,...
'linewidth',3,...
'color',nndkblue);
f_text = text(x+155,y+5,'Linear Neuron: a = purelin(w*p+b)',...
'color',nndkblue,...
'fontweight','bold',...
'horiz','center',...
'erasemode','none');

% PARAMETERS
y = 160;
x = 40;
text(x+40,y-100,'F:',...
'fontname','helvetica',...
'fontweight','bold',...
'color',nndkblue,...
'horiz','center',...
'fontsize',12)

w_axis = nnsfo('a2','w','','');

set(w_axis, ...
'units','points',...
'position',[x y 70 20],...
'color',nnmdgray,...
'ylim',[-0.3 1.3],...
'ytick',[],...
'xlim',[-1.3 1.3]*w_max,...
'xtick',[-1 -0.5 0 0.5 1]*w_max,...
'xticklabel',str2mat(num2str(-w_max),'','0','',num2str(w_max)))

w_ind = fill([0 0.2 -0.2]*w_max+w,[0 1 1],nnred,...


'edgecolor',nndkblue,...
'erasemode','none');

b_axis = nnsfo('a2','b','','');
set(b_axis, ...
'units','points',...
'position',[x y-65 70 20],...
'color',nnmdgray,...
'ylim',[-0.3 1.3],...
'ytick',[],...
'xlim',[-1.3 1.3]*w_max,...
'xtick',[-1 -0.5 0 0.5 1]*w_max,...
'xticklabel',str2mat(num2str(-w_max),'','0','',num2str(w_max)))

b_ind = fill([0 0.2 -0.2]*w_max+b,[0 1 1],nnred,...


'edgecolor',nndkblue,...
'erasemode','none');
f_menu = uicontrol(...
'units','points',...
'position',[x y-130, 80 20],...
'style','popupmenu',...
'string','Hardlim|Hardlims|Purelin|Satlin|Satlins|Logsig|Tansig',...
'background',nnmdgray,...
'callback',[me '(''function'')'],...
'value',3);

% NEURON FUNCTION
y = 40;
x = 200;
a_axis = nnsfo('a2','','p','a');
set(get(a_axis,'xlabel'),...
'fontsize',12)
set(get(a_axis,'ylabel'),...
'fontsize',12)
set(a_axis, ...
'units','points',...
'position',[x y 140 140],...
'color',nnltyell,...
'xlim',[-1.05 1.05]*p_max,...
'xtick',[-4 -2 0 2 4],...
'ylim',[-1.05 1.05]*p_max,...
'ytick',[-4 -2 0 2 4])
cross = plot([-p_max p_max NaN 0 0],[0 0 NaN -p_max p_max],':',...
'color',nndkblue,...
'erasemode','none');
p_line = plot([p p],[-p_max p_max],'--',...
'color',nnred,...
'erasemode','none');
a = feval(f,w*p+b);
a_line = line([-p_max p],[a a],...
'color',nnred,...
'erasemode','none');
A_line = line(P,A,...
'color',nndkblue,...
'erasemode','none',...
'linewidth',2);
p_axis = nnsfo('a2','','','');
set(p_axis, ...
'units','points',...
'position',[x y+140 140 15],...
'color',nnltyell,...
'xlim',[-1.05 1.05]*p_max,...
'xtick',[-4 -2 0 2 4],...
'ylim',[-0.1 1.1],...
'ytick',[])
axis('off')
p_ind = fill([0 0.125 -0.125]*p_max+p,[0 1 1],nnred,...
'edgecolor',nndkblue,...
'erasemode','none');

% 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
meters = [w_axis b_axis p_axis a_axis];
indicators = [w_ind b_ind p_ind];
lines = [p_line a_line A_line];
w_ptr = uicontrol('visible','off'); set(w_ptr,'userdata',w);
b_ptr = uicontrol('visible','off'); set(b_ptr,'userdata',b);
f_ptr = uicontrol('visible','off'); set(f_ptr,'userdata',f);
p_ptr = uicontrol('visible','off'); set(p_ptr,'userdata',p);

% SAVE WINDOW DATA AND LOCK


H = [fig_axis desc_text meters indicators lines w_ptr b_ptr f_ptr p_ptr
...
f_menu f_text cross];
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,...
'Alter the weight, bias',...
'and input by dragging',...
'the triangular shaped',...
'indicators.',...
'',...
'Pick the transfer',...
'function with the',...
'F menu.',...
'',...
'Watch the change to',...
'the neuron function',...
'and its output.')

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

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

q = 0;
for i=1:3
pt = get(meters(i),'currentpoint');
x = pt(1);
y = pt(3);

if (i <= 2)
if (x >= -1.3*w_max) & (x <= 1.3*w_max) & (y >= 0) & (y <= 1)
q = i;
z_max = w_max;
z = x;
hide_color = nnmdgray;
width = 0.2;
break;
end
else
if (x >= -1.1*p_max) & (x <= 1.1*p_max) & (y >= 0) & (y <= 1)
q = i;
z_max = p_max;
z = x;
hide_color = nnltgray;
width = 0.125;
break;
end
end
end

if (q)
set(fig,'pointer','crosshair')
z = min(z_max,max(-z_max,z));
set(indicators(q),...
'facecolor',hide_color,...
'edgecolor',hide_color)
set(indicators(q),...
'xdata',[0 1 -1]*width*z_max+z,...
'facecolor',nnred,...
'edgecolor',nndkblue)
set(fig,'WindowButtonMotionFcn',[me '(''down'')']);
set(fig,'WindowButtonUpFcn',[me '(''up'')']);

% ALTER VARIABLES
if (q == 1)
set(w_ptr,'userdata',z);
change_func = 1;
elseif (q == 2)
set(b_ptr,'userdata',z);
change_func = 1;
else
set(p_ptr,'userdata',z);
end
cmd = 'update';
else
set(fig,'pointer','arrow')
end

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

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

set(fig,...
'WindowButtonMotionFcn','',...
'pointer','arrow')

%==================================================================
% Respond to function menu.
%
% ME('function')
%==================================================================

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

v = get(f_menu,'value');

if v == 1, f = 'hardlim'; new_text = 'Hard Limit Neuron';


elseif v == 2, f = 'hardlims'; new_text = 'Sym. Hard Limit Neuron';
elseif v == 3, f = 'purelin'; new_text = 'Linear Neuron';
elseif v == 4, f = 'satlin'; new_text = 'Saturating Linear Neuron';
elseif v == 5, f = 'satlins'; new_text = 'Sym. Sat. Linear Neuron';
elseif v == 6, f = 'logsig'; new_text = 'Log Sigmoid Neuron';
elseif v == 7, f = 'tansig'; new_text = 'Tan Sigmoid Neuron';
elseif v == 8, f = 'radbas'; new_text = 'Log Sigmoid Neuron';
end

new_text = [new_text ': a = ' f '(w*p+b)'];


set(f_text,...
'color',nnltgray);
set(f_text,...
'string',new_text,...
'color',nndkblue);
set(f_ptr,'userdata',f);
change_func = 1;
cmd = 'update';

%==================================================================
end

%==================================================================
% Respond to request to update displays.
%
% ME('update')
%==================================================================
if strcmp(cmd,'update') & (fig)

% GET DATA
w = get(w_ptr,'userdata');
b = get(b_ptr,'userdata');
f = get(f_ptr,'userdata');
p = get(p_ptr,'userdata');

% UPDATE OUTPUT
n = w*p+b;
if strcmp(f,'satlin')
a = (~((n < 0) | (n > 1))).*n + (n > 1);
elseif strcmp(f,'satlins')
a = (~((n < -1) | (n > 1))).*n + (n > 1) - (n < -1);
else
a = feval(f,n);
end

% HIDE LINES
set(lines(1:2),...
'color',nnltyell)

% HIDE & RECALCULATE FUNCTION IF ALTERED


if (change_func)
set(lines(3),...
'color',nnltyell)
P = [-p_max:0.1:p_max];
[R,Q] = size(P);
A = feval(f,w*P+b*ones(1,Q));
ind = find(A >= -p_max & A <= p_max);
P = P(ind);
A = A(ind);
set(lines(3),...
'xdata',P,...
'ydata',A)
end

% REFRESH NEURON FUNCTION & CROSS LINE


set(lines(3),...
'color',nndkblue)
set(cross,...
'color',nndkblue)

% SHOW NEW LINES


set(lines(1),...
'xdata',[p p],...
'color',nnred)
if (a >= -p_max) & (a <= p_max)
set(lines(2),...
'xdata',[-p_max p],...
'ydata',[a a],...
'color',nnred)
set(lines(2),...
'visible','on')
else
set(lines(2),...
'ydata',[a a],...
'visible','off')
end
end
hasil output
neuron with vector input
function nnd2n2(cmd,arg1,arg2,arg3)
%NND2N2 Two-input neuron demonstration.
%
% This demonstration requires either the MININNET functions
% on the NND disk or the Neural Network Toolbox.

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

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

% CONSTANTS
me = 'nnd2n2';
max_t = 0.5;
w_max = 2;
p_max = 1;
n_max = 6;
a_max = 6;

% 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
meters = H(3:9); % input and output meters (axes)
indicators = H(10:16); % input and output indicators (patches)
w_ptr = H(17); % pointer to weight vector
b_ptr = H(18); % pointer to bias value
f_ptr = H(19); % pointer to transfer function
p_ptr = H(20); % pointer to input vector
f_menu = H(21); % transfer function menu
f_text = H(22); % neuron heading text
f_text2 = H(23); % neuron function text
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 TRANSFER FUNCTIONS


if ~nnfexist(me), return, end

% CONSTANTS
w = [0.5 -0.5]*w_max;
b = [0];
p = [0; 0];
f = 'purelin';
n = w*p+b;
a = feval(f,n);
title_str = 'Neuron Model Demonstration';
chapter_str = 'Chapter 2';

% NEW DEMO FIGURE


fig = nndemof2(me,'DESIGN','Two-Input Neuron','','Chapter 2');
set(fig, ...
'windowbuttondownfcn',nncallbk(me,'down'), ...
'BackingStore','off',...
'nextplot','add');
H = get(fig,'userdata');
fig_axis = H(1);
desc_text = H(2);

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

% NEURON DIAGRAM
x = 60;
y = 230;
plot(x+[0 100 0],y-[0 50 100],...
'linewidth',4,...
'color',nnred);
plot(x+[100 100],y-[49 112],...
'linewidth',4,...
'color',nnred);
nndicon(100,x+100,y-50)
plot(x+[125 136],y-[50 50],...
'linewidth',4,...
'color',nnred);
plot(x+[160 185],y-[50 50],...
'linewidth',4,...
'color',nnred);
plot(x+[200 250],y-[50 50],...
'linewidth',4,...
'color',nnred);
plot(x+[240 250 240],y-[40 50 60],...
'linewidth',4,...
'color',nnred);
nndicon(101,x+200,y-50)

deg = pi/180;
angle = [0:5:90]*deg;
xc = cos(angle)*10;
yc = sin(angle)*10;

plot(x-20-xc,y+70+yc,...
'linewidth',3,...
'color',nndkblue);
plot(x+[-20 0],y+[80 80],...
'linewidth',3,...
'color',nndkblue);
plot(x+xc,y+70+yc,...
'linewidth',3,...
'color',nndkblue);
plot(x-20-xc,y-160-yc,...
'linewidth',3,...
'color',nndkblue);
plot(x+[-20 0],y-[170 170],...
'linewidth',3,...
'color',nndkblue);
plot(x+xc,y-160-yc,...
'linewidth',3,...
'color',nndkblue);
text(x-10,y+95,'Input',...
'color',nndkblue,...
'fontweight','bold',...
'horiz','center');

plot(x+30-xc,y+70+yc,...
'linewidth',3,...
'color',nndkblue);
plot(x+[30 280],y+[80 80],...
'linewidth',3,...
'color',nndkblue);
plot(x+280+xc,y+70+yc,...
'linewidth',3,...
'color',nndkblue);
plot(x+30-xc,y-160-yc,...
'linewidth',3,...
'color',nndkblue);
plot(x+[30 280],y-[170 170],...
'linewidth',3,...
'color',nndkblue);
plot(x+280+xc,y-160-yc,...
'linewidth',3,...
'color',nndkblue);
f_text = text(x+155,y+95,'Linear Neuron',...
'color',nndkblue,...
'fontweight','bold',...
'horiz','center',...
'erasemode','none');
func_str = sprintf('a = %s(w*p+b) = %g',f,a);
f_text2 = text(x+155,y-190,'a = purelin(w*p+b)',...
'color',nndkblue,...
'fontweight','bold',...
'horiz','center',...
'erasemode','none');

text(x+155,y+45,'F',...
'fontname','helvetica',...
'fontweight','bold',...
'color',nndkblue,...
'horiz','center',...
'fontsize',12)

% SIGNALS
p1_axis = nnsfo('a2','p(1)','','');
set(p1_axis, ...
'units','points',...
'position',[x-20 y-35 20 70],...
'color',nnltyell,...
'xlim',[-0.3 1.3],...
'xtick',[],...
'ylim',[-1.3 1.3]*p_max,...
'ytick',[-1 -0.5 0 0.5 1]*p_max,...
'yticklabel',str2mat(num2str(-p_max),'','0','',num2str(p_max)))
p1_ind = fill([0 1 1],[0 0.2 -0.2]*p_max+p(1),nnred,...
'edgecolor',nndkblue,...
'erasemode','none');
p2_axis = nnsfo('a2','p(2)','','');
set(p2_axis, ...
'units','points',...
'position',[x-20 y-135 20 70],...
'color',nnltyell,...
'xlim',[-0.3 1.3],...
'xtick',[],...
'ylim',[-1.3 1.3]*p_max,...
'ytick',[-1 -0.5 0 0.5 1]*p_max,...
'yticklabel',str2mat(num2str(-p_max),'','0','',num2str(p_max)))
p2_ind = fill([0 1 1],[0 0.2 -0.2]*p_max+p(2),nnred,...
'edgecolor',nndkblue,...
'erasemode','none');
n_axis = nnsfo('a2','n','','');
set(n_axis, ...
'units','points',...
'position',[x+140 y-85 20 70],...
'color',nnltyell,...
'xlim',[-0.3 1.3],...
'xtick',[],...
'ylim',[-1.3 1.3]*n_max,...
'ytick',-n_max:2:n_max,...
'yticklabel',str2mat(num2str(-n_max),'','','','','',num2str(n_max)))
n_ind = fill([0 1 1],[0 0.2 -0.2]*n_max+n,nndkblue,...
'edgecolor','none',...
'erasemode','none');
a_axis = nnsfo('a2','a','','');
set(a_axis, ...
'units','points',...
'position',[x+270 y-120 20 140],...
'color',nnltyell,...
'xlim',[-0.3 1.3],...
'xtick',[],...
'ylim',[-1.15 1.15]*a_max,...
'ytick',[-a_max:1:a_max],...
'yticklabel',...
['-6';' ';'-4';' ';'-2';' ';'0 ';' ';'2 ';' ';'4 ';' ';'6 '])
a_ind = fill([0 1 1],[0 0.2 -0.2]*a_max/2+a,nndkblue,...
'edgecolor','none',...
'erasemode','none');
bi_axis = nnsfo('a2','','','');
set(bi_axis, ...
'units','points',...
'position',[x+90 y-135 20 20],...
'color',nnltyell,...
'xlim',[-1 1],...
'xtick',[],...
'ylim',[-1 1],...
'ytick',[])
bi_ind = text(0,0,'1',...
'color',nndkblue,...
'fontweight','bold',...
'horiz','center');

% PARAMETERS
w1_axis = nnsfo('a2','w(1,1)','','');
set(w1_axis, ...
'units','points',...
'position',[x+10 y+15 70 20],...
'color',nnmdgray,...
'ylim',[-0.3 1.3],...
'ytick',[],...
'xlim',[-1.3 1.3]*w_max,...
'xtick',[-1 -0.5 0 0.5 1]*w_max,...
'xticklabel',str2mat(num2str(-w_max),'','0','',num2str(w_max)))
w1_ind = fill([0 0.2 -0.2]*w_max+w(1),[0 1 1],nnred,...
'edgecolor',nndkblue,...
'erasemode','none');
w2_axis = nnsfo('a2','w(1,2)','','');
set(w2_axis, ...
'units','points',...
'position',[x+10 y-135 70 20],...
'color',nnmdgray,...
'ylim',[-0.3 1.3],...
'ytick',[],...
'xlim',[-1.3 1.3]*w_max,...
'xtick',[-1 -0.5 0 0.5 1]*w_max,...
'xticklabel',str2mat(num2str(-w_max),'','0','',num2str(w_max)))
w2_ind = fill([0 0.2 -0.2]*w_max+w(2),[0 1 1],nnred,...
'edgecolor',nndkblue,...
'erasemode','none');
b_axis = nnsfo('a2','b','','');
set(b_axis, ...
'units','points',...
'position',[x+120 y-135 70 20],...
'color',nnmdgray,...
'ylim',[-0.3 1.3],...
'ytick',[],...
'xlim',[-1.3 1.3]*w_max,...
'xtick',[-1 -0.5 0 0.5 1]*w_max,...
'xticklabel',str2mat(num2str(-w_max),'','0','',num2str(w_max)))
b_ind = fill([0 0.2 -0.2]*w_max+b,[0 1 1],nnred,...
'edgecolor',nndkblue,...
'erasemode','none');
f_menu = uicontrol(...
'units','points',...
'position',[x+115 y+15, 80 20],...
'style','popupmenu',...
'string','Hardlim|Hardlims|Purelin|Satlin|Satlins|Logsig|Tansig',...
'background',nnmdgray,...
'callback',[me '(''function'')'],...
'value',3);

% SLIDE BARS
drawnow % Let everything else appear before buttons

% 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
meters = [p1_axis p2_axis w1_axis w2_axis b_axis n_axis a_axis];
indicators = [p1_ind p2_ind w1_ind w2_ind b_ind n_ind a_ind];
w_ptr = uicontrol('visible','off'); set(w_ptr,'userdata',w);
b_ptr = uicontrol('visible','off'); set(b_ptr,'userdata',b);
f_ptr = uicontrol('visible','off'); set(f_ptr,'userdata',f);
p_ptr = uicontrol('visible','off'); set(p_ptr,'userdata',p);

% SAVE WINDOW DATA AND LOCK


H = [fig_axis desc_text meters indicators w_ptr b_ptr f_ptr p_ptr ...
f_menu f_text f_text2];
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,...
'Alter the input values',...
'by clicking & dragging',...
'the triangle indicators.',...
'',...
'Alter the weights and',...
'bias in the same way.',...
'Use the menu to pick',...
'a transfer function.',...
'',...
'Pick the transfer',...
'function with the',...
'F menu.',...
'',...
'The net input and the',...
'output will respond to',...
'each change.')

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

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

q = 0;
for i=1:5
pt = get(meters(i),'currentpoint');
x = pt(1);
y = pt(3);

if (i <= 2)
if (y >= -1.3*p_max) & (y <= 1.3*p_max) & (x >= 0) & (x <= 1)
q = i;
data = 'ydata';
z_max = p_max;
z = y;
hide_color = nnltyell;
break;
end
else
if (x >= -1.3*w_max) & (x <= 1.3*w_max) & (y >= 0) & (y <= 1)
q = i;
data = 'xdata';
z_max = w_max;
z = x;
hide_color = nnmdgray;
break;
end
end
end

if (q)
set(fig,'pointer','crosshair')
z = min(z_max,max(-z_max,z));
set(indicators(q),...
'facecolor',hide_color,...
'edgecolor',hide_color)
set(indicators(q),...
data,[0 0.2 -0.2]*z_max+z,...
'facecolor',nnred,...
'edgecolor',nndkblue)
set(fig,'WindowButtonMotionFcn',[me '(''down'')']);
set(fig,'WindowButtonUpFcn',[me '(''up'')']);

% ALTER VARIABLES
if (q <= 2)
p = get(p_ptr,'userdata');
p(q) = z;
set(p_ptr,'userdata',p);
elseif (q <= 4)
w = get(w_ptr,'userdata');
w(q-2) = z;
set(w_ptr,'userdata',w);
else
set(b_ptr,'userdata',z);
end

cmd = 'update';
else
set(fig,'pointer','arrow')
end

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

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

set(fig,...
'WindowButtonMotionFcn','',...
'pointer','arrow')

%==================================================================
% Respond to function menu.
%
% ME('function')
%==================================================================

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

v = get(f_menu,'value');

if v == 1, f = 'hardlim'; new_text = 'Hard Limit Neuron';


elseif v == 2, f = 'hardlims'; new_text = 'Sym. Hard Limit Neuron';
elseif v == 3, f = 'purelin'; new_text = 'Linear Neuron';
elseif v == 4, f = 'satlin'; new_text = 'Saturating Linear Neuron';
elseif v == 5, f = 'satlins'; new_text = 'Sym. Saturating Linear
Neuron';
elseif v == 6, f = 'logsig'; new_text = 'Log Sigmoid Neuron';
elseif v == 7, f = 'tansig'; new_text = 'Tan Sigmoid Neuron';
end

set(f_text,...
'color',nnltgray);
set(f_text,...
'string',new_text,...
'color',nndkblue);
set(f_text2,...
'color',nnltgray);
set(f_text2,...
'string',['a = ' f '(w*p+b)'],...
'color',nndkblue)
set(f_ptr,'userdata',f);
cmd = 'update';

%==================================================================
end

%==================================================================
% Respond to request to update displays.
%
% ME('update')
%==================================================================

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

% GET DATA
w = get(w_ptr,'userdata');
b = get(b_ptr,'userdata');
f = get(f_ptr,'userdata');
p = get(p_ptr,'userdata');

% UPDATE NET INPUT


n = w*p+b;
set(indicators(6),...
'facecolor',nnltyell)
set(indicators(6),...
'ydata',[0 0.2 -0.2]*n_max+n,...
'facecolor',nndkblue)

% UPDATE OUTPUT
n = w*p+b;
if strcmp(f,'satlin')
a = (~((n < 0) | (n > 1))).*n + (n > 1);
elseif strcmp(f,'satlins')
a = (~((n < -1) | (n > 1))).*n + (n > 1) - (n < -1);
else
a = feval(f,n);
end

set(indicators(7),...
'facecolor',nnltyell)
set(indicators(7),...
'ydata',[0 0.2 -0.2]*a_max/2+a,...
'facecolor',nndkblue)
end

hasil output
decision boundaries
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.

% $Revision: 1.7 $
% Copyright 1994-2002 PWS Publishing Company and The MathWorks, Inc.
% 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 = 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
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');
[R,Q] = size(P);
A = hardlim(w*P+b*ones(1,Q));
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,...
'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(db1,...
'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 '(''movept2'')'],...
'WindowButtonUpFcn',[me '(''setpt2'')'])

% EDIT INPUT/TARGET DOT


else
P = get(P_ptr,'userdata');
q = size(P,2);
for i=1:q
if sqrt(sum(sum((P(:,i)-[x; y]).^2))) < 0.15
T = get(T_ptr,'userdata');
w = get(w_ptr,'userdata');
b = get(b_ptr,'userdata');
dots = get(dots_ptr,'userdata');

set(dots(i),...
'facecolor',nnltyell,...
'edgecolor',nnltyell);
set(db_line,...
'color',nndkblue)
A = hardlim(w*P+b*ones(1,q));
if all(A == T)
col = nndkgray;
else
col = nnred;
end
set(w_arrow,...
'color',col)

set(db1,...
'color',nndkblue)
set(db2,...
'color',nndkblue)
set(cross,...
'color',nndkblue)

delete(dots(i));
t = T(i);
T(i) = [];
P(:,i) = [];
dots(i) = [];
for i=1:length(T)
set(dots(i),...
'facecolor',[1 1 1]-T(i))
end

set(T_ptr,'userdata',T);
set(P_ptr,'userdata',P);
set(dots_ptr,'userdata',dots);

set(fig,...
'pointer','circle',...
'WindowButtonUpFcn',[me '(''setdot'')'])
set(dot_ptr,'userdata',t);
break;
end
end
end

% NEW DOTS
else
pt = get(t1_axis,'currentpoint');
x = pt(1);
y = pt(3);

% BLACK DOT
if (x >= -.15) & (x <= .15) & (y >= -.15) & (y <= .15)
set(fig,...
'pointer','circle',...
'WindowButtonUpFcn',[me '(''setdot'')'])
set(dot_ptr,'userdata',1);

else
pt = get(t2_axis,'currentpoint');
x = pt(1);
y = pt(3);

% WHITE DOT
if (x >= -.15) & (x <= .15) & (y >= -.15) & (y <= .15)
set(fig,...
'pointer','circle',...
'WindowButtonUpFcn',[me '(''setdot'')'])
set(dot_ptr,'userdata',0);
end
end
end

%==================================================================
% Set decision dot.
%
% ME('setdot')
%==================================================================

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

pt = get(v_axis,'currentpoint');
x = pt(1);
x = round(x*2)/2;
y = pt(3);
y = round(y*2)/2;

dot = get(dot_ptr,'userdata');

if (x >= -p_max) & (x <= p_max) & (y >= -p_max) & (y <= p_max)
P = get(P_ptr,'userdata');
T = get(T_ptr,'userdata');
w = get(w_ptr,'userdata');
b = get(b_ptr,'userdata');
dots = get(dots_ptr,'userdata');

q = length(dots)+1;
deg = pi/180;
angle = [0:5:360]*deg;
cx = cos(angle)*0.15;
cy = sin(angle)*0.15;
set(fig,'nextplot','add');
axes(v_axis);

a = hardlim(w*[x;y]+b);
if (a == dot)
col = nndkgray;
else
col = nnred;
end

dots(q) = fill(cx+x,cy+y,[1 1 1]-dot,...


'edgecolor',col,...
'erasemode','none',...
'linewidth',2);
set(fig,'nextplot','new');
P = [P [x;y]];
T = [T dot];

set(P_ptr,'userdata',P);
set(T_ptr,'userdata',T);
set(dots_ptr,'userdata',dots);
cmd = 'update';
end
set(fig,...
'pointer','arrow',...
'WindowButtonUpFcn','')

%==================================================================
% Move decision boundary point #1.
%
% ME('movept1')
%==================================================================

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

pt = get(v_axis,'currentpoint');
x = pt(1);
x = round(x*4)/4;
y = pt(3);
y = round(y*4)/4;

% MOVE DECISION POINT #1


if (x >= -p_max) & (x <= p_max) & (y >= -p_max) & (y <= p_max)
pt = get(pt_ptr,'userdata');
pt(1,:) = [x y];
set(pt_ptr,'userdata',pt);
end

%==================================================================
% Respond to set decision boundary point #1.
%
% ME('setpt1')
%==================================================================

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

set(fig,...
'WindowButtonMotionFcn','',...
'WindowButtonUpFcn','',...
'pointer','arrow')

pt = get(pt_ptr,'userdata');
set(db1,...
'xdata',pt(1,1),...
'ydata',pt(1,2),...
'color',nndkblue)
cmd = 'update';

%==================================================================
% Move decision boundary point #1.
%
% ME('movept2')
%==================================================================

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

pt = get(v_axis,'currentpoint');
x = pt(1);
x = round(x*4)/4;
y = pt(3);
y = round(y*4)/4;
% MOVE DECISION POINT #1
if (x >= -p_max) & (x <= p_max) & (y >= -p_max) & (y <= p_max)
pt = get(pt_ptr,'userdata');
pt(2,:) = [x y];
set(pt_ptr,'userdata',pt);
end

%==================================================================
% Respond to set decision boundary point #2.
%
% ME('setpt2')
%==================================================================

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

set(fig,...
'WindowButtonMotionFcn','',...
'WindowButtonUpFcn','',...
'pointer','arrow')

pt = get(pt_ptr,'userdata');
set(db2,...
'xdata',pt(2,1),...
'ydata',pt(2,2),...
'color',nndkblue)
cmd = 'update';

%==================================================================
end

%==================================================================
% Respond to request to update displays.
%
% ME('update')
%==================================================================

if strcmp(cmd,'update')

% GET DATA
b = get(b_ptr,'userdata');
pt = get(pt_ptr,'userdata');
P = get(P_ptr,'userdata');
T = get(T_ptr,'userdata');

% UPDATE WEIGHTS & DECISION BOUNDARY


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,:)';

if (w(1) ~= 0)
pp2 = [-p_max p_max];
pp1 = -(w(2)*pp2+b)/w(1);

if (pp1(1) < -p_max | pp1(1) > p_max)


pp1(1) = p_max*sign(pp1(1));
pp2(1) = -(w(1)*pp1(1)+b)/w(2);
end
if (pp1(2) < -p_max | pp1(2) > p_max)
pp1(2) = p_max*sign(pp1(2));
pp2(2) = -(w(1)*pp1(2)+b)/w(2);
end

elseif (w(2) ~= 0)
pp1 = [-p_max p_max];
pp2 = -(w(1)*pp1+b)/w(2);
else
pp1 = [0 0];
pp2 = [0 0];
end

% NEW BOUNDARY
set(db_line,...
'xdata',pp1,...
'ydata',pp2,...
'color',nndkblue)

% NEW WEIGHT VECTOR


set(fig,'nextplot','add')
axes(v_axis)
w_arrow = get(w_arrow_ptr,'userdata');
delete(w_arrow);
[R,Q] = size(P);
A = hardlim(w*P+b*ones(1,Q));
if all(A == T)
col = nndkgray;
else
col = nnred;
end
w_arrow = nndrwvec(w(1),w(2),2,0.2,col,'W','none');
set(w_arrow_ptr,'userdata',w_arrow)
set(fig,'nextplot','new')

% REFRESH DOTS
dots = get(dots_ptr,'userdata');
T = get(T_ptr,'userdata');
for i=1:length(T)
a = hardlim(w*P(:,i)+b);
if (a == T(i))
col = nndkgray;
else
col = nnred;
end
set(dots(i),...
'facecolor',[1 1 1]-T(i),...
'edgecolor',col)
end

% NEW PARAMETER VALUES


set(w1_text,...
'string',sprintf('%5.3g',w(1)),...
'color',nndkblue)
set(w2_text,...
'string',sprintf('%5.3g',w(2)),...
'color',nndkblue)
set(b_text,...
'string',sprintf('%5.3g',b),...
'color',nndkblue)
nntxtchk
end

hasil output

percepton learning rule


function nnd4pr(cmd,arg1,arg2,arg3)
%NND4PR Perceptron rule demonstration.
%
% This demonstration requires either the MININNET functions
% on the NND disk or the Neural Network Toolbox.

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

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

% CONSTANTS
me = 'nnd4pr';
p_max = 3;
max_epoch = 5;

% 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
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
bias = H(8); % "Bias" radio button
no_bias = H(9); % "No bias" radio button
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
index_ptr = H(21); % index of pattern to train on
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 = [1 -0.8];
b = 0;
P = [1 2; -1 2; 0 -1]';
T = [1 0 0];

% NEW DEMO FIGURE


fig = nndemof2(me,'DESIGN','Perceptron Rule','','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+285,bracket_y*4+20,'color',nndkblue,'linewidth',3);
plot(-bracket_x+355,bracket_y*4+20,'color',nndkblue,'linewidth',3);
b_text = text(320,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',[105 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 p_max];
zz2 = zz1*0;
cross = plot([zz1 NaN zz2],[zz2 NaN zz1],':',...
'color',nndkblue,...
'erasemode','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
[R,Q] = size(P);
if all(T == hardlim(w*P+b*ones(1,Q)))
col = nndkblue;
else
col = nnred;
end
db_line = plot(pp1,pp2,...
'linewidth',2,...
'color',col,...
'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',[40 320 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',[40 270 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',[20 220 60 20],...
'string','Learn',...
'callback',[me '(''learn'')'])
uicontrol(...
'units','points',...
'position',[20 185 60 20],...
'string','Train',...
'callback',[me '(''train'')'])
uicontrol(...
'units','points',...
'position',[20 150 60 20],...
'string','Random',...
'callback',[me '(''random'')'])
bias = uicontrol(...
'units','points',...
'position',[20 115 70 20],...
'string','Bias',...
'callback',[me '(''bias'')'],...
'style','radio',...
'value',0,...
'back',nnltgray);
no_bias = uicontrol(...
'units','points',...
'position',[20 85 70 20],...
'string','No Bias',...
'callback',[me '(''nobias'')'],...
'style','radio',...
'value',1,...
'back',nnltgray);

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','userdata',w);
b_ptr = uicontrol('visible','off','userdata',b);
pt_ptr = uicontrol('visible','off','userdata',pt);
P_ptr = uicontrol('visible','off','userdata',P);
T_ptr = uicontrol('visible','off','userdata',T);
dots_ptr = uicontrol('visible','off','userdata',dots);
dot_ptr = uicontrol('visible','off','userdata',[]);
index_ptr = uicontrol('visible','off','userdata',1);

% SAVE WINDOW DATA AND LOCK

H = [fig_axis desc_text v_axis t1_axis cross NaN db_line...


bias no_bias w_ptr b_ptr pt_ptr w1_text w2_text b_text P_ptr T_ptr ...
dots_ptr t2_axis dot_ptr index_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,...
'Click [Learn] to apply',...
'the perceptron rule',...
'to a single vector.',...
'',...
'Click [Train] to apply',...
'the rule up to 5 times.',...
'',...
'Click [Random] to set',...
'the weights to random',...
'values.',...
'',...
'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)
% EDIT INPUT/TARGET DOT
P = get(P_ptr,'userdata');
q = size(P,2);
for i=1:q
if sqrt(sum((P(:,i)-[x; y]).^2)) < 0.15
T = get(T_ptr,'userdata');
w = get(w_ptr,'userdata');
b = get(b_ptr,'userdata');
dots = get(dots_ptr,'userdata');

set(dots(i),...
'facecolor',nnltyell,...
'edgecolor',nnltyell);

delete(dots(i));
t = T(i);
T(i) = [];
P(:,i) = [];
dots(i) = [];

[R,Q] = size(P);
if all(T == hardlim(w*P+b*ones(1,Q)))
col = nndkblue;
else
col = nnred;
end
set(db_line,...
'color',col)
set(cross,...
'color',nndkblue)

for i=1:length(T)
set(dots(i),...
'facecolor',[1 1 1]-T(i))
end

set(T_ptr,'userdata',T);
set(P_ptr,'userdata',P);
set(dots_ptr,'userdata',dots);

set(fig,...
'pointer','circle',...
'WindowButtonUpFcn',[me '(''setdot'')'])
set(dot_ptr,'userdata',t);
break;
end
end

% NEW DOTS
else
pt = get(t1_axis,'currentpoint');
x = pt(1);
y = pt(3);

% BLACK DOT
if (x >= -.15) & (x <= .15) & (y >= -.15) & (y <= .15)
set(fig,...
'pointer','circle',...
'WindowButtonUpFcn',[me '(''setdot'')'])
set(dot_ptr,'userdata',1);
else
pt = get(t2_axis,'currentpoint');
x = pt(1);
y = pt(3);

% WHITE DOT
if (x >= -.15) & (x <= .15) & (y >= -.15) & (y <= .15)
set(fig,...
'pointer','circle',...
'WindowButtonUpFcn',[me '(''setdot'')'])
set(dot_ptr,'userdata',0);
end
end
end

%==================================================================
% Set decision dot.
%
% ME('setdot')
%==================================================================

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

pt = get(v_axis,'currentpoint');
x = pt(1);
x = round(x*2)/2;
y = pt(3);
y = round(y*2)/2;

dot = get(dot_ptr,'userdata');

if (x >= -p_max) & (x <= p_max) & (y >= -p_max) & (y <= p_max)
P = get(P_ptr,'userdata');
T = get(T_ptr,'userdata');
w = get(w_ptr,'userdata');
b = get(b_ptr,'userdata');
dots = get(dots_ptr,'userdata');

q = length(dots)+1;
deg = pi/180;
angle = [0:5:360]*deg;
cx = cos(angle)*0.15;
cy = sin(angle)*0.15;
set(fig,'nextplot','add');
axes(v_axis);

P = [P [x;y]];
T = [T dot];

[R,Q] = size(P);
if all(T == hardlim(w*P+b*ones(1,Q)))
col = nndkblue;
else
col = nnred;
end
set(db_line,...
'color',col);
a = hardlim(w*[x;y]+b);
if (a == dot)
col = nndkgray;
else
col = nnred;
end
dots(q) = fill(cx+x,cy+y,[1 1 1]-dot,...
'edgecolor',col,...
'erasemode','none',...
'linewidth',2);
set(fig,'nextplot','new');

set(P_ptr,'userdata',P);
set(T_ptr,'userdata',T);
set(dots_ptr,'userdata',dots);
end

set(fig,...
'pointer','arrow',...
'WindowButtonUpFcn','')

%==================================================================
% Learn.
%
% ME('learn')
%==================================================================

elseif strcmp(cmd,'learn')

max_epoch = 1;
cmd = 'train';

%==================================================================
% Bias.
%
% ME('bias')
%==================================================================

elseif strcmp(cmd,'bias')

set(no_bias,'value',0);

%==================================================================
% Bias.
%
% ME('bias')
%==================================================================

elseif strcmp(cmd,'nobias')

set(bias,'value',0);

w = get(w_ptr,'userdata');
b = 0;
P = get(P_ptr,'userdata');
T = get(T_ptr,'userdata');
dots = get(dots_ptr,'userdata');

if (w(1) ~= 0)
pp2 = [-p_max p_max];
pp1 = -(w(2)*pp2+b)/w(1);
if (pp1(1) < -p_max | pp1(1) > p_max)
pp1(1) = p_max*sign(pp1(1));
pp2(1) = -(w(1)*pp1(1)+b)/w(2);
end
if (pp1(2) < -p_max | pp1(2) > p_max)
pp1(2) = p_max*sign(pp1(2));
pp2(2) = -(w(1)*pp1(2)+b)/w(2);
end
elseif (w(2) ~= 0)
pp1 = [-p_max p_max];
pp2 = -(w(1)*pp1+b)/w(2);
else
pp1 = [0 0];
pp2 = [0 0];
end

% NEW BOUNDARY
set(db_line,...
'color',nnltyell)
set(cross,...
'color',nndkblue);
[R,Q] = size(P);
if all(T == hardlim(w*P+b*ones(1,Q)))
col = nndkblue;
else
col = nnred;
end
set(db_line,...
'xdata',pp1,...
'ydata',pp2,...
'color',col)

% REFRESH DOTS
for k=1:length(T)
a = hardlim(w*P(:,k)+b);
if (a == T(k))
col = nndkgray;
else
col = nnred;
end
set(dots(k),...
'facecolor',[1 1 1]-T(k),...
'edgecolor',col)
end

% NEW PARAMETER VALUES


set(b_text,...
'color',nnltgray)
nntxtchk;
set(b_text,...
'string',sprintf('%5.3g',b),...
'color',nndkblue)
nntxtchk;

set(b_ptr,'userdata',b);

%==================================================================
% Random weights.
%
% ME('random')
%==================================================================

elseif strcmp(cmd,'random')

[w,b] = feval('rands',1,2);
if get(no_bias,'value'), b = 0; end

P = get(P_ptr,'userdata');
T = get(T_ptr,'userdata');
dots = get(dots_ptr,'userdata');

if (w(1) ~= 0)
pp2 = [-p_max p_max];
pp1 = -(w(2)*pp2+b)/w(1);
if (pp1(1) < -p_max | pp1(1) > p_max)
pp1(1) = p_max*sign(pp1(1));
pp2(1) = -(w(1)*pp1(1)+b)/w(2);
end
if (pp1(2) < -p_max | pp1(2) > p_max)
pp1(2) = p_max*sign(pp1(2));
pp2(2) = -(w(1)*pp1(2)+b)/w(2);
end
elseif (w(2) ~= 0)
pp1 = [-p_max p_max];
pp2 = -(w(1)*pp1+b)/w(2);
else
pp1 = [0 0];
pp2 = [0 0];
end

% NEW BOUNDARY
set(db_line,...
'color',nnltyell)
set(cross,...
'color',nndkblue);
[R,Q] = size(P);
if all(T == hardlim(w*P+b*ones(1,Q)))
col = nndkblue;
else
col = nnred;
end
set(db_line,...
'xdata',pp1,...
'ydata',pp2,...
'color',col)

% REFRESH DOTS
for k=1:length(T)
a = hardlim(w*P(:,k)+b);
if (a == T(k))
col = nndkgray;
else
col = nnred;
end
set(dots(k),...
'facecolor',[1 1 1]-T(k),...
'edgecolor',col)
end
% NEW PARAMETER VALUES
set(w1_text,...
'color',nnltgray)
set(w2_text,...
'color',nnltgray)
set(b_text,...
'color',nnltgray)
nntxtchk;
set(w1_text,...
'string',sprintf('%5.3g',w(1)),...
'color',nndkblue)
set(w2_text,...
'string',sprintf('%5.3g',w(2)),...
'color',nndkblue)
set(b_text,...
'string',sprintf('%5.3g',b),...
'color',nndkblue)
nntxtchk;

set(w_ptr,'userdata',w);
set(b_ptr,'userdata',b);

%==================================================================
end

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

if strcmp(cmd,'train')

% GET DATA
w = get(w_ptr,'userdata');
b = get(b_ptr,'userdata');
P = get(P_ptr,'userdata');
T = get(T_ptr,'userdata');
dots = get(dots_ptr,'userdata');
bf = get(bias,'value');
j = get(index_ptr,'userdata');
q = length(T);
if (q == 0)
return
end

for i=1:max_epoch
if (j > q), j = 1; end
if all(T == hardlim(w*P+b*ones(1,q))), break, end

dot_col = get(dots(j),'facecolor');
set(dots(j),...
'facecolor',nngreen)
nnpause(0.5);
set(dots(j),...
'facecolor',nnltyell)
nnpause(0.5);
set(dots(j),...
'facecolor',nngreen)
nnpause(0.5);

a = hardlim(w*P(:,j)+b);
e = T(:,j) - a;
dw = e*P(:,j)';
db = e;
w = w + dw;
if bf, b = b + db; end

% CALCULATE NEW DECISION BOUNDARY


if (dw ~= 0) | (db ~= 0)
if (w(1) ~= 0)
pp2 = [-p_max p_max];
pp1 = -(w(2)*pp2+b)/w(1);
if (pp1(1) < -p_max | pp1(1) > p_max)
pp1(1) = p_max*sign(pp1(1));
pp2(1) = -(w(1)*pp1(1)+b)/w(2);
end
if (pp1(2) < -p_max | pp1(2) > p_max)
pp1(2) = p_max*sign(pp1(2));
pp2(2) = -(w(1)*pp1(2)+b)/w(2);
end
elseif (w(2) ~= 0)
pp1 = [-p_max p_max];
pp2 = -(w(1)*pp1+b)/w(2);
else
pp1 = [0 0];
pp2 = [0 0];
end

% NEW BOUNDARY
set(db_line,...
'color',nnltyell)
set(cross,...
'color',nndkblue);

[R,Q] = size(P);
if all(T == hardlim(w*P+b*ones(1,Q)))
col = nndkblue;
else
col = nnred;
end
set(db_line,...
'xdata',pp1,...
'ydata',pp2,...
'color',col)

% REFRESH DOTS
for k=1:length(T)
a = hardlim(w*P(:,k)+b);
if (a == T(k))
col = nndkgray;
else
col = nnred;
end
set(dots(k),...
'facecolor',[1 1 1]-T(k),...
'edgecolor',col)
end
% NEW PARAMETER VALUES
set(w1_text,...
'color',nnltgray)
set(w2_text,...
'color',nnltgray)
set(b_text,...
'color',nnltgray)
nntxtchk;
set(w1_text,...
'string',sprintf('%5.3g',w(1)),...
'color',nndkblue)
set(w2_text,...
'string',sprintf('%5.3g',w(2)),...
'color',nndkblue)
set(b_text,...
'string',sprintf('%5.3g',b),...
'color',nndkblue)
nntxtchk;
end
set(dots(j),...
'facecolor',dot_col)
j = j + 1;
end

set(w_ptr,'userdata',w);
set(b_ptr,'userdata',b);
set(index_ptr,'userdata',j);
end

hasil output

Vous aimerez peut-être aussi