用MATLAB做的模糊神经网络代码
1个回答
展开全部
您好,是这样的:
经过训练后的参数比较差,用原数据输入训练好的网络,得出结果和要的结果误差很大,不明白是怎么回事?
还有要是多输入多输出这段程序该怎么改?模糊神经网络可以用matlab工具箱实现吗?
还有输入数据差别比较大(就是大小差异大)是不是要进行归一化再学习训练呢?
求解,求解答!
对于你的帮助不胜感激!
clear all
clc
close all
tic,
%[x,y]=data;
x=[1 2 3 4 5 6 7 8;
-1 -2 -3 -4 -5 -6 -7 -8];
y=[2 3 4 5 6 7 8 9]; %%%%%--数据显示,输入为-两输入,输出为-单输出。--------样本为p2组
[p1,p2]=size(x);
% 隶属度函数个数
k=7;
% 初始化四个隶属度函数的参数A,B及输出层初始权值W
for i=1:p1;
for j=1:k;
m(i,j)=1+0.6*rand(1);
b(i,j)=1+0.6*rand(1);
end
end
for j=1:k*k;
w(j)=1+rand(1);
end
%%%---推理计算输出值
for q=1:p2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%-----用同一隶属度参数对 输入样本 X 累计计算
% 选用高斯函数作为隶属度,求隶属度,共 size(x,2)+k 个。x(1) K个,x(2) K个
for i=1:p1;
for j=1:k;
u(i,j)=gaussmf(x(i,q),[m(i,j),b(i,j)]);
end
end
% 模糊推理计算:a21,a22.几个隶属度函数,得出几个值,此处已知输入为2
%%%%----由以前的取小做法改为相乘—prod(x,1) or prod(x,2)———
v=[];
for i=1:k
for j=1:k
v=[v,u(1,i)*u(2,j)];
end
end
% 归一化计算模糊推理的值;相当于已经除去了经典去模糊输出的分母值
for i=1:length(v);
v1(i)=v(i)/sum(v);
end
% 系统输出
% out1(q)=w*v';
% e(q)=(y(q)-out1(q));
% end
% out=out1
out1(q)=w*v1';
e(q)=y(q)-out1(q);
end
out=out1;
%- 三。参数修正过程。 增加方式,非批处理方式迭代
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%-----------------------------误差反向传播过程--------------------------------------------
% 取误差函数:E=(1/2)*sumsqr(t-y)
E=(1/2)*sumsqr(y-out);
EE=E;
% e=sum(y-out)
lr=0.3; % c2=zeros(2,2);
%%%%----------------------------------------误差反传后的参数修正过程-------------------
r=1; %
p=1;
s=1000; %
% e(r)=y(r)-out(r);
while p<=s & EE>0.05
%%%%%%%%%%%%%_____隶属度参数 M. B 输出层权值参数 W 的修正过程_____%%%%%%%%%%%%
%%1.--W
wc=zeros(1,k*k);
for i=1:k*k;
wc(i)=-lr*e(r)*v1(i);
end
%%2.--M
mc=zeros(p1,k);
for i=1:p1;
for j=1:k;
mc(i,j)=2*lr*e(r) * w(j) * (v(j)/u(i,j)) * exp(-((x(i,r)-m(i,j)).^2)/(b(i,j).^2))* (x(i,r)-m(i,j))/(b(i,j).^2);
end
end
%%3.--B
bc=zeros(p1,k);
for i=1:p1;
for j=1:k;
bc(i,j)=2*lr*e(r)* w(j) * (v(j)/u(i,j)) * exp(-((x(i,r)-m(i,j)).^2)/(b(i,j).^2)) * ((x(i,r)-m(i,j)).^2)/(b(i,j).^3);
end
end
% 4.参数修正 m b w
m=m-mc;
b=b-bc;
w=w-wc;
%%%%%%%%%%%_______利用修正后的参数重新计算_____________%%%%%%%%%%%%%%%%%%%%%
% 5.利用修正过的参数重新计算输出
for q=1:p2;
for i=1:p1;
for j=1:k;
u(i,j)=gaussmf(x(i,q),[m(i,j),b(i,j)]);
end
end
v=[];
for i=1:7
for j=1:7
v=[v,u(1,i)*u(2,j)];
end
end
% 归一化计算模糊推理的值;相当于已经除去了经典去模糊输出的分母值
for i=1:length(v)
v1(i)=v(i)/sum(v);
end
out1(q)=w*v1';
end
out=out1;
p=p+1;
EE=(1/2)*sumsqr(y-out);
E(p)=EE;
r=r+1;
if r>p2
r=1;
end
e(r)=(y(r)-out(r));
end
%%%%%%%%%%%%%%%%%%%________________当误差或迭代步数满足要求后得到结果_________________%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
m,b,w,E_out=EE,e
epoch=1:size(E,2);
figure
plot(epoch,E,'-r');
% plot(epoch,out(1),'b');
% axis([0 1.5*s min(E) max(E)]);
% set(gca,'fontsize',8);
% set(gca,'xtick',0:s/10:1.5*s);
%set(gca,'ytick',1e-30:1e5:1e5);
%set(gcf,'color','b')
title('误差变化曲线');xlabel('步数');ylabel('误差');
toc
%% %% 泛化过程
经过训练后的参数比较差,用原数据输入训练好的网络,得出结果和要的结果误差很大,不明白是怎么回事?
还有要是多输入多输出这段程序该怎么改?模糊神经网络可以用matlab工具箱实现吗?
还有输入数据差别比较大(就是大小差异大)是不是要进行归一化再学习训练呢?
求解,求解答!
对于你的帮助不胜感激!
clear all
clc
close all
tic,
%[x,y]=data;
x=[1 2 3 4 5 6 7 8;
-1 -2 -3 -4 -5 -6 -7 -8];
y=[2 3 4 5 6 7 8 9]; %%%%%--数据显示,输入为-两输入,输出为-单输出。--------样本为p2组
[p1,p2]=size(x);
% 隶属度函数个数
k=7;
% 初始化四个隶属度函数的参数A,B及输出层初始权值W
for i=1:p1;
for j=1:k;
m(i,j)=1+0.6*rand(1);
b(i,j)=1+0.6*rand(1);
end
end
for j=1:k*k;
w(j)=1+rand(1);
end
%%%---推理计算输出值
for q=1:p2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%-----用同一隶属度参数对 输入样本 X 累计计算
% 选用高斯函数作为隶属度,求隶属度,共 size(x,2)+k 个。x(1) K个,x(2) K个
for i=1:p1;
for j=1:k;
u(i,j)=gaussmf(x(i,q),[m(i,j),b(i,j)]);
end
end
% 模糊推理计算:a21,a22.几个隶属度函数,得出几个值,此处已知输入为2
%%%%----由以前的取小做法改为相乘—prod(x,1) or prod(x,2)———
v=[];
for i=1:k
for j=1:k
v=[v,u(1,i)*u(2,j)];
end
end
% 归一化计算模糊推理的值;相当于已经除去了经典去模糊输出的分母值
for i=1:length(v);
v1(i)=v(i)/sum(v);
end
% 系统输出
% out1(q)=w*v';
% e(q)=(y(q)-out1(q));
% end
% out=out1
out1(q)=w*v1';
e(q)=y(q)-out1(q);
end
out=out1;
%- 三。参数修正过程。 增加方式,非批处理方式迭代
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%-----------------------------误差反向传播过程--------------------------------------------
% 取误差函数:E=(1/2)*sumsqr(t-y)
E=(1/2)*sumsqr(y-out);
EE=E;
% e=sum(y-out)
lr=0.3; % c2=zeros(2,2);
%%%%----------------------------------------误差反传后的参数修正过程-------------------
r=1; %
p=1;
s=1000; %
% e(r)=y(r)-out(r);
while p<=s & EE>0.05
%%%%%%%%%%%%%_____隶属度参数 M. B 输出层权值参数 W 的修正过程_____%%%%%%%%%%%%
%%1.--W
wc=zeros(1,k*k);
for i=1:k*k;
wc(i)=-lr*e(r)*v1(i);
end
%%2.--M
mc=zeros(p1,k);
for i=1:p1;
for j=1:k;
mc(i,j)=2*lr*e(r) * w(j) * (v(j)/u(i,j)) * exp(-((x(i,r)-m(i,j)).^2)/(b(i,j).^2))* (x(i,r)-m(i,j))/(b(i,j).^2);
end
end
%%3.--B
bc=zeros(p1,k);
for i=1:p1;
for j=1:k;
bc(i,j)=2*lr*e(r)* w(j) * (v(j)/u(i,j)) * exp(-((x(i,r)-m(i,j)).^2)/(b(i,j).^2)) * ((x(i,r)-m(i,j)).^2)/(b(i,j).^3);
end
end
% 4.参数修正 m b w
m=m-mc;
b=b-bc;
w=w-wc;
%%%%%%%%%%%_______利用修正后的参数重新计算_____________%%%%%%%%%%%%%%%%%%%%%
% 5.利用修正过的参数重新计算输出
for q=1:p2;
for i=1:p1;
for j=1:k;
u(i,j)=gaussmf(x(i,q),[m(i,j),b(i,j)]);
end
end
v=[];
for i=1:7
for j=1:7
v=[v,u(1,i)*u(2,j)];
end
end
% 归一化计算模糊推理的值;相当于已经除去了经典去模糊输出的分母值
for i=1:length(v)
v1(i)=v(i)/sum(v);
end
out1(q)=w*v1';
end
out=out1;
p=p+1;
EE=(1/2)*sumsqr(y-out);
E(p)=EE;
r=r+1;
if r>p2
r=1;
end
e(r)=(y(r)-out(r));
end
%%%%%%%%%%%%%%%%%%%________________当误差或迭代步数满足要求后得到结果_________________%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
m,b,w,E_out=EE,e
epoch=1:size(E,2);
figure
plot(epoch,E,'-r');
% plot(epoch,out(1),'b');
% axis([0 1.5*s min(E) max(E)]);
% set(gca,'fontsize',8);
% set(gca,'xtick',0:s/10:1.5*s);
%set(gca,'ytick',1e-30:1e5:1e5);
%set(gcf,'color','b')
title('误差变化曲线');xlabel('步数');ylabel('误差');
toc
%% %% 泛化过程
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询