首先介绍一下CMAC神经网络的算法过程:
1)获取样本:训练+测试样本
2)确定网络结构:神经元个数、激活个数、重 叠个数、压缩率、hash编码方式选择;
3)设置初始的权重数值
4)根据每个训练样本,计算激活的神经元以及 hash映射后的神经元,计算其加权和;
5)根据学习规则调整对应的权重;
6)针对每个训练样本进行同样的操作;
7)计算对应的训练误差,如果误差太大,就再 次迭代;否则,就终止;
8)用测试样本,计算对应的测试误差。
其次以一维函数进行matlab仿真
%%%%%%%%要求实现y=sinx函数的CMAC拟合%%%%%%%%
%%%%%%%%%%%%%%%%%% 初始化%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
max=50000;%%%%最大循环次数
c=5;%%%%%重复覆盖的区域的内存个数
data=360;%%%%需要输入的数据个数
error=0.00001;%%%误差精确度
alpha=0.3;%%%%调整步长
A=uint8(zeros(data,c));%%%用于存储每个数据所激活的内存空间地址
w=rand(1,80);%%%%初始化权值
%%%%%%%%求出训练数据的预测值%%%%%%%%
for i=1:data
x(i)=i/360*pi;
d(i)=sin(i/360*pi);
end
%%%%%%%%%%%%%hash编码%%%%%%%%%%%%%%%%%%%%%%
for i=1:c
for j=1:data
temp=0;
for k=1:i-1
temp=ceil(k/c+1)+temp;
end
A(j,i)=temp+(j-i)/c+1;
end
end
for m=1:max
for i=1:data
out=0;
for j=1:c
out=w(A(i,j))+out;
end
y(i)=out;
e(i)=d(i)-y(i);
if(abs(e(i)/d(i))
continue;
else
for k=1:c
w(A(i,k))=w(A(i,k))+alpha/3*e(i);
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%% 显示%%%%%%%%%%%%%%%%%%%%%
figure
plot(x,d,'r-');
hold on
plot(x,y,'g-');
hold on
plot(x,e,'b-');
阅读(3544) | 评论(0) | 转发(0) |