卷积神经网络
卷积的物理意义是加权叠加——知乎大神
下面是本人的一个神经网络程序,当时为了预测天气编写,因为收敛过快无法得到充分训练,当时未能起到较好效果,但是对于理解matlab神经网络库的用法有帮助,是一个用神经网络预测时间序列,多变量时间序列的一个思路
使用回归神经网络:Elman
function [y,net]=tast(X)
%% 用小波变换处理的数据来做神经网络预测,用截取的过去做输入,紧接着作为输出
%% ===========数据准备=============
forecast = 6;mm = max(X);
X = X./mm;%因为是正值,直接除以最大值归一化
n = length(X);x = zeros(n-forecast+1,forecast);%x是输入数据,逐列输入
for i = 1:n-forecast+1 x(i,:) = X(i:i+forecast-1); end
x = x';t = X(forecast+1:end);t = [t,0.4];%目标数据,逐列输入
%% ========== Elman神经网络训练========
dx = [zeros(forecast,1) , ones(forecast,1)];
% net=newff(dx,[5,18,1],{'logsig','logsig','purelin'},'trainlm');
net=elmannet(1:2:3,15,'traingdx');
% net.trainparam.show=20;
% net.trainparam.lr=0.01;
% net.trainparam.epochs=3000;
% net.trainparam.goal=1e-3;
net.trainParam.max_fail=5;
% net.trainParam.mc=0.99;
% net.trainParam.delt_inc=1.2;
% net.trainParam.delt_dec=0.3;
% net.trainParam.deltamax=50;
% net.trainParam.time=inf;
% net.trainParam.mem_reduc=1;
% net.performFcn='sse';
% % net.layers{1}.initFcn = 'trainlm';
% net.inputWeights{1,1}.initFcn = 'trainlm';
% net.biases{1,1}.initFcn = 'trainlm';
% net.inputWeights{2,1}.initFcn = 'trainlm';
% net.biases{2,1}.initFcn = 'trainlm';
net=train(net,x,t);
%% ========= 用训练的网络预测========
Y = X;y =NaN(1,forecast) ;%y=Y(1:forecast)存放预测值,前十八天缺失
%因为要预测18天,前面的预测结果作为输入预测后面的结果
figure;grid on;
for i=1:n-forecast+18
temp = sim(net,Y(i:i+forecast-1)'); y = [y,temp];
if i>=n-forecast+1 Y = [Y,temp]; end
end
X = X.*mm;y = y.*mm;%数据还原
plot(X) ;hold on;plot(y);
% plot(0.7142857*ones(size(y)));
title('小波神经网络对气温的预测');
legend('原始数据','预测结果');hold off
end
BP神经网络[y,net] = tast(X)
function [y,net]=tast(X)
%% 用小波变换处理的数据来做神经网络预测,用18天作输入,用紧接着的18天作输出,现在共有74
%% 个数据,所以可以得到57组样本进行训练
%% ===========数据准备=============
forecast = 18;mm = max(X);
X = X./mm;%因为是正值,直接除以最大值归一化
n = length(X);
x = zeros(n-forecast+1,forecast);%x是输入数据,逐列输入
for i = 1:n-forecast+1
x(i,:) = X(i:i+forecast-1);
end
x = x';
t = X(forecast+1:end);
t = [t,0.4];%t少了一个,补一个
%目标数据,逐列输入
%% ========== BP神经网络训练========
dx = [zeros(forecast,1) , ones(forecast,1)];
net = newff(dx,[18,7,1],{'tansig','tansig','purelin'},'trainlm');
net.trainParam.show=300;
net.trainParam.Lr=0.05; %学习速率为0.05
net.trainParam.epochs=500; %最大训练轮回为100000;
net.trainParam.goal=0.65*10^(-14); %均方误差
net=train(net,x,t);
%% ========= 用训练的网络预测========
Y = X;y =NaN(1,forecast) ;%y=Y(1:forecast)存放预测值,前十八天缺失
%因为要预测18天,前面的预测结果作为输入预测后面的结果
figure;grid on;
for i=1:n
temp = sim(net,Y(i:i+forecast-1)'); y = [y,temp];
if i>=n-forecast+1 Y = [Y,temp]; end
end
X = X.*mm;y = y.*mm;%数据还原
plot(X) ;hold on;plot(y);
% plot(0.7142857*ones(size(y)));
title('小波神经网络对气温的预测');
legend('原始数据','预测结果');hold off
end
使用前馈神经网络进行多元时间序列预测——BP神经网络 [y,Y,net] = arimaBP(X)
function [y,Y,net]=arimaBP(X)
%多元时间序列预测——BP神经网络
%% ===========数据准备=============
forecast = 6; %mm = max(X);归一化,已经归一化,这里不用
%X = X./mm;%因为是正值,直接除以最大值归一化
[n,m]= size(X);
x = zeros(n-forecast+1,forecast*m);%x是输入数据,m列数据
for i = 1:n-forecast+1
temp = X(i:i+forecast-1,:);
end
t = X(forecast+1:end,:);
t = [t;X(end,:)]%目标数据,逐列输入
%% ========== BP神经网络训练========
dx = [zeros(forecast,1) , ones(forecast,1)];
net=newff(dx,[5,18,m],{'logsig','logsig','purelin'},'trainlm');
net.trainparam.show=20;
net.trainparam.lr=0.01;
net.trainparam.epochs=3000;
net.trainparam.goal=1e-3;
net.inputs{1}.size = m*forecast;
net.trainParam.mc=0.99;
net.trainParam.delt_inc=1.2;
net.trainParam.delt_dec=0.3;
net.trainParam.deltamax=50;
net.trainParam.time=inf;
net.trainParam.mem_reduc=1;
net.performFcn='sse';
% net.layers{1}.initFcn = 'trainlm';
net.inputWeights{1,1}.initFcn = 'trainlm';
net.biases{1,1}.initFcn = 'trainlm';
net.inputWeights{2,1}.initFcn = 'trainlm';
net.biases{2,1}.initFcn = 'trainlm';
net=train(net,x',t');
%% ========= 用训练好的网络预测========
Y = X;y =NaN(forecast,m) ;%y=Y(1:forecast)存放预测值,
for i=1:n-forecast+18 ttemp = Y(i:forecast+i-1,:);
temp = sim(net,ttemp(:)); y = [y;temp(:)'];
if i>=n-forecast+1 Y = [Y;temp(:)']; end
end
% X = X.*mm;y = y.*mm;%数据还原
end