# Load-Balanced Dynamic SFC Migration Based on Resource Demand Prediction
## 4 Algorithm Design
### LSTM-Based Resource Demand Prediction Algorithm  
Traffic variations in network environments are typically highly real-time and dynamic. Short-term forecasting can promptly reflect and quickly capture these changes. Deep learning models possess rapid learning and adaptation capabilities, enabling them to accurately complete prediction tasks and capture real-time traffic fluctuations within a short time frame. Therefore, we have adopted an integrated algorithm that combines Convolutional Neural Networks, Long Short-Term Memory networks, and attention mechanisms to accurately predict the resource demands of VNF [14].  
First, the CNN module is used to extract key features from the historical data of VNF resource usage. CNNs excel at identifying local patterns and spatiotemporal features in data, and are able to generate high-quality feature representations. The convolutional layer scans the VNF resource usage time series data with multiple convolutional kernels. This captures different types of local patterns, such as peaks and regular changes in resource usage. As a result, it generates a rich set of feature maps that clearly show the underlying structure of VNF resource usage.  
Next, the features extracted by the CNN are passed to the LSTM network. LSTM has powerful sequence processing capabilities, which can capture the time dependencies and long-term patterns in the use of VNF resources. This helps the model learn the connections between different time steps. LSTM processes the feature sequences and captures the dynamic information of resource demands change over time by maintaining hidden states and memory cells. LSTM can identify trend changes and cyclical fluctuations in resource use.  
To further improve the model's prediction capability, we integrate an attention mechanism into the LSTM network. The attention mechanism can assign different weights to each time step. This highlights the most important historical information for the current prediction. Figure 3 shows a flowchart of how the attention mechanism is integrated with the LSTM network. The workflow of the attention mechanism is as follows:  
At each time step \(t\), the attention mechanism calculates the attention weights based on the hidden state of the current LSTM \(h_{t}\) and the hidden state of all previous time steps \(\alpha_{t,i}\), where \(i\) takes all previous time steps. Attention weights are calculated by a scoring function that measures the relevance of each hidden state \(h_{i}\) to the current prediction task \(t\):  
\[e_{t,i}=score(h_{t},h_{i})=h_{t}^{\mathrm{T}}W_{a}h_{i} \tag{25}\]  
\[\alpha_{t,i}=\frac{\exp(e_{t,i})}{\sum_{k=1}^{T}\exp(e_{t,k})} \tag{26}\]  
where \(W_{a}\) is a learnable weight matrix, and the softmax function ensures that the attention weights \(\alpha_{t,i}\) are 1 on all \(i\).  
Figure 2: Overall framework of the RP-LBM.  
Then, the attention weight \(\alpha_{t,i}\) is used to perform a weighted sum of the hidden states to generate a context vector \(c_{t}\):  
Finally, the context vector \(c_{t}\) is \(h_{t}\) connected to the current hidden state, and the final resource demand forecast \(\hat{y}_{t}\) is generated through the fully connected layer:  
\[\hat{y}_{t}=softmax(W_{c}[c_{t};h_{t}]+b_{c}) \tag{27}\]  
where \(W_{c}\) and \(b_{c}\) are the weight matrix and bias term of the fully connected layer, respectively.  
Through this hybrid model combining CNN, LSTM, and attention mechanisms, the paper achieves precise predictions of future VNF resource demands, providing a reliable basis for SFC migration.