【问题标题】:Matlab: neural network time series prediction?Matlab:神经网络时间序列预测?
【发布时间】:2013-02-22 02:36:27
【问题描述】:

背景:我正在尝试使用 MATLAB 的神经网络工具箱来预测数据的未来值。我从 GUI 运行它,但我还包含了下面的输出代码。

问题:我的预测值落后于实际值2个时间段,我不知道如何实际看到“t+1”(预测)值。

代码:

% Solve an Autoregression Time-Series Problem with a NAR Neural Network
% Script generated by NTSTOOL
% Created Tue Mar 05 22:09:39 EST 2013
%
% This script assumes this variable is defined:
%
%   close_data - feedback time series.

targetSeries = tonndata(close_data_short,false,false);

% Create a Nonlinear Autoregressive Network
feedbackDelays = 1:3;
hiddenLayerSize = 10;
net = narnet(feedbackDelays,hiddenLayerSize);

% Choose Feedback Pre/Post-Processing Functions
% Settings for feedback input are automatically applied to feedback output
% For a list of all processing functions type: help nnprocess
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};

% Prepare the Data for Training and Simulation
% The function PREPARETS prepares timeseries data for a particular network,
% shifting time by the minimum amount to fill input states and layer states.
% Using PREPARETS allows you to keep your original time series data unchanged, while
% easily customizing it for networks with differing numbers of delays, with
% open loop or closed loop feedback modes.
[inputs,inputStates,layerStates,targets] = preparets(net,{},{},targetSeries);

% Setup Division of Data for Training, Validation, Testing
% For a list of all data division functions type: help nndivide
net.divideFcn = 'dividerand';  % Divide data randomly
net.divideMode = 'time';  % Divide up every value
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;

% Choose a Training Function
% For a list of all training functions type: help nntrain
net.trainFcn = 'trainlm';  % Levenberg-Marquardt

% Choose a Performance Function
% For a list of all performance functions type: help nnperformance
net.performFcn = 'mse';  % Mean squared error

% Choose Plot Functions
% For a list of all plot functions type: help nnplot
net.plotFcns = {'plotperform','plottrainstate','plotresponse', ...
  'ploterrcorr', 'plotinerrcorr'};


% Train the Network
[net,tr] = train(net,inputs,targets,inputStates,layerStates);

% Test the Network
outputs = net(inputs,inputStates,layerStates);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs)

% Recalculate Training, Validation and Test Performance
trainTargets = gmultiply(targets,tr.trainMask);
valTargets = gmultiply(targets,tr.valMask);
testTargets = gmultiply(targets,tr.testMask);
trainPerformance = perform(net,trainTargets,outputs)
valPerformance = perform(net,valTargets,outputs)
testPerformance = perform(net,testTargets,outputs)

% View the Network
view(net)

% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, plotresponse(targets,outputs)
%figure, ploterrcorr(errors)
%figure, plotinerrcorr(inputs,errors)

% Closed Loop Network
% Use this network to do multi-step prediction.
% The function CLOSELOOP replaces the feedback input with a direct
% connection from the outout layer.
netc = closeloop(net);
[xc,xic,aic,tc] = preparets(netc,{},{},targetSeries);
yc = netc(xc,xic,aic);
perfc = perform(net,tc,yc)

% Early Prediction Network
% For some applications it helps to get the prediction a timestep early.
% The original network returns predicted y(t+1) at the same time it is given y(t+1).
% For some applications such as decision making, it would help to have predicted
% y(t+1) once y(t) is available, but before the actual y(t+1) occurs.
% The network can be made to return its output a timestep early by removing one delay
% so that its minimal tap delay is now 0 instead of 1.  The new network returns the
% same outputs as the original network, but outputs are shifted left one timestep.
nets = removedelay(net);
[xs,xis,ais,ts] = preparets(nets,{},{},targetSeries);
ys = nets(xs,xis,ais);
closedLoopPerformance = perform(net,tc,yc)

提出的解决方案:我相信答案就在代码“早期预测网络”的最后一部分。我只是不确定如何消除“一次延迟”。

附加问题:是否有可以从中输出的函数,以便我可以反复使用它?还是我只需要在获得下一个时间段的数据后继续重新训练?

【问题讨论】:

  • 您确定问题出在代码中吗?如果您的时间序列是非平稳的,您可能会有输出滞后的印象!
  • 这是一个动态时间序列,是的。我想预测序列中的下一个值。我可以使用非线性自回归 (NAR) 神经网络来做到这一点吗?
  • NAR 的主要假设是数据是平稳的 - 即均值和方差随时间保持不变。固定数据的一个例子是正弦波,是吗?我的数据是随机的,它非线性变化且非平稳。您会推荐什么来尝试预测这一点?
  • 我相信你应该分步进行:(1)看数据是否平稳; (2)如果不是,处理它(例如,区分数据); (3) 测试最可能的模型,例如ar模型; (4) 尝试非线性模型,例如nar; (5) 去一个 nn 模型。
  • 如果我错了,请纠正我,但 NAR 网络只有一个要预测的输入,那么我们必须在“输入”和“目标”中写什么?

标签: matlab machine-learning neural-network time-series prediction


【解决方案1】:

为了确保这个问题在答案已经存在时不会保持开放,我将发布似乎解决该问题的评论:

感谢@DanielTheRocketMan

我认为你应该分步进行:

  1. 查看数据是否静止
  2. 如果不是,处理它(例如,区分数据)
  3. 测试最可能的模型,例如 ar 模型
  4. 尝试非线性模型,例如 nar
  5. 转到 nn 模型。

【讨论】:

    【解决方案2】:

    尝试更简单的版本。我已经测试了这段代码,这段代码对我来说很好。

    inputs = X;  %define input and target
    targets = y;
    
    hiddenLayerSize = 10;
    net = patternnet(hiddenLayerSize);
    
    %   Set up Division of Data for Training, Validation, Testing
    
    net.divideParam.trainRatio = 70/100;
    net.divideParam.valRatio = 15/100;
    net.divideParam.testRatio = 15/100;
    
    [net,tr] = train(net,inputs,targets);
    
    outputss(x,:) = net(inputs);
    
    errors = gsubtract(targets,outputss);
    mse(errors)
    

    【讨论】:

      猜你喜欢
      • 2013-01-15
      • 2014-07-02
      • 2017-06-20
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 2017-08-14
      相关资源
      最近更新 更多