Abstract
Dynamic Time Wrapping (DTW) is a widely used algorithm for measuring similarities between two time series. It is especially valuable in a wide variety of applications, such as clustering, anomaly detection, classification, or video segmentation, where the time-series have different timescales, are irregularly sampled, or are shifted. However, it is not prone to be considered as a loss function in an end-to-end learning framework because of its non-differentiability and its quadratic temporal complexity. While differentiable variants of DTW have been introduced by the community, they still present some drawbacks: computing the distance is still expensive and this similarity tends to blur some differences in the time-series. In this paper, we propose a fast and differentiable approximation of DTW by comparing two architectures: the first one for learning an embedding in which the Euclidean distance mimics the DTW, and the second one for directly predicting the DTW output using regression. We build the former by training a siamese neural network to regress the DTW value between two time-series. Depending on the nature of the activation function, this approximation naturally supports differentiation, and it is efficient to compute. We show, in a time-series retrieval context on EEG datasets, that our methods achieve at least the same level of accuracy as other DTW main approximations with higher computational efficiency. We also show that it can be used to learn in an end-to-end setting on long time series by proposing generative models of EEGs.
Introduction
Dynamic Time Warping (DTW) is the gold standard for measuring similarity between two time series, particularly biological signals like EEG or ECG. Unlike simple Euclidean distance, DTW handles signals that are shifted in time or sampled at different speeds by finding an optimal alignment. However, DTW presents significant challenges for modern deep learning due to its quadratic temporal complexity ($O(N^2)$) and non-differentiability, which prevents its use as a loss function in end-to-end training. While differentiable variants like SoftDTW exist, they remain computationally expensive. This research proposes a fast, differentiable approximation using Convolutional Neural Networks (CNNs).
Proposed Methodology
We propose and compare two neural architectures to learn the DTW function:
- Siamese Encoder-Decoder: This architecture uses a shared 1D-CNN (SorsNet) to project two signals into a lower-dimensional embedding space. The model is trained to ensure the Euclidean distance between these embeddings ($z$ and $z'$) approximates the DTW distance of the original signals. A decoder is included to reconstruct the original signals from the embeddings, serving as a regularizer to preserve essential signal features.
- Direct Regression: This approach concatenates two signals and feeds them through a CNN that directly predicts the DTW distance as a single scalar value.
Both models are trained using Mean Squared Error (MSE) to minimize the difference between the network's prediction and the ground truth DTW value.
Experimental Evaluation
The models were evaluated on sleep-related EEG data from the SleepEDF-78 and SHHS datasets. Key results include:
- Computational Efficiency: As shown in Figure 2, the proposed CNN approximations offer significant speed advantages. For signals with 3,000 samples, the Direct Regression model achieved a 100x speedup over SoftDTW.
- Accuracy and Generalization: In nearest-neighbor retrieval tasks, the CNN models outperformed approximations like FastDTW and SoftDTW when the number of signals was small. Furthermore, models trained on SleepEDF generalized well to the SHHS dataset without fine-tuning.
- Differentiability: The differentiable nature of the CNN approximation allowed for the successful learning of sleep-stage prototypes, achieving higher classification accuracy than FastDTW-based gradients.
Conclusion
By approximating DTW with a CNN, we transform a slow, non-differentiable algorithm into a fast, GPU-accelerated loss function suitable for end-to-end learning. Future work will explore extending this approach to multivariate time series and assessing its performance across diverse domains beyond EEG data.
Summary updated