%B
clear all;
close all;
clc;
% 导入训练数据-----------------------
P = xlsread('777','sheet1','A2:G221');
%T = xlsread('777','sheet1','H2:H221');
T = xlsread('888','sheet1','C2:C221');
P = P';
T = T';
%设置训练样本集
trainx = P(:,1:200);
trainy = T(1:200);
%设置测试样本集
testx = P(:,201:220);
testy = T(201:220);
% 数据归一化
% 训练集
[Pn_train,inputps] = mapminmax(trainx);
[Tn_train,outputps] = mapminmax(trainy);
% 测试集
Pn_test = mapminmax('apply',testx,inputps);
Tn_test = mapminmax('apply',testy,outputps);
%------------------------------------
%创建神经网络
%net_1 = newff(minmax(P),[12,4],{'tansig','purelin'},'traingdm');
net_1 = newff(Pn_train,Tn_train,[7],{'tansig','purelin'},'traingdm');
% 当前输入层权值和阈值
inputWeights=net_1.IW{1,1};
inputbias=net_1.b{1};
% 当前网络层权值和阈值
layerWeights=net_1.LW{2,1};
layerbias=net_1.b{2};
%设置训练参数
net_1.trainParam.show = 50; %每间隔100步显示一次训练结果
net_1.trainParam.lr = 0.01; %学习速率0.05
net_1.trainParam.mc = 0.9;
net_1.trainParam.epochs = 20000; %允许最大训练步数1000步
net_1.trainParam.goal = 1e-3; %训练目标最小误差0.001
%训练网络
[net_1,tr] = train(net_1,Pn_train,Tn_train);
%%
%使用训练好的网络,自定义输入
%A = sim(net_1,P);
%理想输出与训练输出的结果进行比较
%E = T - A;
%计算误差
%MSE = mse(E)
%%
%第二组验证
A1 = sim(net_1,Pn_test);
%反归一化
yy = mapminmax('reverse',A1,outputps);
E1 = testy' - yy';
MSE1 = mse(E1)
%绘图
hold on;
figure(1);
%subplot(211);
plot(testy','-*');
plot(yy','-.gp');
legend('理论值','实际值');
hold off;
figure(2);
plot(E1','-or');
grid on;
xlabel('E1 = T1 - A1; ');ylabel('误差曲线');
%如果你对该次训练满意,可以保存训练好网络
%save('123.mat','net_1','tr');
<pre name="code" class="cpp">%Em神经网络预测
clear all;
close all;
clc;
% 导入训练数据-----------------------
P = xlsread('777','sheet1','A2:G221');
%T = xlsread('777','sheet1','H2:H221');
T = xlsread('888','sheet1','C2:C221');
P = P';
T = T';
%设置训练样本集
trainx = P(:,1:200);
trainy = T(1:200);
%设置测试样本集
testx = P(:,201:220);
testy = T(201:220);
testx1 = P(:,101:120);
testy1 = T(101:120);
% 数据归一化
% 训练集
[Pn_train,inputps] = mapminmax(trainx);
[Tn_train,outputps] = mapminmax(trainy);
% 测试集
Pn_test = mapminmax('apply',testx,inputps);
Tn_test = mapminmax('apply',testy,outputps);
%------------------------------------
%创建神经网络
net_1 = newelm(Pn_train,Tn_train,[12,4],{'tansig','purelin'},'traingdm');
%设置训练参数
net_1.trainParam.show = 50;
net_1.trainParam.lr = 0.05;
net_1.trainParam.mc = 0.9;
net_1.trainParam.epochs = 10000;
net_1.trainParam.goal = 1e-3;
%训练网络
net_1 = train(net_1,Pn_train,Tn_train);
%测试网络
%第1组验证
%使用训练好的网络,自定义输入
A1 = sim(net_1,Pn_test);
%反归一化
yy = mapminmax('reverse',A1,outputps);
E1 = testy' - yy'; %理想输出与训练输出的结果进行比较
%MSE1 = mse(E1) %计算误差
%绘图
hold on;
figure(1);
%subplot(211);
plot(testy','-*');
plot(yy','-.gp');
legend('理论值','预测值');
hold off;
figure(2);
plot(E1','-or');
grid on;
xlabel('E1 = T1 - A1; ');ylabel('误差曲线');