load NET3 %网络
clear temp
%% 网络的参数,不过网络结构,{}里面的索引不一样
w{1}=net.IW{1};
w{2}= net.LW{2};
w{3}=net.LW{3,2};
w{4}=net.LW{4,3};
b=net.b;
%%归一化参数
[row,col] = find(net.inputConnect==1); %获取输入矩阵
ps_Xxmax = net.inputs{row,col}.range(:,2);
ps_Xxmin = net.inputs{row,col}.range(:,1);
ps_Xymax = net.inputs{row,col}.processedRange(:,2);
ps_Xymin = net.inputs{row,col}.processedRange(:,1);
[row,col] = find(net.outputConnect==1); %获取输入矩阵
ps_Yxmax = net.outputs{row,col}.range(:,2);
ps_Yxmin = net.outputs{row,col}.range(:,1);
ps_Yymax = net.outputs{row,col}.processedRange(:,2);
ps_Yymin = net.outputs{row,col}.processedRange(:,1);
%% 测试输入变量
dataX=[19513.4489795918,20577.612244898,20159.6326530612,20345.1020408163,19241.9387755102,19875.1428571429,17836.8163265306,18450.1734693878,19108.2142857143,17741.193877551,20197.5,17988.5
]';
%% 开始计算
temp{1} = (dataX-ps_Xxmin)./(ps_Xxmax-ps_Xxmin).*(ps_Xymax-ps_Xymin)+ ps_Xymin; %输入归一化
%% 矩阵计算和激活函数计算
for i=2:4
temp{i} = tansig_apply( w{i-1}*temp{i-1}+b{i-1} ); % 前numLayers-1循环计算
end
x = w{4}*temp{4}+b{4} % 最后一层不使用tansig函数
dataY = (ps_Yxmax-ps_Yxmin).*(x-ps_Yymin)./(ps_Yymax-ps_Yymin)+ps_Yxmin %反归一化
%%最后是将相应变量输出到txt,为了方便写入KEIL中
for i=1:length(w)
d=w{i};
d=d';
writematrix(d(:)',['w' num2str(i)]);
end
for i=1:length(b)
d=b{i};
writematrix(d(:)',['b' num2str(i)]);
end
writematrix((ps_Xxmax)','ps_Xxmax');
writematrix((ps_Xxmin)','ps_Xxmin');
writematrix((ps_Xymax)','ps_Xymax');
writematrix((ps_Xymin)','ps_Xymin');
writematrix((ps_Yxmax)','ps_Yxmax');
writematrix((ps_Yxmin)','ps_Yxmin');
writematrix((ps_Yymax)','ps_Yymax');
writematrix((ps_Yymin)','ps_Yymin');
function a = tansig_apply(n,~) %tansig函数,为了能够编译成C
a = 2 ./ (1 + exp(-2*n)) - 1;
end
———————————————— 版权声明:本文为CSDN博主「苏小泉」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/m0_37953670/article/details/120587487