# Deep Learning-Based Autoscaling Using Bidirectional Long Short-Term Memory for Kubernetes
## 4 Proactive Custom Autoscaler
### Analysis Phase:Bi-LSTM Forecasting Service  
In the analysis phase, the Bi-LSTM forecasting service period obtains the latest collected metric data with window size \(w\) through Prometheus's restful API. The trained model then uses these latest data \(d_{t-w},...,d_{t-2},d_{t-1},d_{t}\) to predict the next data in sequence \(d_{t+1}\). In our implementation, the Bi-LSTM neural network model was used to predict workload of HTTP requests in the future. Thus, we will discuss the Bi-LSTM neural network model in more detail.  
First, we discuss the standard (known as Forward or Uni-) Long Short-Term Memory (LSTM). LSTM is a member of the deep RNN family, which is used in many fields, including sentiment analysis, speech recognition, and time-series analysis. It is a modified version of the RNN architecture, and was first introduced by [PERSON] and [PERSON] [34] in 1996 and popularized by many studies thereafter. Unlike the feed-forward neural network, RNNs take the current and previous time-step output data as input to build the network [35]. There were networks with loops in them, which allow the history data to be maintained. However, traditional RNNs incur long-term dependencies due to the gradient vanishing problem. In RNNs, the gradient expands exponentially over time [36]. To address this problem, LSTM, which is a special type of RNN, has been proposed [37]. However, LSTM can only handle information in one direction, which is a limitation because it ignores the continuous data changes. Therefore, LSTM can only capture partial information.  
The regular RNN or LSTM traverses input sequence with forward direction of the original time series. The Bi-LSTM [20] are an extension of the LSTM models with two LSTMs applied to the input data. The first LSTM is trained on the input sequence with its original direction (forward). The second LSTM is fed with the reverse form of the input sequence (backward). This architecture of Bi-LSTM is two LSTM networks stacked on top of each other. It bidirectionally runs the input from two directions, the first one from past to future, the second from future to past. The difference from forward LSTM is that running backward helps preserve information from the future. By combining two hidden states, one can preserve information from the past and the future. Applying the LSTM twice leads to improved learning long-term dependencies and thus consequently will improve the accuracy of the model [38].  
The Bi-LSTM processes data in both directions with two separate hidden LSTM layers to get more data. The Bi-LSTM equations are expressed as (1)-(3) below:  
\[h_{t}^{F}=f(U_{F}m_{t}+W_{F}h_{t-1}^{F}+b_{F}) \tag{1}\]  
\[h_{t}^{B}=f(U_{B}m_{t}+W_{B}h_{t+1}^{B}+b_{B}) \tag{2}\]  
\[\hat{y}_{t}=V_{F}h_{t}^{F}+V_{B}h_{B}^{B}+b_{O} \tag{3}\]  
where \(U_{F}\) and \(U_{B}\), \(W_{F}\) and \(W_{B}\), and \(V_{F}\) and \(V_{B}\) are different weight matrices. \(f\) denotes the activation functions, for example, Tanh sigmoid or logistic sigmoid; and \(b_{F}\), \(b_{B}\), and \(b_{O}\) are the forward, backward, and output layer biases, respectively.  
Given an input sequence \(m=(m_{1},...,m_{T})\), a Bi-LSTM computes the forward hidden sequence, \(h^{F}\), the backward hidden sequence, \(h^{B}\), and the output sequence, \(\hat{y}\), by iterating the forward layer from \(t=1\) to \(T\) and the backward layer from \(t=T\) to \(1\) and then updating the output layer.  
For the t-th time step, both parallel hidden layers in both forward and backward directions process \(m_{t}\) as input. The hidden states \(h_{t}^{F}\) and \(h_{t}^{B}\) are the output results of two hidden layers in both forward and backward directions at time step \(t\), respectively. Then the output value \(\hat{y}_{t}\) depends on the sum of the hidden states \(h_{t}^{F}\) and \(h_{t}^{B}\), as described in Equation (3).