Pad variable-length paths by repeating each path's last point.
This is the recommended padding strategy when batching variable-length paths
for signatures/log-signatures: repeating the final valid point produces zero
increments on the padded tail, so the signature remains unchanged.
When using this padding strategy, you typically do not need to pass a
lengths tensor to
signature_sparse or sparse
log-signature calls,
because the padding does not affect the result.
Parameters:
| Name |
Type |
Description |
Default |
paths
|
list[Tensor]
|
List of tensors shaped (T_i, D) with possibly different lengths
T_i. Each path must have at least one point.
|
required
|
max_length
|
int
|
Target padded length. If None, uses max(T_i).
|
None
|
Returns:
| Type |
Description |
tuple[Tensor, Tensor]
|
- padded_paths: Tensor of shape
(batch, max_length, D)
- lengths: Tensor of shape
(batch,) containing the original lengths
|
Examples:
>>> import torch
>>> from tensorsig.classical.sparse import pad_paths_correctly, signature_sparse
>>>
>>> paths = [
... torch.tensor([[0.0], [1.0], [2.0]]), # length 3
... torch.tensor([[0.0], [1.0]]), # length 2
... ]
>>> padded, lengths = pad_paths_correctly(paths)
>>> padded.shape
torch.Size([2, 3, 1])
>>> lengths
tensor([3, 2])
>>> # Padding is "signature-safe" (zero increments), so lengths is optional here:
>>> sig = signature_sparse(padded, depth=2)
Source code in src/tensorsig/classical/sparse_signature.py
| def pad_paths_correctly(
paths: list[Tensor], max_length: int | None = None
) -> tuple[Tensor, Tensor]:
"""Pad variable-length paths by repeating each path's last point.
This is the recommended padding strategy when batching variable-length paths
for signatures/log-signatures: repeating the final valid point produces zero
increments on the padded tail, so the signature remains unchanged.
When using this padding strategy, you typically do **not** need to pass a
``lengths`` tensor to
[`signature_sparse`][tensorsig.classical.sparse.signature_sparse] or sparse
log-signature calls,
because the padding does not affect the result.
Parameters
----------
paths : list[Tensor]
List of tensors shaped ``(T_i, D)`` with possibly different lengths
``T_i``. Each path must have at least one point.
max_length : int, optional
Target padded length. If None, uses ``max(T_i)``.
Returns
-------
tuple[Tensor, Tensor]
- padded_paths: Tensor of shape ``(batch, max_length, D)``
- lengths: Tensor of shape ``(batch,)`` containing the original lengths
Examples
--------
>>> import torch
>>> from tensorsig.classical.sparse import pad_paths_correctly, signature_sparse
>>>
>>> paths = [
... torch.tensor([[0.0], [1.0], [2.0]]), # length 3
... torch.tensor([[0.0], [1.0]]), # length 2
... ]
>>> padded, lengths = pad_paths_correctly(paths)
>>> padded.shape
torch.Size([2, 3, 1])
>>> lengths
tensor([3, 2])
>>> # Padding is "signature-safe" (zero increments), so lengths is optional here:
>>> sig = signature_sparse(padded, depth=2)
"""
if len(paths) == 0:
raise ValueError("paths must be a non-empty list of tensors")
lengths_list = [int(p.shape[0]) for p in paths]
if any(length <= 0 for length in lengths_list):
raise ValueError("each path must have at least one point (T_i >= 1)")
first = paths[0]
if first.ndim != 2:
raise ValueError(
f"each path must have shape (T, D); got {first.shape} for paths[0]"
)
d = int(first.shape[1])
device = first.device
dtype = first.dtype
for i, p in enumerate(paths[1:], start=1):
if p.ndim != 2:
raise ValueError(f"each path must have shape (T, D); got {p.shape} at {i}")
if int(p.shape[1]) != d:
raise ValueError(
f"all paths must have the same D; got {d} and {int(p.shape[1])} at {i}"
)
if p.device != device:
raise ValueError("all paths must be on the same device")
if p.dtype != dtype:
raise ValueError("all paths must have the same dtype")
if max_length is None:
max_length = max(lengths_list)
max_length = int(max_length)
if max_length <= 0:
raise ValueError("max_length must be >= 1")
if max_length < max(lengths_list):
raise ValueError("max_length must be >= max(path lengths)")
batch = len(paths)
padded = torch.empty((batch, max_length, d), device=device, dtype=dtype)
lengths = torch.tensor(lengths_list, device=device, dtype=torch.long)
for b, p in enumerate(paths):
t = int(p.shape[0])
padded[b, :t] = p
if t < max_length:
padded[b, t:] = p[-1].expand(max_length - t, -1)
return padded, lengths
|