📄 nnd17ols.m
字号:
% w1_1_bar = uicontrol(...
% 'units','points',...
% 'position',[x y-25 16 len],...
% 'style','slider',...
% 'backg',nnltgray,...
% 'callback',[me '(''w1_1'')'],...
% 'min',-2,...
% 'max',2,...
% 'value',w1_1);
% BUTTONS
uicontrol(...
'units','points',...
'position',[400 145 60 20],...
'string','Reset',...
'callback',[me '(''reset'')'])
add_button=uicontrol(...
'units','points',...
'position',[400 115 60 20],...
'string','Add Neuron',...
'callback',[me '(''add'')']);
uicontrol(...
'units','points',...
'position',[400 85 60 20],...
'string','Contents',...
'callback','nndtoc')
uicontrol(...
'units','points',...
'position',[400 55 60 20],...
'string','Close',...
'callback',[me '(''close'')'])
% DATA POINTERS
fa_ptr = uicontrol('visible','off','userdata',fa_plot);
fa_ptr2 = uicontrol('visible','off','userdata',fa_plot2);
fa_ptr3 = uicontrol('visible','off','userdata',fa_plot3);
fa_ptr4 = uicontrol('visible','off','userdata',fa_plot4);
fb_ptr = uicontrol('visible','off','userdata',fb_plot);
rand_ptr = uicontrol('visible','off','userdata',randseq);
% SAVE WINDOW DATA AND LOCK
H = [fig_axis desc_text s1_text fa_axis fa_ptr fa_ptr2 fa_ptr3 ...
fa_ptr4 fb_axis fb_ptr sigma_bar sigma_text npts_bar npts_text ...
freq_bar freq_text phase_bar phase_text ...
bias_bar bias_text bias_checkbox s1calc_text rand_ptr add_button];
% bias_bar bias_text bias_checkbox w1_1_bar w1_1_text s1calc_text];
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,...
'Use the slider bars',...
'to choose network',...
'or function values',...
'',...
'Click [Add Neuron]',...
'to increase size',...
'of Hidden layer and',...
'update plots',...
'',...
'Click [Reset] to',...
'initialize menu values',...
'',...
'Blue - Function',...
'Red - Network Response')
%==================================================================
% Respond to hidden neuron slider.
%
% ME('s1')
%==================================================================
%elseif strcmp(cmd,'s1')
% if strcmp(get(s1_bar,'enable'),'off')
% return
% end
% set(s1_bar,'enable','off');
% s1 = get(s1_bar,'value');
% s1 = round(s1);
% set(s1_text,'string',sprintf('%g',s1))
% cmd='update';
%==================================================================
% Respond to difficulty index slider.
%
% ME('sigma')
%==================================================================
elseif strcmp(cmd,'sigma')
if strcmp(get(sigma_bar,'enable'),'off')
return
end
set(sigma_bar,'enable','off');
sigma = get(sigma_bar,'value');
sigma = round(sigma*10)/10;
set(sigma_text,'string',sprintf('%g',sigma))
% With new sigma value, we change random sequence
randseq = randn(1,20);
set(rand_ptr,'userdata',randseq);
set(s1_text,'string','0')
set(s1calc_text,'string','0')
cmd='update';
%==================================================================
% Respond to Number of Points slider.
%
% ME('npts')
%==================================================================
elseif strcmp(cmd,'npts')
if strcmp(get(npts_bar,'enable'),'off')
return
end
set(npts_bar,'enable','off');
npts = get(npts_bar,'value');
npts = round(npts);
set(npts_text,'string',sprintf('%g',npts))
set(s1_text,'string','0')
set(s1calc_text,'string','0')
cmd='update';
%==================================================================
% Respond to Frequency slider.
%
% ME('freq')
%==================================================================
elseif strcmp(cmd,'freq')
if strcmp(get(freq_bar,'enable'),'off')
return
end
set(freq_bar,'enable','off');
freq = get(freq_bar,'value');
freq = round(freq*100)/100;
set(freq_text,'string',sprintf('%g',freq))
set(s1_text,'string','0')
set(s1calc_text,'string','0')
cmd='update';
%==================================================================
% Respond to phase slider.
%
% ME('phase')
%==================================================================
elseif strcmp(cmd,'phase')
if strcmp(get(phase_bar,'enable'),'off')
return
end
set(phase_bar,'enable','off');
phase = get(phase_bar,'value');
phase = round(phase);
set(phase_text,'string',sprintf('%g',phase))
set(s1_text,'string','0')
set(s1calc_text,'string','0')
cmd='update';
%==================================================================
% Respond to bias slider.
%
% ME('bias')
%==================================================================
elseif strcmp(cmd,'bias')
if strcmp(get(bias_bar,'enable'),'off')
return
end
set(bias_bar,'enable','off');
bias = get(bias_bar,'value');
bias = round(bias*100)/100;
set(bias_text,'string',sprintf('%g',bias))
set(s1_text,'string','0')
set(s1calc_text,'string','0')
cmd='update';
%==================================================================
% Respond to bias checkbox.
%
% ME('bias_checkbox')
%==================================================================
elseif strcmp(cmd,'bias_checkbox')
if get(bias_checkbox,'value') == 1
set(bias_bar,'enable','off');
else
set(bias_bar,'enable','on');
bias = get(bias_bar,'value');
bias = round(bias*100)/100;
set(bias_text,'string',sprintf('%g',bias))
end
set(s1_text,'string','0')
set(s1calc_text,'string','0')
cmd='update';
%==================================================================
% Respond to w1_1 slider.
%
% ME('w1_1')
%==================================================================
%elseif strcmp(cmd,'w1_1')
% if strcmp(get(w1_1_bar,'enable'),'off')
% return
% end
% set(w1_1_bar,'enable','off');
% w1_1 = get(w1_1_bar,'value');
% w1_1 = round(w1_1*10)/10;
% set(w1_1_text,'string',sprintf('%g',w1_1))
% cmd='update';
%==================================================================
% Respond to Add Neuron button.
%
% ME('add')
%==================================================================
elseif strcmp(cmd,'add')
set(add_button,'enable','off')
s1 = str2num(get(s1_text,'string'));
s1 = round(s1);
s1=s1+1;
set(s1_text,'string',sprintf('%g',s1))
%set(s1_bar,'value',s1)
cmd='update';
%==================================================================
% Respond to reset button.
%
% ME('reset')
%==================================================================
elseif strcmp(cmd,'reset')
if strcmp(get(sigma_bar,'enable'),'off') | strcmp(get(npts_bar,'enable'),'off') ...
| (strcmp(get(bias_bar,'enable'),'off') & get(bias_checkbox,'value') == 0) ...
| strcmp(get(freq_bar,'enable'),'off') | strcmp(get(phase_bar,'enable'),'off')
return
end
% set(s1_bar,'enable','off');
set(sigma_bar,'enable','off');
set(npts_bar,'enable','off');
set(bias_bar,'enable','off');
set(freq_bar,'enable','off');
set(phase_bar,'enable','off');
S1 = 0;
% set(s1_bar,'value',S1);
set(s1_text,'string',sprintf('%g',S1))
sigma = 0;
set(sigma_bar,'value',sigma);
set(sigma_text,'string',sprintf('%g',sigma))
npts = 10;
set(npts_bar,'value',npts);
set(npts_text,'string',sprintf('%g',npts))
freq = 1/2;
set(freq_bar,'value',freq);
set(freq_text,'string',sprintf('%g',freq))
phase = 90;
set(phase_bar,'value',phase);
set(phase_text,'string',sprintf('%g',phase))
range = [-2 2];
%delta = (range(2)-range(1))/(S1-1);
%bias=sqrt(S1)/delta;
bias=1;
set(bias_bar,'value',bias);
set(bias_text,'string',sprintf('%g',round(bias*100)/100))
%set(w1_1_bar,'value',range(1));
%set(w1_1_text,'string',sprintf('%g',round(range(1)*10)/10))
cmd='update';
end
%==================================================================
% Train and update the figures.
%
% ME('update')
%==================================================================
if strcmp(cmd,'update')
S1=str2num(get(s1_text,'string'));
npts=str2num(get(npts_text,'string'));
phase=str2num(get(phase_text,'string'));
freq=str2num(get(freq_text,'string'));
sigma=str2num(get(sigma_text,'string'));
bias=str2num(get(bias_text,'string'));
%w1_1=str2num(get(w1_1_text,'string'));
range = [-2 2];
d1 = (range(2)-range(1))/(npts-1);
p = range(1):d1:range(2);
%t = sin(2*pi*(freq*p + phase/360))+1 + sigma*randn(size(p));
randseq = get(rand_ptr,'userdata');
t = sin(2*pi*(freq*p + phase/360))+1 + sigma*randseq(1:length(p));
c=p;
if get(bias_checkbox,'value') == 1
b = ones(size(c));
else
b = ones(size(c))*bias;
end
n=S1;
[W1,b1,W2,b2,mf,of,indf] = rb_ols(p,t,c,b,n,0);
S1=length(W1);
set(s1calc_text,'string',num2str(S1));
%delta = (range(2)-w1_1)/(S1-1);
%if get(bias_checkbox,'value') == 1
% b=sqrt(S1)/delta;
% bias=b;
% set(bias_text,'string',sprintf('%g',round(bias*100)/100))
%else
% set(bias_bar,'value',bias);
% set(bias_text,'string',sprintf('%g',round(bias*100)/100))
% b=bias; %sqrt(S1)/delta;
%end
total = range(2)-range(1);
%W1 = w1_1:delta:range(2);
%W1 = W1(1:(end))';
%b1 = b*ones(size(W1));
Q = length(p);
pp = repmat(p,S1,1);
if S1==0
n1=0;
a1=0;
a2=b2*ones(1,Q);
% p2=0;
else
n1 = abs(pp-W1*ones(1,Q)).*(b1*ones(1,Q));
a1 = exp(-n1.^2);
a2 = W2*a1 + b2*ones(1,Q);
end
p2 = range(1):(total/1000):range(2);
Q2 = length(p2);
if S1==0
pp2=0;
n12=0;
a12=0;
a22=b2*ones(1,Q2);
else
pp2 = repmat(p2,S1,1);
n12 = abs(pp2-W1*ones(1,Q2)).*(b1*ones(1,Q2));
a12 = exp(-n12.^2);
a22 = W2*a12 + b2*ones(1,Q2);
end
t_exact = sin(2*pi*(freq*p2 + phase/360))+1;
if S1==0
temp=[b2*ones(1,Q2)];
else
temp=[(W2'*ones(1,Q2)).*a12; b2*ones(1,Q2)];
end
%sse = sumsqr(t-a2);
sse = sum(sum((t-a2).*(t-a2)));
set(fig,'nextplot','add')
delete(get(fa_axis,'children'))
axes(fa_axis)
cc=get(fa_axis,'color');
fa_plot = plot(p,t,'ok');
hold on
fa_plot2 = plot(p2,temp,':k');
fa_plot3 = plot(p2,t_exact,'b','LineWidth',2);
fa_plot4 = plot(p2,a22,'r','LineWidth',1);
hold off
set(fa_axis,'color',cc);
set(get(fa_axis,'ylabel'),...
'string','a^2','FontSize',12,'FontWeight','bold')
set(get(fa_axis,'title'),...
'string','Function Approximation','FontSize',12,'FontWeight','bold')
set(fa_axis,'ylim',[-2 4])
delete(get(fb_axis,'children'))
axes(fb_axis)
fb_plot = plot(p2,a12,'k');
%fa_plot=get(fa_ptr,'userdata');
%set(fa_plot,'xdata',p,'ydata',t);
%fa_plot2=get(fa_ptr2,'userdata');
%set(fa_plot2,'xdata',p2,'ydata',temp);
%fa_plot3=get(fa_ptr3,'userdata');
%set(fa_plot3,'xdata',p2,'ydata',t_exact);
%fa_plot4=get(fa_ptr4,'userdata');
%set(fa_plot4,'xdata',p2,'ydata',a22);
%fb_plot=get(fb_ptr,'userdata');
%set(fb_plot,'xdata',p2,'ydata',a12);
% DATA POINTERS
set(fa_ptr,'userdata',fa_plot);
set(fa_ptr2,'userdata',fa_plot2);
set(fa_ptr3,'userdata',fa_plot3);
set(fa_ptr4,'userdata',fa_plot4);
set(fb_ptr,'userdata',fb_plot);
% SAVE WINDOW DATA AND LOCK
H = [fig_axis desc_text s1_text fa_axis fa_ptr fa_ptr2 fa_ptr3 ...
fa_ptr4 fb_axis fb_ptr sigma_bar sigma_text npts_bar npts_text ...
freq_bar freq_text phase_bar phase_text ...
bias_bar bias_text bias_checkbox s1calc_text rand_ptr add_button];
% bias_bar bias_text bias_checkbox w1_1_bar w1_1_text s1calc_text];
set(fig,'userdata',H,'nextplot','new')
set(sigma_bar,'enable','on');
% set(s1_bar,'enable','on');
set(npts_bar,'enable','on');
set(freq_bar,'enable','on');
set(phase_bar,'enable','on');
set(add_button,'enable','on')
% set(w1_1_bar,'enable','on');
if get(bias_checkbox,'value') == 0
set(bias_bar,'enable','on');
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -