Built-In Variants
The classes in ddtw.ddtw_variants are convenience frontends around the
general dDTW class. Each variant fixes the graph components introduced in
the core concepts:
The table uses the paper’s one-based notation. Tensor indices in the implementation are zero-based.
Parameter Overview
Variant |
\(\mathcal{S}\) |
\(\mathbf{W}(n,m)\) |
\(\mathcal{B}_\mathrm{start}\) |
\(\mathcal{B}_\mathrm{end}\) |
\(\mu\) |
|---|---|---|---|---|---|
|
\((1,0)\) |
\([1,1,1]\) |
\(\{(1,1)\}\) |
\(\{(N,M)\}\) |
\(\mu_\mathrm{hard}\) |
|
\((1,0)\) |
\([1,1,1]\) |
\((1,1),\ldots\) |
\((1,M),\ldots\) |
\(\mu_\mathrm{hard}\) |
|
\((1,0)\) |
\([0,0,1]\) |
\(\mathcal{I}\) |
\(\mathcal{I}\) |
\(\mu_\mathrm{hard}\) |
|
\((1,0)\) |
allowed: \([1,1,1]\) |
\((1,1)\) |
\((N,2M)\) |
\(\mu_\mathrm{soft}\) |
DTW and Differentiable DTW Variants
Classical DTW and SDTW use the same graph topology. [1]
[2]
with boundary conditions
They differ only in the aggregation operator. DTW uses
\(\mu_\mathrm{hard}\), so it selects one minimum-cost path. SDTW uses
\(\mu_\mathrm{soft}\), so it softly aggregates all paths and yields a
smooth loss. The start weight
\(w_\mathrm{start}^{(1,1)}=c(x_1,y_1)\) accounts for the initial local
cost, and \(w_\mathrm{end}^{(N,M)}=0\).
smoothDTW and sparseDTW keep the same graph and boundary conditions but
replace \(\mu\) by \(\mu_\mathrm{smooth}\) or
\(\mu_\mathrm{sparse}\). [3] [4] These operators can
be used as differentiable recursive losses, but they do not yield the same
global path aggregation cost guaranteed for hardmin and softmin.
Subsequence (S)DTW
subSDTW is designed for matching a query sequence
\(Y=(y_1,\ldots,y_M)\) to a subsequence of a longer document
\(X=(x_1,\ldots,x_N)\), typically with \(N\gg M\). [5] It
keeps the standard DTW step set
but widens the boundary conditions. For the standard query-in-document setup, paths may start anywhere in the first query column and end anywhere in the last query column:
Hard subsequence DTW uses \(\mu_\mathrm{hard}\); differentiable subsequence SDTW uses \(\mu_\mathrm{soft}\). [6] The toolbox also exposes boundary penalties. These implement the idea that start and end weights can compensate for skipped prefixes or suffixes and help prevent collapse to overly short alignments.
In the implementation, sub_X and sub_Y control whether subsequence
behavior is enabled along the prediction axis, the target axis, or both.
Partial Matching
Partial matching selects a monotonically ordered subset of matched sequence elements. [7] [8] [9] This behavior is obtained by keeping the standard step set
but assigning zero weight to horizontal and vertical moves:
Only diagonal steps accumulate the local matching cost. Since the match may start and end anywhere,
The local cost \(c\) should encode whether a local correspondence is
favorable. In the classical formulation, favorable matches have negative costs
and unfavorable matches have positive costs, so the best path selects a useful
ordered subset. The toolbox default partial_matching class uses
cost_function="CTC" and min_function="hardmin", but the same graph can
be paired with differentiable minimum functions when a smooth approximation is
desired.
CTC
CTC is represented by expanding the target sequence with the blank symbol
\(\epsilon\): [10] [11]
The alignment graph is then built over \(X\) and \(Y^\mathrm{e}\). The local cost is the negative log-probability
Paths start either at the initial blank or at the first target symbol and end either at the final target symbol or at the final blank:
The step set is
All steps are strictly monotonic in \(n\), so each input frame is consumed in order. A \((1,2)\) step skips over a blank and is allowed only when the adjacent target labels differ. The toolbox implements this constraint through local step weights
where finite weights mark allowed transitions and infinite weights suppress forbidden blank-skipping or repeated-label transitions. The aggregation operator is \(\mu_\mathrm{soft}\), which recovers the usual CTC log-sum-exp over valid label paths within the dDTW graph formulation.
In code, pass log-probabilities as X and integer target labels as Y.
The class constructs \(Y^\mathrm{e}\), boundary sets, and local transition
weights automatically.
References