已知八个经济指标的10年数据,如何对这个八个经济指标通过构建BP神经网络模型,对未来二十年的数据进行预测。
怎么编程,希望高手指点?
clear all
close all
aa=xlsread('xmsj.xls');
at=[{'日期:2001至2010'};{'交通量'};{'GDP'};{'人均可支配收入'};{'人口总量'};{'汽车保有量'};......
{'旅游人均居民消费水平'};{'工业生产总值'};{'农业生产总值'}];
ys='brgmcyk'; %未知?
%%输入数据及目标数据
pd=aa(:,1:10)';
[pn,minp,maxp]=premnmx(pd);
p=pn(1:9,:)'; %p存放训练的输入数据
t=pn(10,:)'; %t存放训练的目标数据
%%%测试数据及目标数据
pdc=aa(:,5:10)'; %pdc表示测试数据
[pnc,minpc,maxpc]=premnmx(pdc); %
ptest=pnc(1:5,:)';%%ptest存放测试集的输入数据
ttest=pnc(6,:)';%%ttest存放测试集的目标数据
%计算训练集的样本个数和输入元素个数
%Q存放样本个数
%R存放输入元素个数
[Q,R]=size(p);
%隐含层神经元个数
H=10; %%隐层神经元数(??)
%创建一个新的前向神经网络
net=newff(minmax(p'),[H,1],{'tansig','purelin'},'trainlm');
% 初始化输入层权值和偏置值
net.IW{1,1}=0.1*rand(H,R);
inputWeights=net.IW{1,1};
net.b{1}=0.1*rand(H,1);
inputbias=net.b{1};
% 初始化网络层权值和偏置值
net.LW{2,1}=0.1*rand(1,H);
layerWeights=net.LW{2,1};
net.b{2}=0.1*rand(1);
layerbias=net.b{2};
%设置网络训练参数
%设置显示间隔
net.trainParam.show=50;
%设置学习速率
net.trainParam.lr=0.05;
%设置动量项
net.trainParam.mc=0.25
%设置训练次数
net.trainParam.epochs=5000;
%设置训练目标值
net.trainParam.goal=1e-5;
%训练BP网络
P=p';
T=t';
[net,tr]=train(net,P,T);
%仿真计算测试集的输出
output=sim(net,ptest');
%将计算结果输出到Excel文件output_ann.xls中
%绘制仿真结果图
figure(1);
plot(output,'ok');
%保持图形不被后续图形绘制操作刷新
hold on;
%绘制阈值上下限
x=linspace(1,6);
y1=0.51;
y2=0.65;
plot(x,y1,'-k',x,y2,'-k');
运行结果:
net =
Neural Network object:
architecture:
numInputs: 1
numLayers: 2
biasConnect: [1; 1]
inputConnect: [1; 0]
layerConnect: [0 0; 1 0]
outputConnect: [0 1]
targetConnect: [0 1]
numOutputs: 1 (read-only)
numTargets: 1 (read-only)
numInputDelays: 0 (read-only)
numLayerDelays: 0 (read-only)
subobject structures:
inputs: {1x1 cell} of inputs
layers: {2x1 cell} of layers
outputs: {1x2 cell} containing 1 output
targets: {1x2 cell} containing 1 target
biases: {2x1 cell} containing 2 biases
inputWeights: {2x1 cell} containing 1 input weight
layerWeights: {2x2 cell} containing 1 layer weight
functions:
adaptFcn: 'trains'
initFcn: 'initlay'
performFcn: 'mse'
trainFcn: 'trainlm'
parameters:
adaptParam: .passes
initParam: (none)
performParam: (none)
trainParam: .epochs, .goal, .max_fail, .mem_reduc,
.min_grad, .mu, .mu_dec, .mu_inc,
.mu_max, .show, .time, .lr,
.mc
weight and bias values:
IW: {2x1 cell} containing 1 input weight matrix
LW: {2x2 cell} containing 1 layer weight matrix
b: {2x1 cell} containing 2 bias vectors
other:
userdata: (user stuff)
TRAINLM, Epoch 0/5000, MSE 0.342733/1e-005, Gradient 1.01865/1e-010
TRAINLM, Epoch 5/5000, MSE 4.729e-010/1e-005, Gradient 0.000224833/1e-010
TRAINLM, Performance goal met.
??? Error using ==> network.sim
Inputs are incorrectly sized for network.
Matrix must have 9 rows.
Error in ==> xmyy at 56
output=sim(net,ptest');
怎么解决? 望高手指点,谢谢!