## 4 Methodology
### Architecture  
The whole architecture is shown in Figure 5. The architecture detects anomalous sequences by using information gathered from its current under-detection and previous sequences. Figure 4: Liquid level in mold sequence (**a**) vs. liquid level differential sequence (**b**). In (**b**), each value represents the liquid level change since the previous sample. The anomaly sequence duration is significantly shorter in (**b**) and more suitable for unsupervised learning. Figure 5: An example of sliding window techniques used in the framework with \(s_{w}=8\) and \(s_{w_{y}}=4\). The first four data points in the sliding window are being used to forecast the next four. The error extraction network uses all the data in the window sequence to generate the error sequence of the last four data points. A sliding window \(\mathbf{W}\) with size \(s_{w}\) contains data from \(i\)th data to \(i+s_{w}\)th data in a time series. A sliding window \(\mathbf{W}_{p}\) contains first \(s_{w_{p}}\) data in \(\mathbf{W}\). Sliding window \(\mathbf{W}_{l}\) with size \(s_{w}\) contains last \(s_{w}\) data in \(\mathbf{W}\). \(s_{w_{p}}\), and \(s_{w_{l}}\) are defined as follows:  
\[\left\{ \begin{aligned} s_{w}&=s_{w_{p}}+s_{w_{l}}\\ 0&<s_{w_{p}}<s_{w}\\ 0&<s_{w_{l}}<s_{w}\end{aligned} \right. \tag{2}\]  
Sliding window \(\mathbf{W}\) contains a time subsequence \(T_{w}\). The forecasting network takes the first \(n\) data in \(T_{w}\) as input and forecasts the remaining time series \(T_{fw_{l}}\in\mathbb{R}^{B\times s_{w_{l}}\times D}\), where \(B\) is the dataset size, and \(D\) is the feature size. The error extraction network takes the entire \(T_{w}\) sequence as input and then outputs the error series \(T_{ew_{l}}\in\mathbb{R}^{B\times s_{w_{l}}\times D}\) and an anomaly-free time sequence \(T_{fw_{l}}\). \(T_{w_{l}}\), \(T_{fw_{l}}\), \(T_{ew_{l}}\), and a reconstructed version of the original time series \(\hat{T}_{w_{l}}\) are defined as follows:  
\[T_{w}=\left\{x_{i},x_{i+1},\ldots,x_{i+s_{w}-1}\right\} \tag{3}\]  
\[T_{fw_{l}}=\left\{x_{i+s_{w_{p}}},x_{i+s_{w_{p}}+1},\ldots,x_{i+s_{w}-1}\right\} \tag{4}\]  
\[T_{ew_{l}}=\left\{e_{i+s_{w_{p}}},e_{i+s_{w_{p}}+1},\ldots,e_{i+s_{w}-1}\right\} \tag{5}\]  
\[\hat{T}_{w_{l}}=T_{ew_{l}}+T_{fw_{l}} \tag{6}\]  
The forecasting network and error extraction network can be implemented by using RNN. The two networks can be defined as follows:  
\[F_{\Theta_{F}}\left(T_{w_{p}}\right)=T_{fw_{l}} \tag{7}\]  
\[E_{\Theta_{E}}(T_{w})=T_{ew_{l}} \tag{8}\]  
The losses \(\mathcal{L}\), \(\mathcal{L}_{e}\) and \(\mathcal{L}_{f}\) of the proposed framework are designed as follows. \(\mathcal{L}\) is defined by DIstortion Loss including shApe and TimE (DILATE) [24] and MSE loss. DILATE is a loss function design for time series data. It uses Soft Dynamic Time Warping (SoftDTW) to define shape loss and Time Distortion Index (TDI) to define temporal losses. MSE is also used to accelerate the fitting process. \[\mathcal{L}=\text{DILATE}(\hat{T}_{w_{l}},T_{w_{l}})+\frac{\left\|\left(F_{ \Theta_{F}}\left(T_{w_{p}}\right)+E_{\Theta_{E}}(T_{w})\right)-T_{w_{l}}\right\| _{2}^{2}+\left\|E_{\Theta_{E}}(T_{w})\right\|}{s_{w_{l}}} \tag{9}\]  
\[\mathcal{L}_{e}=\frac{\left\|T_{ew_{l}}-\left(T_{w_{l}}-T_{fw_{l}}\right) \right\|_{2}^{2}}{s_{w_{l}}}=\frac{\left\|T_{ew}-\left(T_{w}-F_{\Theta_{F}} \left(T_{w_{p}}\right)\right)\right\|_{2}^{2}}{s_{w_{l}}} \tag{10}\]  
\[\mathcal{L}_{f}=\frac{\left\|T_{fw_{l}}-\left(T_{w_{l}}-T_{ew_{l}}\right) \right\|_{2}^{2}}{s_{w_{l}}}=\frac{\left\|T_{fw_{l}}-\left(T_{w_{l}}-E_{ \Theta_{E}}(T_{w})\right)\right\|_{2}^{2}}{s_{w_{l}}} \tag{11}\]  
Equation (9) defines the total loss for the entire framework. This equation makes sure that generated \(T_{fw_{l}}\) and \(T_{ew_{l}}\) are decomposed from \(T_{w}\). Minimizing \(\left\|E_{\Theta_{E}}(T_{w})\right\|\) minimize the error sequence \(T_{ew_{l}}\) to make sure that the error sequence stays sparse.