在 matlab 上配置和训练后,如何使用神经网络运行预测?

Posted

技术标签:

【中文标题】在 matlab 上配置和训练后,如何使用神经网络运行预测?【英文标题】:How do I run a forecast with neural network after configuring it and training it on matlab? 【发布时间】:2021-02-16 15:25:50 【问题描述】:

我使用 nnstart 命令并获得了一个用于配置和训练网络的 matlab 应用程序。导入数据并训练网络后,我无法选择实际运行时间序列预测。我能做的最好的事情是我可以生成一个脚本。但该脚本似乎没有包含实际进行预测的程序。这是代码。如何运行预测?另外,如何选择激活函数 g(x)?

% Solve an Input-Output Time-Series Problem with a Time Delay Neural Network
% Script generated by Neural Time Series app.
% Created 03-Nov-2020 23:33:27
%
% This script assumes these variables are defined:
 %
 %   data - input time series.
 %   data_1 - target time series.

 X = tonndata(data,false,false);
  T = tonndata(data_1,false,false);

  % Choose a Training Function
  %For a list of all training functions type: help nntrain
    % 'trainlm' is usually fastest. 
   % 'trainbr' takes longer but may be better for challenging problems.
    % 'trainscg' uses less memory. Suitable in low memory situations.
    trainFcn = 'trainlm';  % Levenberg-Marquardt backpropagation.

    % Create a Time Delay Network
     inputDelays = 1:2;
     hiddenLayerSize = 10;
     net = timedelaynet(inputDelays,hiddenLayerSize,trainFcn);

     % Choose Input and Output Pre/Post-Processing Functions
     % For a list of all processing functions type: help nnprocess
     net.input.processFcns = 'removeconstantrows','mapminmax';
     net.output.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.
    [x,xi,ai,t] = preparets(net,X,T);

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

% 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', 'ploterrhist', ...
 'plotregression', 'plotresponse', 'ploterrcorr', 'plotinerrcorr';

  % Train the Network
   [net,tr] = train(net,x,t,xi,ai);

  % Test the Network
  y = net(x,xi,ai);
  e = gsubtract(t,y);
  performance = perform(net,t,y)

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

% View the Network
view(net)

% Plots
 % Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotresponse(t,y)
%figure, ploterrcorr(e)
%figure, plotinerrcorr(x,e)

 % Step-Ahead 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 x(t+1). For some applications such as decision making, it would
 % help to have predicted y(t+1) once x(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);
 nets.name = [net.name ' - Predict One Step Ahead'];
 view(nets)
 [xs,xis,ais,ts] = preparets(nets,X,T);
  ys = nets(xs,xis,ais);
  stepAheadPerformance = perform(nets,ts,ys)

  % Deployment
  % Change the (false) values to (true) to enable the following code blocks.
% See the help for each generation function for more information.
if (false)
% Generate MATLAB function for neural network for application
% deployment in MATLAB scripts or with MATLAB Compiler and Builder
% tools, or simply to examine the calculations your trained neural
% network performs.
genFunction(net,'myNeuralNetworkFunction');
y = myNeuralNetworkFunction(x,xi,ai);
end
if (false)
% Generate a matrix-only MATLAB function for neural network code
% generation with MATLAB Coder tools.
genFunction(net,'myNeuralNetworkFunction','MatrixOnly','yes');
x1 = cell2mat(x(1,:));
xi1 = cell2mat(xi(1,:));
y = myNeuralNetworkFunction(x1,xi1);
end
if (false)
% Generate a Simulink diagram for simulation or deployment with.
% Simulink Coder tools.
gensim(net);
end
 % Solve an Input-Output Time-Series Problem with a Time Delay Neural Network
% Script generated by Neural Time Series app.
% Created 03-Nov-2020 23:33:27
%
% This script assumes these variables are defined:
%
%   data - input time series.
%   data_1 - target time series.

X = tonndata(data,false,false);
T = tonndata(data_1,false,false);

% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. Suitable in low memory situations.
 trainFcn = 'trainlm';  % Levenberg-Marquardt backpropagation.

% Create a Time Delay Network
 inputDelays = 1:2;
 hiddenLayerSize = 10;
net = timedelaynet(inputDelays,hiddenLayerSize,trainFcn);

% Choose Input and Output Pre/Post-Processing Functions
 % For a list of all processing functions type: help nnprocess
 net.input.processFcns = 'removeconstantrows','mapminmax';
 net.output.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.
 [x,xi,ai,t] = preparets(net,X,T);
  
 % Setup Division of Data for Training, Validation, Testing
 % For a list of all data division functions type: help nndivision
 net.divideFcn = 'dividerand';  % Divide data randomly
 net.divideMode = 'time';  % Divide up every sample
 net.divideParam.trainRatio = 70/100;
 net.divideParam.valRatio = 15/100;
 net.divideParam.testRatio = 15/100;

 % 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', 'ploterrhist', ...
 'plotregression', 'plotresponse', 'ploterrcorr', 'plotinerrcorr';

 % Train the Network 
[net,tr] = train(net,x,t,xi,ai);

% Test the Network
y = net(x,xi,ai);
e = gsubtract(t,y);
performance = perform(net,t,y)

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

% View the Network
view(net)

% Plots
%Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotresponse(t,y)
%figure, ploterrcorr(e)
%figure, plotinerrcorr(x,e)

% Step-Ahead 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 x(t+1). For some applications such as decision making, it would
% help to have predicted y(t+1) once x(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);
nets.name = [net.name ' - Predict One Step Ahead'];
view(nets)
[xs,xis,ais,ts] = preparets(nets,X,T);
ys = nets(xs,xis,ais);
stepAheadPerformance = perform(nets,ts,ys)

% Deployment
% Change the (false) values to (true) to enable the following code blocks.
% See the help for each generation function for more information.
if (false)
% Generate MATLAB function for neural network for application
% deployment in MATLAB scripts or with MATLAB Compiler and Builder
% tools, or simply to examine the calculations your trained neural
% network performs.
genFunction(net,'myNeuralNetworkFunction');
y = myNeuralNetworkFunction(x,xi,ai);
end
if (false)
% Generate a matrix-only MATLAB function for neural network code
% generation with MATLAB Coder tools.
genFunction(net,'myNeuralNetworkFunction','MatrixOnly','yes');
x1 = cell2mat(x(1,:));
xi1 = cell2mat(xi(1,:));
y = myNeuralNetworkFunction(x1,xi1);
end
if (false)
% Generate a Simulink diagram for simulation or deployment with.
% Simulink Coder tools.
gensim(net);
end

【问题讨论】:

【参考方案1】:

对于分类模型,请在您的模型对象上使用 predictY = predict(Mdl,X)

对于回归模型,请在模型对象上使用 simY = sim(Mdl,X)

与其他语言不同,MATLAB 不会将所有方法包装到一个类中,而是有一个适合所有模型的命令(实际上,有两个命令:一个用于分类数据,一个用于连续预测)。因此,您也可以在 SVM (fitcsvm/fitrsvm) 或 kNN (fitcknn) 上使用它们。

【讨论】:

在这种情况下,Mdl 和 X 是什么?我假设 X 是您希望预测的时间步数? 不,mdl 是您训练的模型。这是您的 NN 对象。X 是您要预测的数据的特征矩阵。您的网络通常只适用于预定义数量的时间步长。预测范围是固定的(与训练阶段相同)。

以上是关于在 matlab 上配置和训练后,如何使用神经网络运行预测?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Matlab 中使用经过训练的神经网络在真实系统中进行分类

matlab中神经网络训练结束后出现nntraintool面板,如何记录此时nntraintool上的performance值和epoch值?

Matlab - 神经网络 - 如何使用不同的数据集进行训练、验证和测试?

如何在 Matlab 中改进神经网络中的数字识别预测?

如何在 Matlab 中逐步训练神经网络?

Matlab - 将训练(神经网络)图像特征插入 ms 访问数据库