?? cmac1d.m
字號(hào):
function cmac1d()
%
% A single input single output CMAC network
help cmac1d
clf reset
pausetime = 0.1;
P = -1:0.1:1;
P3 = -1:0.005:1;
% INITIALIZE
iprange = 256;
disp(sprintf('iprange is %d',iprange));
%width = input('enter generalisation width ');
%c = input('enter number of weights to be accessed, c ');
%beta = input('enter learning rate, beta ');
width=1;
c=10;
beta=0.5;
memreq = c*ceil(iprange/width);
s = sprintf('number of weights required is %d',memreq);
disp(s);
%memsize = input('enter memsize (0 to disable hashing) ');
memsize=0;
% INITIALISE WEIGHTS
% ==================
if memsize == 0
wts = zeros(memreq,1);
else
wts = zeros(memsize,1);
end
% PLOT CMAC OUTPUT
[wts,netout] = modcmac(wts,[(2.0)*64],1,1.0,1.0,iprange,c,width,memsize);
for k=1:401
[wts,op(k)] = opcmac(wts,[(P3(k)+2.0)*64],1,iprange,c,width,memsize);
end;
plot(P3,op,'r');
axis([-1.0,1.0,-2.0,2.0]);
title('CMAC output after one training example');
xlabel('Input Vector P');
ylabel('Network Output');
disp('any key to continue');
pause
%Define the associated twenty-one 1-element targets.
z = menu('select target function', ...
'damped sinusoid', ...
'step', ...
'impulse');
disp('')
if z == 1
T = [-.9602 -.5770 -.0729 .3771 .6405 .6600 .4609 ...
.1336 -.2013 -.4344 -.5000 -.3930 -.1647 .0988 ...
.3072 .3960 .3449 .1816 -.0312 -.2189 -.3201];
else
if z == 2
T = [0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1];
else
T = [0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0];
end
end
plot(P,T,'+');
title('Training Vectors');
xlabel('Input Vector P');
ylabel('Target Vector T');
hold on
% PLOT INITIAL
if memsize == 0
wts = zeros(memreq,1);
else
wts = zeros(memsize,1);
end
for i=1:401
[wts,op(i)] = opcmac(wts,[(P3(i)+2.0)*64],1,iprange,c,width,memsize);
end
plot(P3,op,'m');
title('Function Approximation');
xlabel('Input Vector P');
ylabel('Target + Network -');
h = get(gca,'Children');
h = h(1);
hold on
pause2(pausetime);
% TRAIN THE NETWORK
%==================
% TRAINING PARAMETERS
max_epoch = input('enter maximum number of training epochs ');
epoch = 1;
SSE = 1;
while (epoch < max_epoch) %& (SSE > 0.02)
for i = 1:21
[wts,netout] = modcmac(wts,[(P(i)+2.0)*64],1,T(i),beta,iprange,c,width,memsize);
end
for k=1:401
[wts,op(k)] = opcmac(wts,[(P3(k)+2.0)*64],1,iprange,c,width,memsize);
end
%for i=1:memreq
% disp(sprintf('%d',i));
% disp(sprintf('wts is :%d',wts(i)));
%end;
for i = 1:21
[wts,p]=opcmac(wts,[(P(i)+2.0)*64],1,iprange,c,width,memsize);
neterr(i) = T(i) - p;
end
SSE = sumsqr(neterr);
fprintf('epoch = %d SSE = %5.3f\n',epoch,SSE);
delete(h);
h = plot(P3,op,'m');
drawnow
pause2(pausetime);
epoch = epoch + 1;
end
end
disp('any key to continue');
pause
hold off
bar(wts,'r')
title('Network Weights');
xlabel('Weight Number');
ylabel('Weight Value');
if memsize == 0
set(gca,'xlim',[0 memreq]);
else
set(gca,'xlim',[0 memsize]);
end
function [wts,op]=opcmac(wts,ip,ipdim,iprange,c,width,memsize)
% OP = OPCMAC(WTS,IP,IPRANGE,C,WIDTH,MEMSIZE)
HASH=12345;
addrs=zeros(c,1);
quantisation=ceil(iprange/width);
offset=width/c;
ofs=0;
op=0;
for i=1:c
address=0;
shift=1.0;
for j=1:ipdim
%address=address+rem((ceil((ip(j)+ofs)/width)),quantisation)*shift;
address=address+ceil(ip(j)/width)*shift;
shift=shift*quantisation;
end;
% address=address+shift*i;
if memsize>0
addrs(i)=ceil(rem(((log(address+1))*HASH),memsize));
else
%addrs(i)=ceil(rem(address,c*ceil(iprange/width)));
memreq = c*ceil(iprange/width);
addrs(i)=ceil(rem(address,memreq));
end;
ofs=ofs+offset;
op=op+wts(addrs(i));
end;
function [wts,mod]=modcmac(wts,ip,ipdim,target,beta,iprange,c,width,memsize)
%sub function
HASH=12345;
addrs=zeros(c,1);
quantisation=ceil(iprange/width);
offset=width/c;
ofs=0;
sum=0.0;
for i=1:c
address=0;
shift=1.0;
for j=1:ipdim
%address=address+rem((ceil((ip(j)+ofs)/width)),quantisation)*shift;
address=address+ceil(ip(j)/width)*shift;
shift=shift*quantisation;
end;
address=address+shift*i;
if memsize>0
addrs(i)=ceil(rem((log(address+1)*HASH),memsize));
else
%addrs(i)=ceil(rem(address,c*ceil(iprange/width)));
memreq = c*ceil(iprange/width);
addrs(i)=ceil(rem(address,memreq));
end;
ofs=ofs+offset;
sum=sum+wts((addrs(i)));
end;
delta=beta*(target-sum)/c;
for i=1:c
wts(addrs(i))=wts(addrs(i))+delta;
end;
mod=sum+delta*c;
%s=sprintf('mod is %d',mod);
%disp(s);
% for i=1:c
%disp(sprintf('wts is %d',wts(addrs(i))));
%disp(sprintf('addrs is %d',addrs(i)));
%end;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -