---
title: 'Drop-DTW: Robust Sequence Alignment'
url: https://www.emergentmind.com/topics/drop-dtw
type: topic
---

# Drop-DTW: Robust Sequence Alignment

Drop-DTW is an algorithm for sequence-to-sequence alignment that extends Dynamic Time Warping (DTW) to robustly handle signals containing outliers. Unlike standard DTW, which assumes all elements in two temporal sequences correspond to each other (with possible shifts and temporal distortions), Drop-DTW aligns only the shared "inlier" content and automatically drops unmatched (outlier) elements at a fixed penalty. The method is formalized as a dynamic program, is efficient and easily differentiable, and has demonstrated state-of-the-art performance as a sequence similarity measure and a training loss across various vision and multimodal retrieval tasks [2108.11996].

## 1. Problem Setting and Motivation

Let $X = \{x_1, ..., x_N\}$ and $Y = \{y_1, ..., y_K\}$ be two sequences, such as feature vectors extracted from video or audio, with lengths $N$ and $K$ respectively. In many applications, these sequences are contaminated by outliers—elements not corresponding to any inlier in the other sequence. The principal goal is to:
- Discover a one-to-many monotonic matching between the inlier portions of $X$ and $Y$, as in standard DTW (which is robust to temporal shifts/dilations but not to arbitrary outliers).
- Allow for any subset of elements in $X \cup Y$ to be "dropped" (removed from matching) subject to a fixed penalty, thereby aligning only the shared signal and excluding outliers from the computation.

This setting generalizes classical DTW by relaxing the requirement that every element be aligned, improving robustness in noisy retrieval, localization, and representation learning scenarios.

## 2. Mathematical Objective

Drop-DTW defines a binary correspondence matrix $M \in \{0,1\}^{K \times N}$ where $M_{i,j} = 1$ iff $y_i$ is matched to $x_j$. A penalty is assigned for each unmatched element (i.e., zero row or column in $M$). Using $C_{i,j}$ as the pairwise cost (e.g., $1 - \cos(y_i, x_j)$) and $d^Y_i$, $d^X_j$ as drop penalties for $y_i$ and $x_j$:
\[
\min_{M \in \overline{M}} \sum_{i,j} M_{i,j} C_{i,j}
\quad + \sum_{i: \sum_j M_{i,j} = 0} d^Y_i
\quad + \sum_{j: \sum_i M_{i,j} = 0} d^X_j
\]
where $\overline{M}$ is the set of monotonic (no backward matches) but not fully dense matrices.

In shorthand, if $P_Y(M) \in \{0,1\}^K$ and $P_X(M) \in \{0,1\}^N$ indicate which rows/columns are entirely zero,
\[
\text{Drop-DTW}(X, Y) = \min_{M \in \overline{M}} \langle M, C \rangle + P_Y(M) \cdot d^Y + P_X(M) \cdot d^X
\]
This formulation allows alignment with selective skipping of outliers, controlled by tunable drop penalties.

## 3. Dynamic Programming Recurrence

The Drop-DTW recursion constructs a cost table $D_{i,j}$ for optimal alignment of prefixes $Y[1..i]$ and $X[1..j]$.

### Drop-X variant (dropping only in $X$; $d^Y_i = \infty$):

Auxiliary tables:
- $D^+_{i,j}$: cost if $y_i$ matched to $x_j$
- $D^-_{i,j}$: cost if $x_j$ is dropped

Recurrence:
1. $D^+_{i,j} = C_{i,j} + \min\{ D_{i-1,j-1}, D_{i, j-1}, D_{i-1, j} \}$
2. $D^-_{i,j} = d^X_j + D_{i,j-1}$
3. $D_{i,j} = \min\{ D^+_{i,j}, D^-_{i,j} \}$

With $D_{0,0}=0$, $D^+_{i,0}=D^+_{0,j}=\infty$, $D^-_{0,j} = \sum_{k=1}^j d^X_k$, $D^-_{i,0} = \infty$.

### Full (bi-directional drop) version:

Four tables track match/drop for each boundary:
1. $D^{yx}_{i,j}$ (match), $D^{y-}_{i,j}$ (drop-$X$), $D^{-x}_{i,j}$ (drop-$Y$), $D^{--}_{i,j}$ (drop both).
2. Recurrences (see Drop-DTW summary above for full equations).

Standard back-tracing yields optimal match/drop decisions $M^*$. This dynamic program accommodates a strictly monotonic but partially matched correspondence.

## 4. Differentiable ("Soft") Drop-DTW

All "min" operators are replaced by a soft-min:
\[
\text{softMin}(v; \gamma) = \frac{v_1 e^{-v_1/\gamma} + \dots + v_M e^{-v_M/\gamma}}{e^{-v_1/\gamma} + \cdots + e^{-v_M/\gamma}}
\]
As $\gamma \to 0$, soft-min converges to hard min. This yields a fully differentiable dynamic program suitable for gradient-based optimization. Gradients propagate through the Drop-DTW recurrence via the chain rule, as in Soft-DTW and differentiable DP in modern attention models. The entire computation remains $O(KN)$ in time and space.

## 5. Pseudocode and Implementation

For the drop-$X$ variant, the core procedure is:

```python
function DropDTW(C[0..K,0..N], dX[1..N]):
  # initialize
  for i=0..K:
    D⁺[i,0]=∞; D⁻[i,0]=∞; D[i,0]=∞
  for j=0..N:
    D⁺[0,j]=∞; D⁻[0,j]=sum(dX[1..j]); D[0,j]=D⁻[0,j]
  D[0,0]=0; D⁺[0,0]=∞; D⁻[0,0]=0

  # fill DP
  for i in 1..K:
    for j in 1..N:
      matchCost = C[i,j] + min(D[i-1,j-1], D[i, j-1], D[i-1,j])
      dropCost  = dX[j]  + D[i, j-1]
      D⁺[i,j] = matchCost
      D⁻[i,j] = dropCost
      D [i,j] = min(matchCost, dropCost)

  # back-trace to build M*
  M = zeros(K,N)
  (i,j) = (K,N)
  while i>0 or j>0:
    if D⁺[i,j] ≤ D⁻[i,j]:
      M[i,j] = 1  # matched
      # choose predecessor that gave D⁺
      (i,j) = argmin predecessor cost
    else:
      # dropped x_j
      j = j-1
  return D[K,N], M
```

Pairwise cost $C_{i,j}$ can be symmetric ($1-\text{cosine}$) or asymmetric (negative log-softmax). Drop penalty $d$ may be a fixed percentile of $C$ (tunable $p\%$) or derived via a small neural network predicting per-element costs. The soft-min temperature $\gamma$ trades bias versus stability ($\gamma\sim0.1$–$1$ practical). For very long sequences, windowing/pruning (e.g., Sakoe–Chiba band) can reduce computation and memory to $O(w \cdot \max(K,N))$.

## 6. Computational Complexity

Drop-DTW requires $O(K N)$ time and space for full alignment and back-trace, equivalent to standard DTW. If only the final alignment cost is needed, memory can be reduced to $O(\min(K, N))$. Application of windowing or pruning further optimizes runtime for very long sequences while maintaining alignment fidelity.

| Step                  | Time Complexity | Space Complexity |
|-----------------------|----------------|-----------------|
| Full DP fill          | $O(KN)$        | $O(KN)$         |
| Cost-only computation | $O(KN)$        | $O(\min(K,N))$  |
| Windowed DP           | $O(w\max(K,N))$| $O(w\max(K,N))$ |

## 7. Experimental Applications and Empirical Findings

Drop-DTW has been validated on multiple noisy alignment and retrieval tasks:

- **Synthetic trajectory retrieval (TMNIST):** Short sub-trajectories with interspersed Gaussian-blurred frames ("noise"). Drop-DTW Recall@1 greatly exceeds DTW (up to $8\times$ improvement under heavy noise), and achieves 97.1% subsequence localization accuracy with IoU = 91.2%.
- **Instructional video localization (CrossTask, COIN, YouCook2):** In weakly supervised settings (ordered step descriptions, no frame labels), Drop-DTW (percentile drop cost) outperforms Smooth-DTW, D$^3$TW, and OTAM by 3–5 points in IoU. With learned drop costs plus clustering regularizer: CrossTask IoU = 36.9% (vs 30.5% for Smooth-DTW); COIN IoU = 29.5%; YouCook2 IoU = 49.4%.
- **Unsupervised representation learning (PennAction):** Under heavy outlier contamination (up to 50% distractor frames), models trained with Drop-DTW loss yield high Kendall’s $\tau$ alignment accuracy, while Smooth-DTW degrades rapidly.
- **Unsupervised cross-modal audio-visual localization (AVE):** Given a 1 s query in one modality, Drop-DTW used as matching cost in a contrastive triplet loss achieves A2V localization of 41.1% (vs Smooth-DTW 39.8% and OTAM 37.5%) and V2A localization of 35.8% (exceeding even a supervised baseline on V2A).

In all applications, Drop-DTW's ability to exclude arbitrary outliers establishes a more robust similarity measure than DTW or previous differentiable DTW methods, supporting weakly and unsupervised sequence localization and retrieval tasks in vision and multimodal domains [2108.11996].

Source: https://www.emergentmind.com/topics/drop-dtw