资源简介
自己做的神经网络逼近函数的小例子,非常适合初学者理论与编程对照学习!!
代码片段和文件信息
close all;
clc;
t1=[0:0.1:pi]‘;%自变量t1
y=sin(t1);%因变量y
size_n=size(t11);%输入样本的个数
train_num=10;%训练样本个数
hid_num=5;%隐含层神经单元个数
w=0.3*zeros(1hid_num);%隐含层-输出层的权值
b=0.8*ones(1hid_num);%节点基宽
% center=[2.490.831.693.25-0.02]; %基函数中心 sin pi
% center=[6.862.4018.241.644.63]; %基函数中心 sin 2pi
center=rand(1hid_num);
eta_w=0.25*ones(1hid_num);%学习率η
eta_b=0.25*ones(1hid_num);
eta_c=0.25*ones(1hid_num);
alpha=0.5;%动量因子α
maxgen=800;%最大迭代次数
t2=1:1:maxgen;
error_goal=0.000000000000000000000000000000000000000001;%误差精度要求
mse=zeros(1maxgen);
wk_1=w;wk_2=w;%前1 2 时刻的权值
bk_1=b;bk_2=b;%前1 2 时刻的基宽值
ck_1=center;ck_2=center;%前1 2时刻的中心值
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 提取训练样本 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:train_num
seq=floor(i/train_num*size_n);
train_in(i)=t1(seq:);
train_out(i)=y(seq:);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 迭代学习参数 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gen=0;
for i=1:maxgen
gen=gen+1;
B(gen:)=b;
W(gen:)=w;
C(gen:)=center;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 训练样本 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=1:train_num
for k=1:hid_num
h(k)=exp(-norm(train_in(j)-center(k))^2/(b(k)^2));
end
ye(j)=w*h‘;
%%%%%%%%%%%%%%%%%%%%%%%% 隐含层参数学习 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:hid_num
e=train_out(j)-ye(j); %定义误差
w(k)=wk_1(k)+eta_w(k)*e*h(k)+alpha*(wk_1(k)-wk_2(k)); %隐含层-输出层的权值w学习公式
b(k)=bk_1(k)-eta_b(k)*(e*w(k)*h(k
评论
共有 条评论