📄 demo_linclass.m
字号:
demo_linclass('reset',hfigure); else errordlg('This file does not contain required data.','Bad file','modal'); end endcase 'redraw' % == Redraw points in axes ================================== h=get(hfigure,'UserData'); % uicontrol handlers % get point sets sets=get(h.axes1,'UserData'); if isempty(sets)==1, return; end % clears axes set(get(h.axes1,'Children'),'EraseMode','normal'); clrchild(h.axes1); h.line.handler=line('EraseMode','xor','Color','k','Visible','off','Parent',h.axes1); set(hfigure,'UserData',h); % uicontrol handlers%%% pplot(sets.X,sets.I);%% ppoints(sets.X,sets.I); ppatterns(sets); demo_linclass('drawline',hfigure,h.line.theta,h.line.alpha); drawnow;case 'loadsets' % == Load data sets ======================================== % Get file name from the pop up menu according to menu pointer. % Than clear axes,load new file and appear the points from the file. h=get(hfigure,'UserData'); % Clear axes clrchild(h.axes1); set(h.axes1, ... 'Box','on', ... 'DrawMode','fast' ); xlabel('feature x_1'); ylabel('feature x_2'); % No line h.line.handler=-1; set(hfigure,'UserData',h); % Get file name with sets file=get(h.btload,'UserData'); % Load sets sets=load(file.pathname); %%% sets.I=sets.y; sets.N=2; for ii=1:max(sets.y), sets.K(ii)=length(find(sets.y==ii)); end % store loaded sets set(h.axes1,'UserData',sets); % set axes according to current point set win=cmpwin(min(sets.X'),max(sets.X'),BORDER,BORDER); setaxis(h.axes1,win); axes(h.axes1); % plots points%% ppoints(sets.X,sets.I); ppatterns(sets); drawnow; case 'play' % == Start up the adaptation process ============================ % Get handle to data. h=get(hfigure,'UserData'); if h.line.handler==-1, return; end % Check if data are loaded. sets=get(h.axes1,'UserData'); if isempty(sets)==1, return; end % Disable and enable buttons. set([h.btinfo h.btstep h.btclose h.btplay h.btreset h.btload h.btcreat ... h.pualgo h.editer],'Enable','off'); set(h.btstop,'Enable','on'); set(h.btstop,'UserData',0); h.stop = 0; set(hfigure,'UserData',h); % Play - adaptation process while h.stop==0 & get(h.btstop,'UserData')==0, demo_linclass('step',hfigure); h=get(hfigure,'UserData'); end % Enable and dissable buttons. set([h.btinfo h.btstep h.btclose h.btplay h.btreset h.pualgo ... h.editer h.btload h.btcreat],'Enable','on'); set(h.btstop,'Enable','off');case 'step' % == Perform one adaptation step ====================================== h=get(hfigure,'UserData'); % get handlers we will need... if h.line.handler==-1, return; end % get sets sets=get(h.axes1,'UserData'); % no data set loaded if isempty(sets)==1, return; end [alpha,theta,solution,t]=exec(hfigure); if mod(h.stepcnt,2)==1 & h.line.t >0, if solution ~= 1, demo_linclass('drawline',hfigure,theta,alpha); else text=makeinfo(t,alpha,theta,solution); set(h.console,'String',text ); end else if get(h.xbanim,'Value')==0, demo_linclass('drawline',hfigure,theta,alpha); else demo_linclass('animline',hfigure,theta,alpha); end if solution==1 | solution==-1, h.stop=1; end h.line.alpha = alpha; h.line.theta = theta; h.line.t = t; if solution==0 | solution ==1, % appear time and line parameters text=makeinfo(t,alpha,theta,solution); elseif solution==-1, text=sprintf('Solution does not exist.\n'); end set(h.console,'String',text ); end % store new solution h.stepcnt=h.stepcnt+1; set(hfigure,'UserData',h);case 'animline' % == Smooth transition of line from old to new position =============== h=get(hfigure,'UserData'); % get handlers % old position of line is... alpha2=h.line.alpha; theta2=h.line.theta; t2=h.line.t; % New position get from input arguments theta1=varargin{1}; alpha1=varargin{2}; if t2~=0, % move line step=1/ANIM_STEPS; for k=0:step:1, alpha=(1-k)*alpha2+k*alpha1; % smooth transition of alpha theta=(1-k)*theta2+k*theta1; % --//-- theta demo_linclass('drawline',hfigure,theta,alpha); end else % it is first step demo_linclass('drawline',hfigure,theta1,alpha1); % first step end % if t2~=0case 'reset' % == Reset adaptation process, set up zero step ================ h=get(hfigure,'UserData'); % get handlers % get data set sets=get(h.axes1,'UserData'); % get file file=get(h.btload,'UserData'); % zeroize parameters of the separation line h.line.t=0; h.line.theta=0; h.line.alpha=[0;0]; h.stepcnt=0; if h.line.handler==-1, % create line axes(h.axes1); h.line.handler=... line('EraseMode','xor','Color','k','Visible','off','Parent',h.axes1); h.badpoint.handler=line('EraseMode','xor','Color','k','Visible','off',... 'Parent',h.axes1,... 'Marker','o',... 'MarkerSize',WRONGPOINT_SIZE); drawnow; else % change parameters of line set(h.line.handler,'Visible','off'); set(h.badpoint.handler,'Visible','off'); end % if hline==-1 % set up handlers and flush queue with graph. objects set(hfigure,'UserData',h); % create comment if isempty(sets)==0, consoletext=sprintf('Step t=0\nNo separation line'); titletext=sprintf('Data file: %s, %d vectors',file.name,sum(sets.K)); else consoletext=sprintf(['No data loaded.\nPress Load data button.\n',... 'Load sample data from ../toolboxroot/data/binary_separable']); titletext=''; pos=get(h.axes1,'Position'); fsize=min(pos(3),pos(4))/10; setaxis(h.axes1,[-1 1 -1 1]); axis([-1 1 -1 1]); builtin('text',0,0,'Press ''Load data'' button.',... 'HorizontalAlignment','center',... 'FontUnits','normalized',... 'Clipping','on',... 'FontSize',fsize); end % show comment set(h.console,'String',consoletext ); % print title pos=get(h.axes1,'Position'); fsize=(1-pos(2)-pos(4))*1; title(titletext,... 'Parent',h.axes1,... 'VerticalAlignment','bottom',... 'HorizontalAlignment','left',... 'FontUnits','normalized',... 'Units','normalized',... 'Position',[0 1 0],... 'FontSize',fsize);case 'drawline' % == Draw separation line ============================ h=get(hfigure,'UserData'); % get handlers % get new line position from input arguments theta=varargin{1}; alpha=varargin{2}; if mod(h.stepcnt,2)==1 & h.line.t >0, set(h.badpoint.handler,'Visible','on',... 'XData',alpha(1),'YData',alpha(2)); else set(h.badpoint.handler,'Visible','off'); % Cut off line along axes [x1,y1,x2,y2,in]=cliplin1(alpha,theta,getaxis(h.axes1)); % erase old line set(h.line.handler,'Visible','off'); % draw new line if is in the axes if in==1, set(h.line.handler,... 'XData',[x1 x2],... 'YData',[y1 y2],... 'LineWidth',LINE_WIDTH,... 'Visible','on'); end end % flush draw queue drawnow; case 'info' % == Invokes standard Matlab`s info box ========================== helpwin(mfilename);end%========================================function [text]=makeinfo(t,alpha,theta,solution)% assembles text description about current solution stateif solution==1, txline{1}=sprintf('Solution was found after t=%d step(s).',t);else txline{1}=sprintf('Step t=%d',t);endtxline{2}=sprintf('Linear decision function:');txline{3}=sprintf('%f x_1 + %f x_2 + %f = 0',alpha(1),alpha(2),-theta);text='';for i=1:3, text=strvcat(text,txline{i});endreturn;%===========================================function [alpha,theta,solution,tplus1]=exec(hfigure);h=get(hfigure,'UserData'); if h.line.handler==-1, return;end% get setssets=get(h.axes1,'UserData');% no data set loadedif isempty(sets)==1, return;end% get parameterst=h.line.t;alpha=h.line.alpha;theta=h.line.theta;iter=max(1,round(str2num(get(h.editer,'String'))));epsil=str2num(get(h.edeps,'String'));if mod(h.stepcnt,2) ~= 0 & t > 0, iter = -1;end% perform one adaptation stepswitch get(h.pualgo,'Value') case 1%%[alpha,theta,solution,tplus1]=perceptr(sets.X,sets.I,iter,t,alpha,theta); if iter==-1, tmp_options.tmax = -1; else tmp_options.tmax=t+iter; end init_model.t=t; init_model.W=alpha; init_model.b=-theta; tmp_model = perceptron(sets,tmp_options,init_model); if iter==-1, if tmp_model.last_update, alpha=sets.X(:,tmp_model.last_update); else alpha=tmp_model.W; end else alpha = tmp_model.W; end solution=tmp_model.exitflag; tplus1 = tmp_model.t; theta = -tmp_model.b; case 2%%%% [alpha,theta,solution,tplus1]=kozinec(sets.X,sets.I,iter,t,alpha,theta); if iter==-1, tmp_options.tmax = -1; else tmp_options.tmax=t+iter; end tmp_options.eps=-1; if t~=0, init_model.t=t; init_model.W=alpha; init_model.b=-theta;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -