# CL-TAD: A Contrastive-Learning-Based Method for Time Series Anomaly Detection
## 3 The Proposed Method for Time Series Anomaly Detection: CL-TAD
### The Architecture for Representation Learning of CL-TAD  
We propose a novel contrastive-learning-based anomaly detection method for time series data, which first augments the training data with an encoder-decoder-based reconstruction module using randomly masked samples, and then trains a contrastive-learning  
Figure 4: A TCN architecture for feature extraction of time series data: (**a**) shows hierarchical 1D convolutions, which are used to extract long-dependency features from the data. (**b**) displays the building block of a TCN architecture, which is comprised of residual, causal dilated convolutional blocks.  
Figure 5: Generation of a set of subsequence samples \(W\) from a time series data \(X\): the subsequence samples are generated by copying the overlapped segments while sliding a window of length \(L\) with the step size \(1\) over the given time series data \(X\).  
based representation learning module using those samples and original data. For anomaly detection, the anomaly scores are evaluated by calculating the differences between the representations of the original data with those of their corresponding reconstructed data. We refer to this proposed method as CL-TAD (Contrastive-Learning-based Time series Anomaly Detection).  
CL-TAD is conducted in two sequential stages: the representation learning stage and the anomaly scoring stage. In the representation learning stage, latent representations for fixed-length subsequences of time series and their augmented subsequences are generated by a contrastive-learning-based representation learning module. In the anomaly scoring phase, the anomaly score for each time step is calculated by comparing the representations of two subsequences: The first is the original subsequence that ends at the time step, and the second is the subsequence that is reconstructed from the first subsequence of which the last time step is masked. Figure 6 illustrates the architecture used for representation learning in CL-TAD, which consists of two main components: reconstruction-based positive sample generation and contrastive-learning-based representation learning.  
The first component plays the role of generating positive samples by leveraging a reconstruction-based encoder-decoder network, which recovers masked samples of the original time series data. Its reconstruction-based learning approach with a masking strategy facilitates the generalization of a robust and large positive training dataset. The representation learning component then uses both the created positive samples and the original samples for training. The whole network for sample generation and representation generation is trained in an end-to-end manner.  
Some contrastive-learning-based methods [44; 45] employ pre-defined transformation functions to augment original time series data. Conversely, a method discussed in [46] adopts a learnable transformation function for the same purpose. In our approach, we integrate a learnable transformation module that pre-processes the augmented data prior to feeding them into the contrastive learning module.