This repository has been archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
neural_network_tools_use.m
77 lines (74 loc) · 2.61 KB
/
neural_network_tools_use.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
clc;
close ;
clear ;
% 原始数据
% 人数(单位:万人)
sqrs=[20.55 22.44 25.37 27.13 29.45 30.10 30.96 34.06 36.42 38.09 39.13 39.99 ...
41.93 44.59 47.30 52.89 55.73 56.76 59.17 60.63];
%机动车数(单位:万辆)
sqjdcs=[0.6 0.75 0.85 0.9 1.05 1.35 1.45 1.6 1.7 1.85 2.15 2.2 2.25 2.35 2.5 2.6...
2.7 2.85 2.95 3.1];
%公路面积(单位:万平方公里)
sqglmj=[0.09 0.11 0.11 0.14 0.20 0.23 0.23 0.32 0.32 0.34 0.36 0.36 0.38 0.49 ...
0.56 0.59 0.59 0.67 0.69 0.79];
%公路客运量(单位:万人)
glkyl=[5126 6217 7730 9145 10460 11387 12353 15750 18304 19836 21024 19490 20433 ...
22598 25107 33442 36836 40548 42927 43462];
%公路货运量(单位:万吨)
glhyl=[1237 1379 1385 1399 1663 1714 1834 4322 8132 8936 11099 11203 10524 11115 ...
13320 16762 18673 20724 20803 21804];
% 输入数据矩阵
p=[sqrs;sqjdcs;sqglmj];
% 目标数据矩阵
t=[glkyl;glhyl];
% 利用mapminmax函数对数据进行归一化
[pn,input_str] = mapminmax(p) ;
[tn,output_str] = mapminmax(t) ;
% 建立BP神经网络,相对旧一点的MATLAB版本,新版本 newff 函数使用更简洁一些
% 但是本质和性能没有区别
net=newff(pn,tn,[3 7 2],{'purelin','logsig','purelin'});
% 10轮回显示一次结果
net.trainParam.show=10;
% 学习速度为0.05
net.trainParam.lr=0.035;
% 最大训练次数为5000次
net.trainParam.epochs=5000;
% 均方误差
net.trainParam.goal=0.65*10^(-3);
% 网络误差如果连续6次迭代都没有变化,训练将会自动终止(系统默认的)
% 为了让程序继续运行,用以下命令取消这条设置
net.divideFcn = '';
% 开始训练,其中pn,tn分别为输入输出样本
net=train(net,pn,tn);
% 利用训练好的网络,基于原始数据对BP网络仿真
an=sim(net,pn);
% 利用函数mapminmax把仿真得到的数据还原为原始的数量级
% 新版本推荐训练样本归一化和反归一化都使用 mapminmax 函数
a = mapminmax('reverse',an,output_str);
% 本例因样本容量有限使用训练数据进行测试,通常必须用新鲜数据进行测试
x=1990:2009;
newk=a(1,:);
newh=a(2,:);
figure (2);
% 绘值公路客运量对比图
subplot(2,1,1);plot(x,newk,'r-o',x,glkyl,'b--+')
legend('网络输出客运量','实际客运量');
xlabel('年份');ylabel('客运量/万人');
title('运用工具箱客运量学习和测试对比图');
% 绘制公路货运量对比图
subplot(2,1,2);plot(x,newh,'r-o',x,glhyl,'b--+')
legend('网络输出货运量','实际货运量');
xlabel('年份');ylabel('货运量/万吨');
title('运用工具箱货运量学习和测试对比图');
% 利用训练好的网络进行预测
% 2010年和2011年的相关数据
% 当用训练好的网络对新数据mapminmax进行预测时,也应作相应的处理:
pnew=[73.39 75.55
3.9635 4.0975
0.9880 1.0268];
% 利用原始输入数据的归一化参数对新数据进行归一化
pnewn = mapminmax('apply',pnew,input_str);
% 利用归一化后的数据进行仿真
anewn=sim(net,pnewn);
% 把仿真得到的数据还原为原始的数量级
anew = mapminmax('reverse',anewn,output_str)