BP神经网络风速预测

BP(Backpropagation)神经网络,也称为反向传播神经网络,是一种非常重要的人工神经网络。它基于梯度下降算法,通过反向传播误差来更新神经网络中的权重和偏差,以达到优化网络和提高预测准确性的目的。

BP神经网络主要包括以下几个步骤:

前向传播:在这个阶段,输入数据被送入网络,并通过每一层传播,直到输出层。每一层的输出都是下一层的输入。每个神经元的输出都是其权重加权输入的总和,再经过一个活化函数。

计算误差:在得到网络输出后,我们需要计算预测值与实际值之间的差异,这个差异被称为误差或损失。常用的误差计算方法有均方误差、交叉熵等。

反向传播:在这个阶段,误差会从输出层开始,反向传播至每一层,用于更新每一层的权重和偏差。误差的反向传播是通过链式法则(Chain Rule)来完成的。

权重更新:根据计算出的误差,我们可以按照梯度下降的方法来更新每个神经元的权重和偏差。通常,我们会设定一个学习率来控制更新的步长。

迭代训练:以上的步骤会在大量的数据上反复进行,直到网络的预测性能达到满意的水平,或者达到预设的迭代次数。

BP神经网络是一种强大的工具,可以应用于许多复杂的预测和分类任务。然而,它也有一些缺点,比如可能会陷入局部最优,或者在面对复杂的网络结构和大规模的数据时,训练过程可能会非常耗时。

数据:

时间

0

15

30

45

3月2日0时

3.530887

5.36667

4.949443

6.20111

3月2日1时

7.369333

4.615667

5.28322

3.614333

3月2日2时

3.447443

4.69911

4.615667

6.53489

3月2日3时

5.116333

5.45011

6.785223

6.618333

3月2日4时

7.119

8.787877

10.12301

10.03957

3月2日5时

10.62368

10.62367

8.120333

7.035553

3月2日6时

7.619667

8.704443

9.622357

11.4581

3月2日7时

10.28989

10.37333

10.95743

9.789233

3月2日8时

11.625

9.288557

8.704443

8.370667

3月2日9时

7.119

8.287223

7.953447

8.03689

3月2日10时

8.787887

7.703113

8.787887

11.12433

3月2日11时

11.45813

10.03954

8.871333

9.705767

3月2日12时

9.872643

8.537553

11.87533

11.70843

3月2日13时

10.54022

8.203777

8.954767

10.20644

3月2日14时

10.03957

10.20644

11.37466

10.2899

3月2日15时

9.9561

7.28589

6.451443

6.368

3月2日16时

6.701777

6.117667

5.95078

5.78389

3月2日17时

3.614333

4.031557

5.950777

5.700443

3月2日18时

4.281887

4.198447

5.867333

6.701777

3月2日19时

7.28589

7.28589

6.785223

6.618333

3月2日20时

6.70178

6.701777

7.786553

7.536223

3月2日21时

7.202447

5.28322

5.283223

7.118997

3月2日22时

8.954777

7.786557

7.786557

5.616997

3月2日23时

5.28322

5.45011

5.116333

5.28322

3月3日0时

6.368

4.865997

6.034223

4.615663

3月3日1时

4.53222

4.782553

3.280557

3.19711

3月3日2时

4.699113

5.283223

4.866

4.281887

3月3日3时

3.614333

3.113667

3.030223

2.779887

3月3日4时

2.696447

3.19711

7.369333

8.787877

3月3日5时

10.95743

7.28589

10.54021

10.28991

3月3日6时

9.37199

9.789233

12.29254

8.287213

3月3日7时

6.70178

6.95211

8.03689

7.28589

3月3日8时

8.621

6.785223

8.954767

8.120333

3月3日9时

9.53889

8.03689

10.95747

7.53622

3月3日10时

7.119

9.288553

10.79054

9.705777

3月3日11时

8.62099

11.70846

12.70977

12.62633

3月3日12时

12.45947

10.123

13.4608

13.3773

3月3日13时

11.12432

13.04353

12.87664

12.04223

3月3日14时

10.45678

12.12567

8.03689

10.28987

3月3日15时

12.37597

12.1257

7.953443

10.20643

3月3日16时

10.28989

8.704447

8.454113

8.28722

3月3日17时

10.79054

10.37333

10.20644

7.28589

3月3日18时

9.872667

8.120333

7.869997

7.953443

3月3日19时

8.537557

7.53622

7.452777

5.116333

3月3日20时

4.448777

5.950777

6.368003

5.533557

3月3日21时

2.946777

3.53089

3.447443

2.362663

3月3日22时

4.53222

4.782557

3.697777

4.69911

3月3日23时

4.699113

4.365333

4.949443

5.700443

3月4日0时

4.69911

5.45011

5.45011

4.115

3月4日1时

5.533553

4.365333

4.198443

5.78389

3月4日2时

4.44878

4.866

5.366667

3.69778

3月4日3时

4.115

5.03289

5.11633

3.69778

3月4日4时

3.614333

5.283223

5.617003

6.284557

3月4日5时

4.782553

3.697777

4.36533

4.865997

3月4日6时

4.69911

5.283223

5.366667

4.866

3月4日7时

4.115

3.447443

3.614333

2.696443

3月4日8时

2.36267

3.614337

3.03022

3.94811

3月4日9时

3.530887

3.030223

4.448777

4.281887

3月4日10时

4.198447

4.782557

2.199224

2.863333

3月4日11时

3.948113

4.031557

3.280553

2.696443

3月4日12时

3.447443

3.113663

4.53222

3.94811

3月4日13时

3.864667

2.863333

4.949443

3.41089

3月4日14时

5.950777

5.283223

4.115

5.116333

3月4日15时

4.53222

3.614333

4.365333

3.19711

3月4日16时

4.782553

5.116333

4.44878

6.201113

3月4日17时

8.120337

6.701777

6.367997

5.867333

3月4日18时

5.867337

5.617

5.28322

5.867333

3月4日19时

5.36667

5.366667

4.61567

4.615663

3月4日20时

5.19978

5.78389

4.949443

4.44878

3月4日21时

4.365333

3.197113

1.488222

2.279223

3月4日22时

2.112333

2.446113

2.362667

2.362667

3月4日23时

2.195777

2.44611

1.945447

0.950999

MATLAB代码:

%% BP神经网络 预测风速

clc;close all;clear all;%清除变量

%% 全部数据输入

%% 1训练

%% (1)读取数据和数据转换

filename1='风速数据.xls';

[data,~,c1]=xlsread(filename1);%读取数据

% 数据处理

dt=15/60;

t0=0;

datasep=reshape(data',1,numel(data));

long1=length(datasep);

datatime=zeros(1,long1);

for t=1:long1

    datatime(1,t)=t0+(t-1)*dt;

end

datatime;

datasep;

%% 生成输入数据(7天预测第八天)

N=7;

inputdata=zeros(N,long1-N);

outputdata=zeros(1,long1-N);

times=zeros(1,long1-N);

for i=1:long1-N

    a1=i;

    a2=a1+N-1;

    inputdata(1:N,i)=datasep(a1:a2);

    outputdata(1,i)=datasep(a2+1);

    times(1,i)=datatime(a2+1);%时间

end

Outputdata=outputdata;

Inputdata=inputdata;

index1=randperm(size(Outputdata,2));

index2=1:size(Outputdata,2);

numberTest=100;%用于测试的样本个数(数据集的最后numberTest个作为测试样本) ,可以修改!!

% 定义训练集

P1=Inputdata(:,index1(1:end-numberTest));

T1=Outputdata(:,index1(1:end-numberTest));

t1=times(:,index1(1:end-numberTest));

% 定义测试集(用最后10个样本做测试)

P2=Inputdata(:,index2(end-numberTest+1:end));

T2=Outputdata(:,index2(end-numberTest+1:end));

t2=times(:,index2(end-numberTest+1:end));

%设置训练集和测试集

%% (2)训练数据归一化

[~,inputps]=mapminmax(Inputdata);

[~,outputps]=mapminmax(Outputdata);

%测试数据归一化

input_train=mapminmax('apply',P1,inputps);

output_train=mapminmax('apply',T1,outputps);

input_test=mapminmax('apply',P2,inputps);

%% (3)BP的构建,训练和测试

%% 搜索最优隐含节点数

set1=6:2:16;

long2=length(set1);

BPa=zeros(long2,1);

for i=1:long2

    hnumber=set1(i);

    net=newff(input_train,output_train,hnumber);%新建BP神经网络net

    % 设置神经网络的参数

    net.divideFcn ='';

    net.trainparam.epochs=1000;%训练次数

    net.trainparam.goal=0.000000001;%精度

    net.trainparam.lr=0.01;%学习率

    [net,tr]=train(net,input_train,output_train);%训练

    %% 2测试

    testvalue=sim(net,input_test);

    testvalue=mapminmax('reverse',testvalue,outputps);%预测数据反归一化

    BPa(i,1)=sum((T2-testvalue).^2./T2);

end

[v1,indexbest]=min(BPa);

%% 绘制隐含节点数的影响

figure;

plot(set1,BPa,'bo--');

xlabel('隐含节点数');

ylabel('相对误差');

title('BP神经网络预测的相对误差与隐含节点数的关系');

hnumberbest=set1(indexbest);

net=newff(input_train,output_train,hnumberbest);%新建BP神经网络net

% 设置神经网络的参数

net.divideFcn ='';

net.trainparam.epochs=1000;%训练次数

net.trainparam.goal=0.000000001;%精度

net.trainparam.lr=0.01;%学习率

[net,tr]=train(net,input_train,output_train);%训练

%% 2测试

testvalue=sim(net,input_test);

testvalue=mapminmax('reverse',testvalue,outputps);%预测数据反归一化

%% (4)输出结果

mse01=tr.perf;

RMSETrain1=sqrt(mse01(end));

testvalue1=testvalue;

t2=t2/24;

figure;

plot(T2,'b-');

hold on;

plot(testvalue1,'r--');

legend('实际值','BP神经网络预测值');

% set(gca,'xtick',t2);

for i=1:length(t2)

    tm{1,i}=datestr(t2(i),'HH:MM');

end

set(gca,'xticklabel',tm);

xlabel('时间');

ylabel('风速');

title('BP神经网络预测的结果');

figure;

plot((testvalue1-T2),'r-');

legend('预测的绝对误差');

for i=1:length(t2)

    tm{1,i}=datestr(t2(i),'HH:MM');

end

set(gca,'xticklabel',tm);

xlabel('时间');

ylabel('绝对误差');

title('BP神经网络预测的绝对误差');

figure;

plot((testvalue1-T2)./T2*100,'r-');

for i=1:length(t2)

    tm{1,i}=datestr(t2(i),'HH:MM');

end

set(gca,'xticklabel',tm);

legend('预测的相对误差');

xlabel('时间');

ylabel('误差(%)');

title('BP神经网络预测的误差');

disp('最佳隐含节点数');

hnumberbest

disp([filename1,'BP神经网络训练的RMSE']);

RMSETrain1

disp('实际风速');

T2

disp('全部数据输入的BP神经网络预测结果');

testvalue1

E1=testvalue1-T2;%绝对误差

E2=(testvalue1-T2)./T2*100;%相对误差

%% 输出结果到Excel

outcell={'时间(h)','实际风速','BP预测风速','预测的绝对误差','预测的相抵误差(%)'};

datacell=[t2',T2',testvalue1',E1',E2'];

datacell=num2cell(datacell);

outcell=[outcell;

    datacell];

xlswrite('BP神经网络预测结果.xls',outcell);

BP神经网络风速预测_第1张图片

BP神经网络风速预测_第2张图片

BP神经网络风速预测_第3张图片

BP神经网络风速预测_第4张图片

你可能感兴趣的:(神经网络,人工智能,深度学习)