# Machine Learning Algorithm for Delay Prediction in IoT and Tactile Internet
## 4 Problem Formulation and System Model
### ANN Training  
We used NARX-RNN to predict the IoT delay. For training NARX-RNN, we loaded the training data and used a tppd with two input and output delays. This work assumed that the output and input delays are the same, where \(d_{y}=d_{u}=d\); also, there are two inputs to the NARX network, \(u(n)\) and \(y(n)\). We created a NARX network using the narxnet functionality. The network consists of three layers, with a hidden layer containing 20 hidden neurons. For training the network, we proposed four different training algorithms, Trainlm, Traincf, Trainbr, and Trainrp, for the training tasks and then the preparets function for preparing the data. Finally, we performed the IoT delay prediction using the k-step-ahead prediction approach. The NARX network is trained in two methods: using an open-loop training architecture and a closed-loop architecture. Closed-loop networks produce a multi-step prediction. In other words, they continue to predict when inner returns lose outer returns (responses). The multi-step-ahead forecasting method is often helpful for simulating a network in an open-loop form where the output data are known and then transitioning to a closed-loop form, where the output is returned to the network input via the NARX network to implement multistage prediction, even though the feedback was just provided. First, all stages, except the k-time stages of the input sequence and the desired output sequence, are used to model the network in the open-loop architecture, as shown in Figure 4, to take advantage of the high precision provided by introducing the desired output sequence. Then, the network and its final stage are transferred to the closed-loop architecture, as shown in Figure 5, to make k-step predictions with only the k inputs. The output is also an input returned with a unit delay to the network's information within the usual NARX structure. Here, the network is simulated as a closed loop only. The ANN performs multiple predictions for the outer input sequence and the initial terms, as the input sequences have periods. Figure 3: Service model for the collected traffic. Note that the \"y\" sequence is a response signal that is also output (desired output). After closing the response path, the corresponding output is delivered to the corresponding input. The one-step prediction for multiple instances helps to obtain the fast time-step prediction. The prediction of the observed data is in the next time step since only a one-time step can be predicted. The one-step \((n+1)\) prediction is satisfied by crossing the actual and preceding information \((n,n-1\ldots,n-k)\) and obtaining the expected output \(y(n+1)\) (Algoritm 1). ```
1 Load the IoT data sets. 2 InputSeries: Time series training data \(u=\{u_{1},u_{2}\ldots,u_{n}\}\)
3 TargetSeries: Time series training data \(y=\{y_{1},y_{2}\ldots y_{n}\}\)
4 Outputs: the predicted output \(\hat{y}=\{\hat{y}_{1},\hat{y}_{2},\ldots\hat{y}_{n}\}\)
5 Error (e) \(\leftarrow(y-g)\)
6 Initialize the weights and biases randomly and threshold
7 Divide the dataset into 70% training, 15% validation, and 15% test sets
8 Normalize the dataset into values between [\(-1\),\(1\)]
9 Specify the predictors (u) and target (y)
10\(1000\leftarrow\) number of epochs
11\(32\leftarrow\) batch size
12 Define NARX neural network model architecture
13 Choose the loss function () \(\rightarrow\) MSE
14 Choose trainingfcn () \(\rightarrow\) Trainbr, Traincgf, Trainlm, Trainrp.