神经网络中的BP算法和Elman算法


clear all;
%输入数据
P = [0.4413 0.4707 0.6953 0.8133 0.4379 0.4677 0.6981 0.8002 0.4517 0.4725 0.7006 0.8201;
     0.4379 0.4677 0.6981 0.8002 0.4517 0.4725 0.7006 0.8201 0.4557 0.4790 0.7019 0.8211;
     0.4517 0.4725 0.7006 0.8201 0.4557 0.4790 0.7019 0.8211 0.4601 0.4911 0.7101 0.8298]';
%输出数据
T = [0.4557 0.4790 0.7019 0.8211;
     0.4601 0.4911 0.7101 0.8298;
     0.4612 0.4845 0.7188 0.8312]';

%创建神经网络
net_1 = newff(minmax(P),[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,tr] = train(net_1,P,T);

%使用训练好的网络,自定义输入
A = sim(net_1,P);
%理想输出与训练输出的结果进行比较
E = T - A;
%计算误差
MSE = mse(E)

%第二组验证
P1 = [0.4557 0.4790 0.7019 0.8211 0.4601 0.4911 0.7101 0.8298 0.4612 0.4845 0.7188 0.8312]';
T1 = [0.4615 0.4891 0.7201 0.8330]';

A1 = sim(net_1,P1);
E1 = T1 - A1;
MSE1 = mse(E1)

%Elman神经网络预测空调负荷(仿BP)
clear all;
%输入数据
P = [0.4413 0.4707 0.6953 0.8133 0.4379 0.4677 0.6981 0.8002 0.4517 0.4725 0.7006 0.8201;
     0.4379 0.4677 0.6981 0.8002 0.4517 0.4725 0.7006 0.8201 0.4557 0.4790 0.7019 0.8211;
     0.4517 0.4725 0.7006 0.8201 0.4557 0.4790 0.7019 0.8211 0.4601 0.4911 0.7101 0.8298]';
%输出数据
T = [0.4557 0.4790 0.7019 0.8211;
     0.4601 0.4911 0.7101 0.8298;
     0.4612 0.4845 0.7188 0.8312]';

%创建神经网络
net_1 = newelm(minmax(P),[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,tr] = train(net_1,P,T);

%第1组验证
%使用训练好的网络,自定义输入
A1 = sim(net_1,P);
%理想输出与训练输出的结果进行比较
E1 = T - A1;
%计算误差
MSE = mse(E1)


%第2组验证(从输入样本中抽取一组输入P2,对应的输出样本中抽出T2)
P2 = [0.4517 0.4725 0.7006 0.8201 0.4557 0.4790 0.7019 0.8211 0.4601 0.4911 0.7101 0.8298]';
T2 = [0.4612 0.4845 0.7188 0.8312]';

A2 = sim(net_1,P2);
E2 = T2 - A2;
MSE1 = mse(E2)



%第3组验证 (本组输入P3和输出T3不是从训练样本中取得的,得到的误差有点大)
P3 = [0.4557 0.4790 0.7019 0.8211 0.4601 0.4911 0.7101 0.8298 0.4612 0.4845 0.7188 0.8312]';
T3 = [0.4615 0.4891 0.7201 0.8330]';

A3 = sim(net_1,P3);
E3 = T3 - A3;
MSE1 = mse(E3)




你可能感兴趣的:(matlab,--,神经网络)