训练和仿真简单的 NARX 网络
本示例利用 narxnet (Deep Learning Toolbox) 和 nlarx 训练了一个具有外生输入的非线性自回归 (NARX) 神经网络,并通过在闭环条件下仿真该训练好的(开环)模型,将其对测试数据的响应与实际输出进行比较。有关使用 narxnet 和 nlarx 估计 NARX 网络的更多信息,请参阅Train NARX Networks Using idnlarx Instead of narxnet。
narxnet 方法
加载简单时间序列预测数据。
[X,T] = simpleseries_dataset;
将数据划分为训练数据 XTrain 和 TTrain,以及用于预测 XPredict 的数据。在创建闭环网络后,使用 XPredict 执行预测。
% training data XTrain = X(1:80); TTrain = T(1:80); % test data XPredict = X(81:100); YPredict = T(81:100); rng default % for reproducibility of shown results
使用 narxnet (Deep Learning Toolbox) 创建一个 NARX 网络。定义隐藏层的输入延迟、反馈延迟和大小。
numUnits = 4; model_narxnetOL = narxnet(1:2,1:2,numUnits);
使用 preparets (Deep Learning Toolbox) 准备时间序列训练数据。该函数会自动将输入和目标时间序列向后平移所需步数,以填充初始输入状态和层延迟状态。
[Xs,Xi,Ai,Ts] = preparets(model_narxnetOL,XTrain,{},TTrain);训练 NARX 网络并显示训练好的模型。train (Deep Learning Toolbox) 函数在开环条件下对网络进行训练,其中包括验证和测试步骤。
model_narxnetOL = train(model_narxnetOL,Xs,Ts,Xi,Ai); view(model_narxnetOL)

在闭环模式下对模型进行仿真。由于 model_narxnet 是一个开环模型,因此首先使用 closeloop (Deep Learning Toolbox) 将其转换为闭环模型。请注意所示模型中的反馈回路。
[model_narxnetCL1,Xi_CL,Ai_CL] = closeloop(model_narxnetOL,Xi,Ai); view(model_narxnetCL1)

使用 sim (Deep Learning Toolbox) 命令仿真闭环模型的输出。
Ys1 = sim(model_narxnetCL1, XPredict, Xi_CL, Ai_CL); Ys1 = cell2mat(Ys1)'; y_measured = cell2mat(YPredict)'; plot([y_measured,Ys1]) legend("Measured","model_narxnetCL1",Interpreter="none")

在闭环(并行架构)下训练模型。
model_narxnetCL2 = narxnet(1:2,1:2,numUnits,"closed");
[Xs2,Xi2,Ai2,Ts2] = preparets(model_narxnetCL2,XTrain,{},TTrain);
model_narxnetCL2 = train(model_narxnetCL2,Xs2,Ts2,Xi2,Ai2);
查看已训练好的模型。
view(model_narxnetCL2)

模型 model_narxnet_CL2 已处于闭环配置状态。因此,无需对其调用 closeloop (Deep Learning Toolbox) 命令。使用 sim (Deep Learning Toolbox) 命令仿真模型输出。
Ys2 = sim(model_narxnetCL2, XPredict, Xi2, Ai2); Ys2 = cell2mat(Ys2)'; plot([y_measured,Ys1,Ys2]) legend("Measured","model_narxnetCL1","model_narxnetCL2",Interpreter="none")

使用 goodnessOfFit 并采用归一化均方根误差 (NRMSE) 度量来衡量性能。
err1 = goodnessOfFit(y_measured,Ys1,"nrmse")err1 = 0.7679
err2 = goodnessOfFit(y_measured,Ys2,"nrmse")err2 = 0.9685
nlarx 方法
将之前使用的数据转换为双精度向量,以准备数据。
% training data XTrain = cell2mat(XTrain)'; TTrain = cell2mat(TTrain)'; % validation data XPredict = cell2mat(XPredict)'; YPredict = cell2mat(YPredict)';
准备模型阶数。
na = 2; nb = 2; nk = 1; Order = [na nb nk];
使用 idNeuralNetwork 创建一个与上文 narxnet (Deep Learning Toolbox) 模型所用函数类似的函数。也就是说,构建一个包含一个由 10 个单元组成的隐藏层 tanh 的神经网络。您可以使用现代的 dlnetwork (Deep Learning Toolbox) 对象或 RegressionNeuralNetwork (Statistics and Machine Learning Toolbox) 回归模型来构建该网络。
netfcn = idNeuralNetwork(numUnits,"tanh",NetworkType="RegressionNeuralNetwork");
神经网络函数 netfcn 采用线性映射与网络的并联连接方式。这在半物理建模中非常有用,在该场景下,您有选项可以使用现有的(可能是基于物理的)传递函数来初始化线性部分。不过,在这个示例中,请关闭线性映射功能,以便 netfcn 的结构与 narxnet 模型所使用的结构等价。
netfcn.LinearFcn.Use = false;
在开环条件下对nlarx模型进行辨识。使用 LM 训练方法。
Method = "lm"; opt = nlarxOptions(Focus="prediction",SearchMethod=Method); model_nlarxOL = nlarx(XTrain,TTrain,Order,netfcn,opt)
model_nlarxOL = Nonlinear ARX model with 1 output and 1 input Inputs: u1 Outputs: y1 Regressors: Linear regressors in variables y1, u1 List of all regressors Output function: Regression neural network Sample time: 1 seconds Status: Estimated using NLARX on time domain data "XTrain". Fit to estimation data: 42.51% (prediction focus) FPE: 0.02858, MSE: 0.0156 Model Properties
在闭环模式下训练模型(这种训练方式耗时较长)。
opt = nlarxOptions(Focus="simulation",SearchMethod=Method);
model_nlarxCL = nlarx(XTrain,TTrain,Order,netfcn,opt)model_nlarxCL = Nonlinear ARX model with 1 output and 1 input Inputs: u1 Outputs: y1 Regressors: Linear regressors in variables y1, u1 List of all regressors Output function: Regression neural network Sample time: 1 seconds Status: Estimated using NLARX on time domain data "XTrain". Fit to estimation data: 13.61% (simulation focus) FPE: 0.04746, MSE: 0.03524 Model Properties
model_nlarxOL 和 model_nlarxCL 在结构上相似,您可以使用其中任意一个进行开环或闭环评估。
使用 predict 命令执行开环评估。在系统辨识术语中,这一练习被称为 one-step-ahead。
Horizon = 1; % prediction horizon [yp1,ic1] = predict(XPredict,YPredict,model_nlarxOL,Horizon); [yp2,ic2] = predict(XPredict,YPredict,model_nlarxCL,Horizon); plot([y_measured,yp1,yp2]) legend("Measured","model_nlarxOL","model_nlarxCL",Interpreter="none") title("One-step-ahead (open-loop) Prediction")

使用 goodnessOfFit 并采用归一化均方根误差 (NRMSE) 度量来衡量性能。
err1 = goodnessOfFit(y_measured,yp1,"nrmse")err1 = 0.8347
err2 = goodnessOfFit(y_measured,yp2,"nrmse")err2 = 0.7552
使用 sim 命令执行闭环评估。在系统辨识术语中,这一操作被称为仿真或无限步前瞻预测。进行仿真时,您不需要该测量输出 (YPredict)。
ys1 = sim(model_nlarxOL,XPredict,simOptions(InitialCondition=ic1)); ys2 = sim(model_nlarxCL,XPredict,simOptions(InitialCondition=ic2)); plot([y_measured,ys1,ys2]) legend("Measured","model_nlarxOL","model_nlarxCL",Interpreter="none") title("Closed-loop Prediction (Simulation)")

使用 goodnessOfFit 并采用归一化均方根误差 (NRMSE) 度量来衡量性能。
err1 = goodnessOfFit(y_measured,ys1,"nrmse")err1 = 0.9925
err2 = goodnessOfFit(y_measured,ys2,"nrmse")err2 = 1.1590
另请参阅
train (Deep Learning Toolbox) | preparets (Deep Learning Toolbox) | closeloop (Deep Learning Toolbox) | narxnet (Deep Learning Toolbox) | idNeuralNetwork | idnlarx | nlarx | nlarxOptions | predict | iddata | linearRegressor | polynomialRegressor | getreg | periodicRegressor