# Deep Learning for Anomaly Detection in CNC Machine Vibration Data: A RoughLSTM-Based Approach
## 2 Materials and Methods
### Long Short-Term Memory  
LSTM is a type of artificial neural network architecture specifically designed for time series data and sequential data modeling [55]. LSTM addresses one of the fundamental problems faced by traditional recurrent neural networks (RNNs), known as the \"vanishing gradient\" problem, and provides the ability to store and retrieve information over long time intervals. This capability of LSTM to store, process, and recall information over extended periods makes it highly effective in modeling complex relationships in sequential data.  
The LSTM architecture differs from that of traditional RNNs by incorporating specialized memory cells and gate mechanisms that control access to these cells. Through the input gate, forget gate, and output gate, the model dynamically determines when to take in, store, and output information [56]. These mechanisms also enable the model to filter out unnecessary information while learning long-term dependencies. In LSTM networks, memory management is facilitated by a structure called the cell state, which is effectively controlled through the gates.  
The architecture of an LSTM layer with T time steps and the structure of an LSTM cell is illustrated in Figure 1, where the input time series data are denoted as \(v=v_{1},v_{2},\ldots,v_{t},\ldots,v_{T}\), and the output values are represented as \(p=p_{1},p_{2},\ldots,p_{t},\ldots,p_{T}\).  
In Figure 1, \(h_{t}\) represents the output, also referred to as the hidden state, while \(c_{t}\) denotes the cell state at time step \(t\). A standard LSTM cell consists of sigmoid (\(\sigma\)) and hyperbolic tangent activation functions (tanh), along with pointwise addition (\(+\)) and multiplication (\(\bigcirc\)) operations. By updating the gates on the memory cell \(c_{t}\)--namely, the input gate (\(i_{t}\)), forget gate (\(f_{t}\)), and output gate (\(p_{t}\))--the LSTM can predict the target variables \(p=p_{1},p_{2},\ldots,p_{t},\ldots,p_{T}\). In an LSTM layer, the learnable weights are divided into three groups: input weights (\(W\)), recurrent weights (\(R\)), and bias (\(b\)). These matrices represent the concatenated input weights, recurrent weights, and bias values for each component. The layer combines these matrices according to the following equations.  
\[W=\begin{bmatrix}W_{i}\\ W_{f}\\ W_{p}\\ W_{c}\end{bmatrix}R=\begin{bmatrix}R_{i}\\ R_{f}\\ R_{p}\\ R_{c}\end{bmatrix}b=\begin{bmatrix}b_{i}\\ b_{f}\\ b_{p}\\ b_{c}\end{bmatrix}\]  
In this context, the mathematical formulation of the LSTM is described below:  
\[i_{t}=\sigma\Big{(}W_{i}\bigodot v_{t}+R_{i}\bigodot h_{t-1}+b_{i}\Big{)} \tag{3}\]  
\[f_{t}=\sigma\Big{(}W_{f}\bigodot v_{t}+R_{f}\bigodot h_{t-1}+b_{f}\Big{)} \tag{4}\]\[p_{t} =\sigma\Big{(}W_{p}\bigodot v_{t}+R_{p}\bigodot h_{t-1}+b_{p}\Big{)} \tag{5}\] \[c_{t} =f_{t}\bigodot c_{t-1}+i_{t}\bigodot\widetilde{c}_{t}\] (6) \[\widetilde{c}_{t} =\sigma\Big{(}W_{c}\bigodot v_{t}+R_{c}\bigodot h_{t-1}+b_{c}\Big{)}\] (7) \[h_{t} =v_{t}\bigodot\sigma(c_{t}) \tag{8}\]  
where \(c_{t-1}\) and \(h_{t-1}\) are, respectively, the previous cell state and its output vector, and \(h_{t}\) is the output vector.