ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Matlab BPNet系统辨识

2021-12-03 15:32:33  阅读:160  来源: 互联网

标签:w2 rand 输出 BPNet w3 Matlab det3 辨识 赋值


Matlab BPNet系统辨识

请添加图片描述

2-20-10-1

function DualModelIden
%% 参数初始化
l = 0.009; % 学习率
alfa = 0.05; % 动量因子

cells1 = 20; % 隐层神经元个数
cells2 = 10;

w1 = rand(cells1,2); % 随机赋值第一层连接权系数 [20 ,2]
w2 = rand(cells2,cells1); % 随机赋值第二层连接权系数 [10, 20]
w3 = rand(1,cells2); % 随机赋值第三层连接权系数 [1, 10]

yw1 = rand(cells1,1); % 随机赋值第一层输出阈值 [20, 1]
yw2 = rand(cells2,1); % 随机赋值第二层输出阈值 [10, 1]
yw3 = rand; % 随机赋值第三层输出阈值 [1]

ts=0.001; % 样本
n = 1000; % 样本数
yn = rand(1,n); % 随机赋值输出(预测)
y = rand(1,n); % 随机赋值输出(真实)

counts = 1; % 计数值初始化

x = [0,0]'; % 输入

u_1 = 0; % 上一时刻的输入
y_1 = 0; % 上一时刻的输出

times = 300; % 训练轮数
e=zeros(1,times); % 均方差初始值设为0
%% 学习过程
for i=1:times % 学习轮数
    ei=0;
    for a=1:n % 样本数
        
        time(a)=a*ts;
        u(a)=0.50*sin(3*2*pi*a*ts);
        y(a)=u_1^3+y_1/(1+y_1^2);
    
        net1=w1*x-yw1; % 第一层网络的输入 [20, 1]
        out1=logsig(net1); % 第一层网路的输出 [20, 1]
        net2=w2*out1-yw2; % 第二层网络的输入 [10, 20]*[20 ,1]=[10, 1]
        out2=logsig(net2); % 第二层网络的输出 [10, 1]
        net3=w3*out2-yw3; % 第三层网络的输出 [1]
        yn(a)=net3; % 第三层网络的输出 [1]
        
        det3=y(a)-yn(a); % 计算偏差 [1]
        det2=((det3*(w3))*out2)*(1-out2); % ([1, 10]'*[10 ,1])*[10, 1] = [10, 1]
        det1=((det2'*(w2))*out1)*(1-out1); % [20, 1]
     
        w1=w1+det1*x'*l; % [20, 2]
        w2=w2+(det2*out1')*l; % [10, 20]
        w3=w3+(det3*out2')*l; % [1, 10]
        
        yw1=-det1*l+yw1;
        yw2=-det2*l+yw2;
        yw3=-det3*l+yw3;
        
        ei=ei+det3^2/2;
        e(i)=ei;      
        
        x(1)=u(a); % 更新输入
        x(2)=y(a);
    
        u_1=u(a);
        y_1=y(a);
    end % 结束一次样本遍历
    
    if ei<0.008
        break;
    end
    counts=counts+1;
    
end % 结束学习
%% 计算学习的曲线
x = [0,0]'; % 输入
yn_test = rand(1,n); % 随机赋值输出(预测)
y_test = rand(1,n); % 随机赋值输出(真实)
u_1 = 0; % 上一时刻的输入
y_1 = 0; % 上一时刻的输出
ts=0.1; % 样本
n = 1000; % 样本数
for a=1:n
    u(a)=sin(2*pi*a*ts/25) + sin(2*pi*a*ts/10);
    y_test(a)=u_1^3+y_1/(1+y_1^2);
 
    net1=w1*x-yw1;
    out1=logsig(net1);
    net2=w2*out1-yw2;
    out2=logsig(net2);
    net3=w3*out2-yw3;
    yn_test(a)=net3;
    
	x(1)=u(a);
	x(2)=y_test(a);
    
	u_1=u(a);
	y_1=y_test(a);
end
%% 绘图
figure(1);
subplot(2,1,1);
plot(time,y,'b-',time,yn,'r-');
legend('true', 'forecast')
grid on
title('BP学习方法逼近y=0.5*(1+cos(x))');
xlabel('x轴');
ylabel('y=0.5*(1+cos(x))');

if (counts<times)
    count=1:counts;
    sum=counts;
else 
    count=1:times;
    sum=times;
end

subplot(2,1,2);
plot(count,e(1:sum));
grid on;
title('BP算法学习曲线');
xlabel('迭代次数');
ylabel('Mean-Square-Error');

figure(2);
plot(time,y_test,'b-',time,yn_test,'r-');
legend('true', 'forecast')
grid on
title('BP学习方法逼近y=sin(2*pi*k/25) + sin(2*pi*k/10)');
xlabel('x轴');
ylabel('y=sin(2*pi*k/25) + sin(2*pi*k/10)');
return

请添加图片描述
请添加图片描述

1-6-2-1 * 2

请添加图片描述

function DualModelIden
%% 参数初始化
l = 0.009; % 学习率
alfa = 0.05; % 动量因子

cells1 = 6; % 隐层神经元个数
cells2 = 2;

w1_1 = rand(cells1,1); % 随机赋值第一层连接权系数 [6 ,1]
w2_1 = rand(cells2,cells1); % 随机赋值第二层连接权系数 [2, 6]
w3_1 = rand(1,cells2); % 随机赋值第三层连接权系数 [1, 2]
yw1_1 = rand(cells1,1); % 随机赋值第一层输出阈值 [6, 1]
yw2_1 = rand(cells2,1); % 随机赋值第二层输出阈值 [2, 1]
yw3_1 = rand; % 随机赋值第三层输出阈值 [1]

w1_2 = rand(cells1,1); % 随机赋值第一层连接权系数 [6 ,1]
w2_2 = rand(cells2,cells1); % 随机赋值第二层连接权系数 [2, 6]
w3_2 = rand(1,cells2); % 随机赋值第三层连接权系数 [1, 2]
yw1_2 = rand(cells1,1); % 随机赋值第一层输出阈值 [6, 1]
yw2_2 = rand(cells2,1); % 随机赋值第二层输出阈值 [2, 1]
yw3_2 = rand; % 随机赋值第三层输出阈值 [1]

ts=0.001; % 样本
n = 1000; % 样本数
yn = rand(1,n); % 随机赋值输出(预测)
y = rand(1,n); % 随机赋值输出(真实)

counts = 1; % 计数值初始化

x = [0,0]'; % 输入

u_1 = 0; % 上一时刻的输入
y_1 = 0; % 上一时刻的输出

times = 300; % 训练轮数
e=zeros(1,times); % 均方差初始值设为0
%% 学习过程
for i=1:times % 学习轮数
    ei=0;
    for a=1:n % 样本数
        
        time(a)=a*ts;
        u(a)=0.50*sin(3*2*pi*a*ts);
        y(a)=u_1^3+y_1/(1+y_1^2);
    
        net1_1=w1_1*x(1)-yw1_1; % 第一层网络的输入 [6, 1]
        out1_1=logsig(net1_1); % 第一层网路的输出 [6, 1]
        net2_1=w2_1*out1_1-yw2_1; % 第二层网络的输入 [2, 6]*[6 ,1]=[2, 1]
        out2_1=logsig(net2_1); % 第二层网络的输出 [2, 1]
        net3_1=w3_1*out2_1-yw3_1; % 第三层网络的输出 [1]
        y_1=net3_1; % 第三层网络的输出 [1]
        
        net1_2=w1_2*x(2)-yw1_2; % 第一层网络的输入 [6, 1]
        out1_2=logsig(net1_2); % 第一层网路的输出 [6, 1]
        net2_2=w2_2*out1_2-yw2_2; % 第二层网络的输入 [2, 6]*[6 ,1]=[2, 1]
        out2_2=logsig(net2_2); % 第二层网络的输出 [2, 1]
        net3_2=w3_2*out2_2-yw3_2; % 第三层网络的输出 [1]
        y_2=net3_2; % 第三层网络的输出 [1]
        
        yn(a) = y_1 + y_2; % 计算两个模型的总输出
        det3=y(a)-yn(a); % 计算偏差 [1]
        
        det3_1=det3; % 计算偏差 [1]
        det2_1=((det3_1*(w3_1))*out2_1)*(1-out2_1); % ([1, 2]*[2 ,1])*[2, 1] = [2, 1]
        det1_1=((det2_1'*(w2_1))*out1_1)*(1-out1_1); % [6, 1]
        w1_1=w1_1+det1_1*x(1)'*l; % [6, 1]
        w2_1=w2_1+(det2_1*out1_1')*l; % [2, 6]
        w3_1=w3_1+(det3_1*out2_1')*l; % [1, 2]
        yw1_1=-det1_1*l+yw1_1;
        yw2_1=-det2_1*l+yw2_1;
        yw3_1=-det3_1*l+yw3_1;
        
        det3_2=det3; % 计算偏差 [1]
        det2_2=((det3_2*(w3_2))*out2_2)*(1-out2_2); % ([1, 2]*[2 ,1])*[2, 1] = [2, 1]
        det1_2=((det2_2'*(w2_2))*out1_2)*(1-out1_2); % [6, 1]
        w1_2=w1_2+det1_2*x(2)'*l; % [6, 1]
        w2_2=w2_2+(det2_2*out1_2')*l; % [2, 6]
        w3_2=w3_2+(det3_2*out2_2')*l; % [1, 2]
        yw1_2=-det1_2*l+yw1_2;
        yw2_2=-det2_2*l+yw2_2;
        yw3_2=-det3_2*l+yw3_2;
        
        ei=ei+det3^2/2;
        e(i)=ei;      
        
        x(1)=u(a); % 更新输入
        x(2)=y(a);
    
        u_1=u(a);
        y_1=y(a);
    end % 结束一次样本遍历
    
    if ei<0.008
        break;
    end
    counts=counts+1;
    
end % 结束学习
%% 计算学习的曲线
x = [0,0]'; % 输入
yn_test = rand(1,n); % 随机赋值输出(预测)
y_test = rand(1,n); % 随机赋值输出(真实)
u_1 = 0; % 上一时刻的输入
y_1 = 0; % 上一时刻的输出
ts=0.1; % 样本
n = 1000; % 样本数
for a=1:n
    u(a)=sin(2*pi*a*ts/25) + sin(2*pi*a*ts/10);
    y_test(a)=u_1^3+y_1/(1+y_1^2);
 
	net1_1=w1_1*x(1)-yw1_1; % 第一层网络的输入 [6, 1]
	out1_1=logsig(net1_1); % 第一层网路的输出 [6, 1]
	net2_1=w2_1*out1_1-yw2_1; % 第二层网络的输入 [2, 6]*[6 ,1]=[2, 1]
	out2_1=logsig(net2_1); % 第二层网络的输出 [2, 1]
	net3_1=w3_1*out2_1-yw3_1; % 第三层网络的输出 [1]
	y_1=net3_1; % 第三层网络的输出 [1]
        
	net1_2=w1_2*x(2)-yw1_2; % 第一层网络的输入 [6, 1]
	out1_2=logsig(net1_2); % 第一层网路的输出 [6, 1]
	net2_2=w2_2*out1_2-yw2_2; % 第二层网络的输入 [2, 6]*[6 ,1]=[2, 1]
	out2_2=logsig(net2_2); % 第二层网络的输出 [2, 1]
	net3_2=w3_2*out2_2-yw3_2; % 第三层网络的输出 [1]
	y_2=net3_2; % 第三层网络的输出 [1]

	yn_test(a) = y_1 + y_2; % 计算两个模型的总输出
    
	x(1)=u(a);
	x(2)=y_test(a);
    
	u_1=u(a);
	y_1=y_test(a);
end
%% 绘图
figure(1);
subplot(2,1,1);
plot(time,y,'b-',time,yn,'r-');
legend('true', 'forecast')
grid on
title('BP学习方法逼近y=0.5*(1+cos(x))');
xlabel('x轴');
ylabel('y=0.5*(1+cos(x))');

if (counts<times)
    count=1:counts;
    sum=counts;
else 
    count=1:times;
    sum=times;
end

subplot(2,1,2);
plot(count,e(1:sum));
grid on;
title('BP算法学习曲线');
xlabel('迭代次数');
ylabel('Mean-Square-Error');

figure(2);
plot(time,y_test,'b-',time,yn_test,'r-');
legend('true', 'forecast')
grid on
title('BP学习方法逼近y=sin(2*pi*k/25) + sin(2*pi*k/10)');
xlabel('x轴');
ylabel('y=sin(2*pi*k/25) + sin(2*pi*k/10)');
return

请添加图片描述
请添加图片描述
并联网络与单个网络效果类似,没有跑出书上的效果,个人估计的原因是权值更新部分的问题,这里写的是两个网络的权值更新方法一样,书上的没看懂所以这么写的。
而且看着两个的仿真图,他的预测把真实值的峰值给摸掉了,是不是可以用来做平滑!!!

标签:w2,rand,输出,BPNet,w3,Matlab,det3,辨识,赋值
来源: https://blog.csdn.net/qq_41990294/article/details/121698006

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有