1、% Solve an Autoregression Problem with External Input with a NARX Neural Network解决一个 NARX神经网络的外部输入自回归问题 % Script generated by Neural Time Series app脚本 由神经时间序列 程序 生成 % Created 28-Mar-2017 10:42:15 % % This script assumes these variables are defined:此脚本假设以下变量已 定义: % % in - input time series.in-输入时间序列
2、% out - feedback time series.out-反馈时间序列 %tonndata/fromnndata把数据转化为标准神经网络格式 X = tonndata(in,false,false); T = tonndata(out,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 bette
3、r for challenging problems. % trainscg uses less memory. Suitable in low memory situations. trainFcn = trainlm; % Levenberg-Marquardt backpropagation. % Create a Nonlinear Autoregressive Network with External Input inputDelays = 1:2; feedbackDelays = 1:2; hiddenLayerSize = 10; net = narxnet(inputDel
4、ays,feedbackDelays,hiddenLayerSize,open,trainFcn); % Choose Input and 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 % Customize input para
5、meters at: net.inputsi.processParam删除矩阵的行定值。映射矩阵行的最小值和 最大值为 1 - 1 % Customize output parameters at: net.outputsi.processParam删除矩阵的行定值。映射矩阵行的最小值和最大值为 1 - 1 net.inputs1.processFcns = removeconstantrows,mapminmax; net.inputs2.processFcns = removeconstantrows,mapminmax; % Prepare the Data for Training a
6、nd Simulation准备数据 用于 训 练 和模拟 % The function PREPARETS prepares timeseries data for a particular network, preparets功能 ,为 一个特定的网络 准备时间序列数据 , % 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, whi
7、le easily customizing it for networks with differing % numbers of delays, with open loop or closed loop feedback modes. 移动时间的最小量从而填写输入状态和层的状态。使用 PREPARETS使你可以保存原时间序列数据不变,同时更易 定制不同的网络延迟,通过开环或闭环反馈模式。 x,xi,ai,t = preparets(net,X,T); % Setup Division of Data for Training, Validation, Testing数据分割用于训练,验证,
8、测试 % For a list of all data division functions type: help nndivide 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 Fun
9、ction选择性能函数 % 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, plo
10、terrcorr, 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 = gmult
11、iply(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, plotpe
12、rform(tr) %figure, plottrainstate(tr) %figure, ploterrhist(e) %figure, plotregression(t,y) %figure, plotresponse(t,y) %figure, ploterrcorr(e) %figure, plotinerrcorr(x,e) % Closed Loop Network闭环网络 % Use this network to do multi-step prediction.利用此网络进行多步预测。 % The function CLOSELOOP replaces the feedba
13、ck input with a direct % connection from the outout layer.函数 CLOSELOOP使用一个输出层的直接连接 代替反馈输入 。 netc = closeloop(net); netc.name = net.name - Closed Loop; view(netc) xc,xic,aic,tc = preparets(netc,X,T); yc = netc(xc,xic,aic); closedLoopPerformance = perform(net,tc,yc) % Multi-step Prediction多步预测 % Somet
14、imes it is useful to simulate a network in open-loop form for as % long as there is known output data, and then switch to closed-loop form % to perform multistep prediction while providing only the external input. % Here all but 5 timesteps of the input series and target series are used % to simulat
15、e the network in open-loop form, taking advantage of the higher % accuracy that providing the target series produces: 有时以开环形式模拟网络 只要有已知的输出数据,然后切换到闭环形式仅提供外部输入时执行多步预测。这里只有 5步的输入序列和目标序列的方法用开环形式模拟网络,利用较高的提供目标系列的精度 numTimesteps = size(x,2); knownOutputTimesteps = 1:(numTimesteps-5); predictOutputTimestep
16、s = (numTimesteps-4):numTimesteps; X1 = X(:,knownOutputTimesteps); T1 = T(:,knownOutputTimesteps); x1,xio,aio = preparets(net,X1,T1); y1,xfo,afo = net(x1,xio,aio); % Next the the network and its final states will be converted to % closed-loop form to make five predictions with only the five inputs %
17、 provided.下一步网络和它的最终状态将被转换为闭环形式 , 提供五个输入 , 作出五个预测 。Typically, preparets is used to define initial input and layer states. Since these have already been obtained from the end of the open-loop simulation, you do not need preparets to continue with the 5 step predictions of the closed-loop network. x2
18、= X(1,predictOutputTimesteps); netc,xic,aic = closeloop(net,xfo,afo); y2,xfc,afc = netc(x2,xic,aic); multiStepPerformance = perform(net,T(1,predictOutputTimesteps),y2) % Alternate predictions can be made for different values of x2, or further % predictions can be made by continuing simulation with a
19、dditional external % inputs and the last closed-loop states xfc and afc.不同的 x2值做出交替的预测,或进一步的预测,模拟使用不用的外部输入和闭环的最新状态 xfc和 afc % 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
20、 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
21、 instead of 1. The new network returns the same outputs as the original % network, but outputs are shifted left one timestep. 对于某些应用,它有助于预测一个 timestep。原来的网络返回预测的 y(t+1),同时得到 y(t+1)。对于一些应用,如决策,它将在 y(t)是可用时帮助预测 y(t+1),但在 y(t+1)实际产生之前,网格可以通过移除一个延迟,返回一个输出的 timestep,以使现在的最小延迟由 0代替 1。新的网络返回了原网络一样的输出,但输出向左
22、左移动一个 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.将( fa
23、lse)值更改为( true)以启用下列代码块 % 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 you
24、r trained neural % network performs. 生成 MATLAB函数,用于神经网络的应用部署在 MATLAB脚本或与 MATLAB编译器和建立 工具,或者只是 简单 检查你 的已训练过 神经 网络的 计算 表现 genFunction(net,myNeuralNetworkFunction); y = myNeuralNetworkFunction(x,xi,ai); end if (false) % Generate a matrix-only MATLAB function for neural network code % generation with MA
25、TLAB Coder tools. genFunction(net,myNeuralNetworkFunction,MatrixOnly,yes); x1 = cell2mat(x(1,:); x2 = cell2mat(x(2,:); xi1 = cell2mat(xi(1,:); xi2 = cell2mat(xi(2,:); y = myNeuralNetworkFunction(x1,x2,xi1,xi2); end if (false) % Generate a Simulink diagram for simulation or deployment with. % Simulin
26、k Coder tools. gensim(net); end Enhanced multi-timestep prediction for switching between open-loop and closed-loop modes with NARX and NAR neural networks Dynamic networks with feedback, such as narxnet and narnet neural networks, can be transformed between open-loop and closed-loop modes with the f
27、unctions openloop and closeloop. Closed-loop networks make multistep predictions. In other words, they continue to predict when external feedback is missing, by using internal feedback. 6 Enhanced multi-timestep prediction for switching between open-loop and closed-loop modes with NARX and NAR neura
28、l networks It can be useful to simulate a trained neural network up the present with all the known values of a time-series in open-loop mode, then switch to closed-loop mode to continue the simulation for as many predictions into the future as are desired. It is now much easier to do this. Previousl
29、y, openloop and closeloop transformed the neural network between those two modes. net = openloop(net) net = closeloop(net) This is still the case. However, these functions now also support the transformation of input and layer delay state values between open- and closed-loop modes, making switching
30、between closed-loop to open-loop multistep prediction easier. net,xi,ai = openloop(net,xi,ai); net,xi,ai = closeloop(net,xi,ai); Here, a neural network is trained to model the magnetic levitation system in default open-loop mode. X,T = maglev_dataset; net = narxnet(1:2,1:2,10); x,xi,ai,t = preparets
31、(net,X,T); net = train(net,x,xi,ai); view(net) Then closeloop is used to convert the network to closed-loop form for simulation. netc = closeloop(net); x,xi,ai,t = preparets(netc,X,T); y = netc(x,xi,ai); 7 R2013b view(netc) Now consider the case where you might have a record of the Maglevs behavior
32、for 20 time steps, but then want to predict ahead for 20 more time steps beyond that. Define the first 20 steps of inputs and targets, representing the 20 time steps where the output is known, as defined by the targets t. Then the next 20 time steps of the input are defined, but you use the network
33、to predict the 20 outputs using each of its predictions feedback to help the network perform the next prediction. x1 = x(1:20); t1 = t(1:20); x2 = x(21:40); Then simulate the open-loop neural network on this data: x,xi,ai,t = preparets(net,x1,t1); y1,xf,af = net(x,xi,ai); Now the final input and lay
34、er states returned by the network are converted to closed-loop form along with the network. The final input states xf, and layer states af, of the open-loop network become the initial input states xi, and layer states ai, of the closed-loop network. netc,xi,ai = closeloop(net,xf,af); Typically, prep
35、arets is used to define initial input and layer states. Since these have already been obtained from the end of the open-loop simulation, you do not need preparets to continue with the 20 step predictions of the closed-loop network. 8 Cross-entropy performance measure for enhanced pattern recognition
36、 and classification accuracy y2,xf,af = netc(x2,xi,ai); Note that x2 can be set to different sequences of inputs to test different scenarios for however many time steps you would like to make predictions. For example, to predict the magnetic levitation systems behavior if 10 random inputs were used:
37、 x2 = num2cell(rand(1,10); y2,xf,af = netc(x2,xi,ai); For more information, see “Multistep Neural Network Prediction”. closeloop Convert neural network open-loop feedback to closed loop collapse all in page Syntax net = closeloop(net) net,xi,ai = closeloop(net,xi,ai) Description net = closeloop(net)
38、 takes a neural network and closes any open-loop feedback. For each feedback output i whose property net.outputsi.feedbackMode is open, it replaces its associated feedback input and their input weights with layer weight connections coming from the output. The net.outputsi.feedbackMode property is se
39、t to closed, and thenet.outputsi.feedbackInput property is set to an empty matrix. Finally, the value of net.outputsi.feedbackDelays is added to the delays of the feedback layer weights (i.e., to the delays values of the replaced input weights). net,xi,ai = closeloop(net,xi,ai) converts an open-loop
40、 network and its current input delay states xi and layer delay states ai to closed-loop form. Examples Convert NARX Network to Closed-Loop Form This example shows how to design a NARX network in open-loop form, then convert it to closed-loop form. X,T = simplenarx_dataset; net = narxnet(1:2,1:2,10);
41、 Xs,Xi,Ai,Ts = preparets(net,X,T); net = train(net,Xs,Ts,Xi,Ai); view(net) Yopen = net(Xs,Xi,Ai) net = closeloop(net) view(net) Xs,Xi,Ai,Ts = preparets(net,X,T); Ycloesed = net(Xs,Xi,Ai); Convert Delay States For examples on using closeloop and openloop to implement multistep prediction, see narxnet
42、 and narnet. See Also Create and Train Custom Neural Network Architectures Neural Network Toolbox software provides a flexible network object type that allows many kinds of networks to be created and then used with functions such as init, sim, and train. Type the following to see all the network cre
43、ation functions in the toolbox. help nnnetwork This flexibility is possible because networks have an object-oriented representation. The representation allows you to define various architectures and assign various algorithms to those architectures. To create custom networks, start with an empty netw
44、ork (obtained with the network function) and set its properties as desired. net = network The network object consists of many properties that you can set to specify the structure and behavior of your network. The following sections show how to create a custom network by using these properties. Custo
45、m Network Before you can build a network you need to know what it looks like. For dramatic purposes (and to give the toolbox a workout) this section leads you through the creation of the wild and complicated network shown below. Each of the two elements of the first network input is to accept values
46、 ranging between 0 and 10. Each of the five elements of the second network input ranges from 2 to 2. Before you can complete your design of this network, the algorithms it employs for initialization and training must be specified. Each layers weights and biases are initialized with the Nguyen-Widrow
47、 layer initialization method (initnw). The network is trained with Levenberg-Marquardt backpropagation (trainlm), so that, given example input vectors, the outputs of the third layer learn to match the associated target vectors with minimal mean squared error (mse). Network Definition The first step
48、 is to create a new network. Type the following code to create a network and view its many properties: net = network Architecture Properties The first group of properties displayed is labeled architecture properties. These properties allow you to select the number of inputs and layers and their conn
49、ections. Number of Inputs and Layers. The first two properties displayed in the dimensions group are numInputs and numLayers. These properties allow you to select how many inputs and layers you want the network to have. net = dimensions: numInputs: 0 numLayers: 0 . Note that the network has no inputs or layers at this time. Change that by setting these properties to the number of inputs and number of layers in the custom