num_in=2 %输入层的神经元数
num_hide=3 %隐含层的神经元数
num_out=1 %输出层的神经元数
e_min=0.0000001
lan=0.9
max_epoch=1000
u1=1.8:1:20.8
u2=1.8:1:20.8
%u1=[0.8,1.3,1.9,2.4,2.9,3.6,4.1,4.8,5.6,6.9,7.8]
%u2=[0.8,1.3,1.9,2.4,2.9,3.6,4.1,4.8,5.6,6.9,7.8]
%d=(cos(2*pi*u1).*cos(2*pi*u2))'
%d=d'
%u=[0 0;0.1 0.1;0.2 0.2;0.3 0.3;0.4 0.4;0.5 0.5;0.6 0.6;0.7 0.7;0.8 0.8;0.9 0.9]
u=[u1',u2']
leng=length(u1)
%wij=rand(num_in,num_hide)
%wjk=rand(num_hide,num_out)
for i=1:num_in
for j=1:num_hide
wij(i,j)=1.8
end
end
for j=1:num_hide
for k=1:num_out
wjk(j,k)=0.2
end
end
%wij0=zeros(size(wij))
%wki0=zeros(size(wki))
d=(cos(2*pi*u1).*cos(2*pi*u2))'
e=1
%while e<e_min
%for epoch=1:max_epoch
for n=1:leng
while (e>e_min)
%隐含层各神经元的输出
for j=1:num_hide
net_hide(j)=0
for i=1:num_in
temp=wij(i,j)*u(n,i)
net_hide(j)=net_hide(j)+temp
end
y(n,j)=1/(1+exp(-net_hide(j)))
end
%输出层各神经元输出
for k=1:num_out
net_out(k)=0
for j=1:num_hide
temp=wjk(j,k)*y(n,j)
net_out(k)=net_out(k)+temp
end
o(n,k)=1/(1+exp(-net_out(k)))
end
%计算误差函数
e=((d(n,k)-o(n,k))*(d(n,k)-o(n,k)))/2
%e=((d-o)'*(d-o))/2
%调整输出层权值
for k=1:num_out
for j=1:num_hide
wjk(j,k)=wjk(j,k)+lan*o(n,k)*(1-o(n,k))*(d(n,k)-o(n,k))*y(n,j)
end
end
%调整隐含层权值
temp=0
for k=1:num_out
temp=temp+o(n,k)*(1-o(n,k))*(d(n,k)-o(n,k))*wjk(j,k)
end
for j=1:num_hide
for i=1:num_in
wij(i,j)=wij(i,j)+lan*temp*y(n,j)*(1-y(n,j))*u(n,i)
end
end
end
end
%%%%%%%%%%%%%%%%%%%%以上为学习%%%%%%%%%%%%%%%%%%%%
%开始训练
u1=1.8:1:20.8
u2=1.8:1:20.8
%u1=[0.8,1.3,1.9,2.4,2.9,3.6,4.1,4.8,5.6,6.9,7.8]
%u2=[0.8,1.3,1.9,2.4,2.9,3.6,4.1,4.8,5.6,6.9,7.8]
d=cos(2*pi*u1).*cos(2*pi*u2)
%d=d'
%u=[0 0;0.1 0.1;0.2 0.2;0.3 0.3;0.4 0.4;0.5 0.5;0.6 0.6;0.7 0.7;0.8 0.8;0.9 0.9]
u=[u1',u2']
leng=length(u1);
for n=1:leng
%隐含层各神经元的输出
for j=1:num_hide
net_hide(j)=0
for i=1:num_in
temp=wij(i,j)*u(n,i)
net_hide(j)=net_hide(j)+temp
end
y(n,j)=1/(1+exp(-net_hide(j)))
end
%输出层各神经元输出
for k=1:num_out
net_out(k)=0
for j=1:num_hide
temp=wjk(j,k)*y(n,j)
net_out(k)=net_out(k)+temp
end
o(n,k)=1/(1+exp(-net_out(k)))
end
end
%epoch
wij
wjk
o
figure(1)
plot(u1,o,'-r',u1,d,'-g')
评论0