功能:7输入1输出 算法:BP神经网络(MATLAB自定义函数)
电话:13483417110
输入:入炉温度、第一加热段温度、第二加热段温度、均热段温度、第一加热段停留时间、第二加热段停留时间、均热段停留时间
输出:出炉温度
数据摘自《唐钢 0 1700 加热炉模型开发与优化》
归一化需要保证每一列是一个样本、因此需要对原始数据进行转置。
clear;close all; clc;
% 网络架构:7-13-1
% 150个样本,每个样本8个变量、输入变量7个、输出变量1个
% y=f(x1,x2,x3,...,x7)
%第一步 读取数据
[data,text] = xlsread('tg1700.xlsx','sheet1','B4:I153');
input=data(:,1:7)';
output=data(:,8)';
%归一化[0,1]
[inputn,inputps]=mapminmax(input,0,1);
[outputn,outputps]=mapminmax(output,0,1);inputn = inputn';
outputn = outputn';nnI = 7;% 输入7个
nnM = 13;% 隐含层13个
nnO = 1;% 输出1个W1=2*rand(nnM,nnI)-1;
W2=2*rand(nnO,nnM)-1;lr = 0.01;% 学习率
epochs = 5000;% 训练次数for i=1:epochs[W1,W2]=TrainNET(W1,W2,inputn,outputn,lr);
end% 单点预测
xtest_=input(:,1);
xtest=mapminmax('apply',xtest_,inputps); % 对样本数据进行归一化v1test=W1*xtest;
y1test=Sigmond(v1test);
vtest=W2*y1test;
ytest_=Sigmond(vtest);
ytest=mapminmax('reverse',ytest_,outputps);formatSpec = 'The ytest is %f';
resutstr = sprintf(formatSpec,ytest)% 多点预测
N=size(inputn,1);
y_ = zeros(1,50);
for i=1:Nx=inputn(i,:)';%7*1v1=W1*x;%10*7*7*1=10*1y1=Sigmond(v1);v2=W2*y1;y_(i)=Sigmond(v2);
end
y=mapminmax('reverse',y_,outputps);figure(1)
subplot(121);
plot(y);
xlabel(' ','Fontsize',18);
ylabel(' ','Fontsize',18);
title('预测');subplot(122);
plot(output);
xlabel(' ','Fontsize',18);
ylabel(' ','Fontsize',18);
title('实测');figure(2)
plot(y);
hold on
plot(output);
xlabel(' ','Fontsize',18);
ylabel(' ','Fontsize',18);
legend('预测','实测')figure(3)
plot(output(121:150),'r--')
hold on;grid on
plot(y(121:150),'b-')
xlabel('\fontsize{14}\fontname{楷体}\rm{测试点}\rm{ (Num)}');
ylabel('\fontsize{14}\fontname{楷体}\rm{钢坯出炉温度}\rm{ (^oC)}');
set(gca,'ytick',[980:20:1200]);
set(gca,'fontsize',14,'fontname','times new roman');
legend('\fontsize{14}\fontname{楷体}\rm{实际值}','\fontsize{14}\fontname{楷体}\rm{预测值}');
legend('boxoff');
title('\fontsize{14}\fontname{times new roman}\rm{BPNN}');
function [W1,W2]=TrainNET(W1,W2,X,D,lr)a=lr; N=size(X,1);for k=1:N x=X(k,:)';d=D(k);v1=W1*x;y1=Sigmond(v1);v=W2*y1;y=Sigmond(v);e=d-y; delta=y.*(1-y).*e; e1=W2'*delta; delta1=y1.*(1-y1).*e1; dW2=a*delta*y1';dW1=a*delta1*x';W1=W1+dW1;W2=W2+dW2;end
end
function y=Sigmond(x)y=1./(1+exp(-x));
end
学习率 Ir = 0.01
学习率 Ir = 0.1