Dynamic neural networks are good at time-series prediction. To see examples of using NARX networks being applied in open-loop form, closed-loop form and open/closed-loop multistep prediction see Multistep Neural Network Prediction.

For deep learning with time series data, see instead Sequence Classification Using Deep Learning.

Suppose, for instance, that you have data from a pH neutralization process. You want to design a network that can predict the pH of a solution in a tank from past values of the pH and past values of the acid and base flow rate into the tank. You have a total of 2001 time steps for which you have those series.

You can solve this problem in two ways:

Use a graphical user interface,

`ntstool`

, as described in Using the Neural Network Time Series App.Use command-line functions, as described in Using Command-Line Functions.

It is generally best to start with the GUI, and then to use the GUI to automatically generate command-line scripts. Before using either method, the first step is to define the problem by selecting a data set. Each GUI has access to many sample data sets that you can use to experiment with the toolbox. If you have a specific problem that you want to solve, you can load your own data into the workspace. The next section describes the data format.

To define a time series problem for the toolbox, arrange a set of TS input vectors as columns in a cell array. Then, arrange another set of TS target vectors (the correct output vectors for each of the input vectors) into a second cell array (see “Data Structures” for a detailed description of data formatting for static and time series data). However, there are cases in which you only need to have a target data set. For example, you can define the following time series problem, in which you want to use previous values of a series to predict the next value:

targets = {1 2 3 4 5};

The next section shows how to train a network to fit a time series data set, using the
neural network time series app, `ntstool`

. This example uses the pH neutralization
data set provided with the toolbox.

If needed, open the Neural Network Start GUI with this command:

nnstart

Click

**Time Series App**to open the Neural Network Time Series App. (You can also use the command`ntstool`

.)Notice that this opening pane is different than the opening panes for the other GUIs. This is because

`ntstool`

can be used to solve three different kinds of time series problems.In the first type of time series problem, you would like to predict future values of a time series

*y*(*t*) from past values of that time series and past values of a second time series*x*(*t*). This form of prediction is called nonlinear autoregressive with exogenous (external) input, or NARX (see “NARX Network” (narxnet, closeloop)), and can be written as follows:*y*(*t*) =*f*(*y*(*t*– 1), ...,*y*(*t*–*d*),*x*(*t*– 1), ..., (*t*–*d*))This model could be used to predict future values of a stock or bond, based on such economic variables as unemployment rates, GDP, etc. It could also be used for system identification, in which models are developed to represent dynamic systems, such as chemical processes, manufacturing systems, robotics, aerospace vehicles, etc.

In the second type of time series problem, there is only one series involved. The future values of a time series

*y*(*t*) are predicted only from past values of that series. This form of prediction is called nonlinear autoregressive, or NAR, and can be written as follows:*y*(*t*) =*f*(*y*(*t*– 1), ...,*y*(*t*–*d*))This model could also be used to predict financial instruments, but without the use of a companion series.

The third time series problem is similar to the first type, in that two series are involved, an input series

*x*(*t*) and an output/target series*y*(*t*). Here you want to predict values of*y*(*t*) from previous values of*x*(*t*), but without knowledge of previous values of*y*(*t*). This input/output model can be written as follows:*y*(*t*) =*f*(*x*(*t*– 1), ...,*x*(*t*–*d*))The NARX model will provide better predictions than this input-output model, because it uses the additional information contained in the previous values of

*y*(*t*). However, there may be some applications in which the previous values of*y*(*t*) would not be available. Those are the only cases where you would want to use the input-output model instead of the NARX model.

For this example, select the NARX model and click

**Next**to proceed.Click

**Load Example Data Set**in the Select Data window. The Time Series Data Set Chooser window opens.### Note

Use the

**Inputs**and**Targets**options in the Select Data window when you need to load data from the MATLAB^{®}workspace.Select

**pH Neutralization Process**, and click**Import**. This returns you to the Select Data window.Click

**Next**to open the Validation and Test Data window, shown in the following figure.The validation and test data sets are each set to 15% of the original data.

With these settings, the input vectors and target vectors will be randomly divided into three sets as follows:

70% will be used for training.

15% will be used to validate that the network is generalizing and to stop training before overfitting.

The last 15% will be used as a completely independent test of network generalization.

(See “Dividing the Data” for more discussion of the data division process.)

Click

**Next**.The standard NARX network is a two-layer feedforward network, with a sigmoid transfer function in the hidden layer and a linear transfer function in the output layer. This network also uses tapped delay lines to store previous values of the

*x*(*t*) and*y*(*t*) sequences. Note that the output of the NARX network,*y*(*t*), is fed back to the input of the network (through delays), since*y*(*t*) is a function of*y*(*t*– 1),*y*(*t*– 2), ...,*y*(*t*– d). However, for efficient training this feedback loop can be opened.Because the true output is available during the training of the network, you can use the open-loop architecture shown above, in which the true output is used instead of feeding back the estimated output. This has two advantages. The first is that the input to the feedforward network is more accurate. The second is that the resulting network has a purely feedforward architecture, and therefore a more efficient algorithm can be used for training. This network is discussed in more detail in “NARX Network” (narxnet, closeloop).

The default number of hidden neurons is set to 10. The default number of delays is 2. Change this value to 4. You might want to adjust these numbers if the network training performance is poor.

Click

**Next**.Select a training algorithm, then click

**Train**. Levenberg-Marquardt (`trainlm`

) is recommended for most problems, but for some noisy and small problems Bayesian Regularization (`trainbr`

) can take longer but obtain a better solution. For large problems, however, Scaled Conjugate Gradient (`trainscg`

) is recommended as it uses gradient calculations which are more memory efficient than the Jacobian calculations the other two algorithms use. This example uses the default Levenberg-Marquardt.The training continued until the validation error failed to decrease for six iterations (validation stop).

Under

**Plots**, click**Error Autocorrelation**. This is used to validate the network performance.The following plot displays the error autocorrelation function. It describes how the prediction errors are related in time. For a perfect prediction model, there should only be one nonzero value of the autocorrelation function, and it should occur at zero lag. (This is the mean square error.) This would mean that the prediction errors were completely uncorrelated with each other (white noise). If there was significant correlation in the prediction errors, then it should be possible to improve the prediction - perhaps by increasing the number of delays in the tapped delay lines. In this case, the correlations, except for the one at zero lag, fall approximately within the 95% confidence limits around zero, so the model seems to be adequate. If even more accurate results were required, you could retrain the network by clicking

**Retrain**in`ntstool`

. This will change the initial weights and biases of the network, and may produce an improved network after retraining.View the input-error cross-correlation function to obtain additional verification of network performance. Under the

**Plots**pane, click**Input-Error Cross-correlation**.This input-error cross-correlation function illustrates how the errors are correlated with the input sequence

*x*(*t*). For a perfect prediction model, all of the correlations should be zero. If the input is correlated with the error, then it should be possible to improve the prediction, perhaps by increasing the number of delays in the tapped delay lines. In this case, all of the correlations fall within the confidence bounds around zero.Under

**Plots**, click**Time Series Response**. This displays the inputs, targets and errors versus time. It also indicates which time points were selected for training, testing and validation.Click

**Next**in the Neural Network Time Series App to evaluate the network.At this point, you can test the network against new data.

If you are dissatisfied with the network's performance on the original or new data, you can do any of the following:

Train it again.

Increase the number of neurons and/or the number of delays.

Get a larger training data set.

If the performance on the training set is good, but the test set performance is significantly worse, which could indicate overfitting, then reducing the number of neurons can improve your results.

If you are satisfied with the network performance, click

**Next**.Use this panel to generate a MATLAB function or Simulink

^{®}diagram for simulating your neural network. You can use the generated code or diagram to better understand how your neural network computes outputs from inputs, or deploy the network with MATLAB Compiler™ tools and other MATLAB and Simulink code generation tools.Use the buttons on this screen to generate scripts or to save your results.

You can click

**Simple Script**or**Advanced Script**to create MATLAB code that can be used to reproduce all of the previous steps from the command line. Creating MATLAB code can be helpful if you want to learn how to use the command-line functionality of the toolbox to customize the training process. In Using Command-Line Functions, you will investigate the generated scripts in more detail.You can also have the network saved as

`net`

in the workspace. You can perform additional tests on it or put it to work on new inputs.

After creating MATLAB code and saving your results, click

**Finish**.

The easiest way to learn how to use the command-line functionality of the toolbox is to generate scripts from the GUIs, and then modify them to customize the network training. As an example, look at the simple script that was created at step 15 of the previous section.

% Solve an Autoregression Problem with External % Input with a NARX Neural Network % Script generated by NTSTOOL % % This script assumes the variables on the right of % these equalities are defined: % % phInputs - input time series. % phTargets - feedback time series. inputSeries = phInputs; targetSeries = phTargets; % Create a Nonlinear Autoregressive Network with External Input inputDelays = 1:4; feedbackDelays = 1:4; hiddenLayerSize = 10; net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize); % Prepare the Data for Training and Simulation % The function PREPARETS prepares time series 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,inputSeries,{},targetSeries); % Set up Division of Data for Training, Validation, Testing net.divideParam.trainRatio = 70/100; net.divideParam.valRatio = 15/100; net.divideParam.testRatio = 15/100; % 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) % View the Network view(net) % Plots % Uncomment these lines to enable various plots. % figure, plotperform(tr) % figure, plottrainstate(tr) % figure, plotregression(targets,outputs) % 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 output layer. netc = closeloop(net); netc.name = [net.name ' - Closed Loop']; view(netc) [xc,xic,aic,tc] = preparets(netc,inputSeries,{},targetSeries); yc = netc(xc,xic,aic); closedLoopPerformance = perform(netc,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); nets.name = [net.name ' - Predict One Step Ahead']; view(nets) [xs,xis,ais,ts] = preparets(nets,inputSeries,{},targetSeries); ys = nets(xs,xis,ais); earlyPredictPerformance = perform(nets,ts,ys)

You can save the script, and then run it from the command line to reproduce the results of the previous GUI session. You can also edit the script to customize the training process. In this case, follow each of the steps in the script.

The script assumes that the input vectors and target vectors are already loaded into the workspace. If the data are not loaded, you can load them as follows:

`load ph_dataset inputSeries = phInputs; targetSeries = phTargets;`

Create a network. The NARX network,

`narxnet`

, is a feedforward network with the default tan-sigmoid transfer function in the hidden layer and linear transfer function in the output layer. This network has two inputs. One is an external input, and the other is a feedback connection from the network output. (After the network has been trained, this feedback connection can be closed, as you will see at a later step.) For each of these inputs, there is a tapped delay line to store previous values. To assign the network architecture for a NARX network, you must select the delays associated with each tapped delay line, and also the number of hidden layer neurons. In the following steps, you assign the input delays and the feedback delays to range from 1 to 4 and the number of hidden neurons to be 10.inputDelays = 1:4; feedbackDelays = 1:4; hiddenLayerSize = 10; net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize);

### Note

Increasing the number of neurons and the number of delays requires more computation, and this has a tendency to overfit the data when the numbers are set too high, but it allows the network to solve more complicated problems. More layers require more computation, but their use might result in the network solving complex problems more efficiently. To use more than one hidden layer, enter the hidden layer sizes as elements of an array in the

`fitnet`

command.Prepare the data for training. When training a network containing tapped delay lines, it is necessary to fill the delays with initial values of the inputs and outputs of the network. There is a toolbox command that facilitates this process -

`preparets`

. This function has three input arguments: the network, the input sequence and the target sequence. The function returns the initial conditions that are needed to fill the tapped delay lines in the network, and modified input and target sequences, where the initial conditions have been removed. You can call the function as follows:`[inputs,inputStates,layerStates,targets] = ... preparets(net,inputSeries,{},targetSeries);`

Set up the division of data.

net.divideParam.trainRatio = 70/100; net.divideParam.valRatio = 15/100; net.divideParam.testRatio = 15/100;

With these settings, the input vectors and target vectors will be randomly divided, with 70% used for training, 15% for validation and 15% for testing.

Train the network. The network uses the default Levenberg-Marquardt algorithm (

`trainlm`

) for training. For problems in which Levenberg-Marquardt does not produce as accurate results as desired, or for large data problems, consider setting the network training function to Bayesian Regularization (`trainbr`

) or Scaled Conjugate Gradient (`trainscg`

), respectively, with eithernet.trainFcn = 'trainbr'; net.trainFcn = 'trainscg';

To train the network, enter:

[net,tr] = train(net,inputs,targets,inputStates,layerStates);

During training, the following training window opens. This window displays training progress and allows you to interrupt training at any point by clicking

**Stop Training**.This training stopped when the validation error increased for six iterations, which occurred at iteration 44.

Test the network. After the network has been trained, you can use it to compute the network outputs. The following code calculates the network outputs, errors and overall performance. Note that to simulate a network with tapped delay lines, you need to assign the initial values for these delayed signals. This is done with

`inputStates`

and`layerStates`

provided by`preparets`

at an earlier stage.outputs = net(inputs,inputStates,layerStates); errors = gsubtract(targets,outputs); performance = perform(net,targets,outputs)

performance = 0.0042

View the network diagram.

view(net)

Plot the performance training record to check for potential overfitting.

figure, plotperform(tr)

This figure shows that training and validation errors decrease until the highlighted epoch. It does not appear that any overfitting has occurred, because the validation error does not increase before this epoch.

All of the training is done in open loop (also called series-parallel architecture), including the validation and testing steps. The typical workflow is to fully create the network in open loop, and only when it has been trained (which includes validation and testing steps) is it transformed to closed loop for multistep-ahead prediction. Likewise, the

`R`

values in the GUI are computed based on the open-loop training results.Close the loop on the NARX network. When the feedback loop is open on the NARX network, it is performing a one-step-ahead prediction. It is predicting the next value of

*y*(*t*) from previous values of*y*(*t*) and*x*(*t*). With the feedback loop closed, it can be used to perform multi-step-ahead predictions. This is because predictions of*y*(*t*) will be used in place of actual future values of*y*(*t*). The following commands can be used to close the loop and calculate closed-loop performance`netc = closeloop(net); netc.name = [net.name ' - Closed Loop']; view(netc) [xc,xic,aic,tc] = preparets(netc,inputSeries,{},targetSeries); yc = netc(xc,xic,aic); perfc = perform(netc,tc,yc)`

perfc = 2.8744

Remove a delay from the network, to get the prediction one time step early.

`nets = removedelay(net); nets.name = [net.name ' - Predict One Step Ahead']; view(nets) [xs,xis,ais,ts] = preparets(nets,inputSeries,{},targetSeries); ys = nets(xs,xis,ais); earlyPredictPerformance = perform(nets,ts,ys)`

earlyPredictPerformance = 0.0042

From this figure, you can see that the network is identical to the previous open-loop network, except that one delay has been removed from each of the tapped delay lines. The output of the network is then

*y*(*t*+ 1) instead of*y*(*t*). This may sometimes be helpful when a network is deployed for certain applications.

If the network performance is not satisfactory, you could try any of these approaches:

Reset the initial network weights and biases to new values with

`init`

and train again (see “Initializing Weights” (init)).Increase the number of hidden neurons or the number of delays.

Increase the number of training vectors.

Increase the number of input values, if more relevant information is available.

Try a different training algorithm (see “Training Algorithms”).

To get more experience in command-line operations, try some of these tasks:

During training, open a plot window (such as the error correlation plot), and watch it animate.

Plot from the command line with functions such as

`plotresponse`

,`ploterrcorr`

and`plotperform`

. (For more information on using these functions, see their reference pages.)

Also, see the advanced script for more options, when training from the command line.

Each time a neural network is trained, can result in a different solution due to different initial weight and bias values and different divisions of data into training, validation, and test sets. As a result, different neural networks trained on the same problem can give different outputs for the same input. To ensure that a neural network of good accuracy has been found, retrain several times.

There are several other techniques for improving upon initial solutions if higher accuracy is desired. For more information, see Improve Shallow Neural Network Generalization and Avoid Overfitting.