Skip to content

signature

from tensorsig.classical.signatures import signature

Compute signatures for batched paths.

The signature of a path is a collection of iterated integrals that captures the path's geometric properties. It is computed as a truncated tensor series up to the specified depth.

Parameters:

Name Type Description Default
path Tensor

Tensor of shape (batch, length, dim) representing batched paths. For a single path, pass path.unsqueeze(0) to add a batch dimension.

required
depth int

Maximum depth to truncate signature computation. The output dimension will be sum(dim**k for k in range(1, depth+1)).

required
stream bool

If True, return signatures at each step along the path. If False, return only the final signature. Default is False.

False

Returns:

Type Description
Tensor

If stream=False: Tensor of shape (batch, dim + dim² + ... + dim^depth) containing the final signature for each path in the batch.

If stream=True: Tensor of shape (batch, length-1, dim + dim² + ... + dim^depth) containing signatures at each step along each path.

Raises:

Type Description
ValueError

If path is not three-dimensional.

Examples:

>>> import torch
>>> from tensorsig.classical.signatures import signature
>>>
>>> # Single path (add batch dimension)
>>> path = torch.tensor([[0.0, 0.0], [1.0, 1.0], [2.0, 0.0]]).unsqueeze(0)
>>> sig = signature(path, depth=2)
>>> sig.shape
torch.Size([1, 6])
>>>
>>> # Batched paths
>>> batch_paths = torch.tensor([
...     [[0.0, 0.0], [1.0, 1.0]],
...     [[0.0, 0.0], [2.0, 2.0]],
... ])
>>> sig = signature(batch_paths, depth=2)
>>> sig.shape
torch.Size([2, 6])
>>>
>>> # Streaming signatures
>>> sig_stream = signature(path, depth=2, stream=True)
>>> sig_stream.shape
torch.Size([1, 2, 6])
Source code in src/tensorsig/classical/signature.py
def signature(
    path: Tensor,
    depth: int,
    stream: bool = False,
) -> Tensor:
    """Compute signatures for batched paths.

    The signature of a path is a collection of iterated integrals that captures
    the path's geometric properties. It is computed as a truncated tensor series
    up to the specified depth.

    Parameters
    ----------
    path : Tensor
        Tensor of shape ``(batch, length, dim)`` representing batched paths.
        For a single path, pass ``path.unsqueeze(0)`` to add a batch dimension.
    depth : int
        Maximum depth to truncate signature computation. The output dimension
        will be ``sum(dim**k for k in range(1, depth+1))``.
    stream : bool, optional
        If True, return signatures at each step along the path. If False,
        return only the final signature. Default is False.
    Returns
    -------
    Tensor
        If ``stream=False``: Tensor of shape ``(batch, dim + dim² + ... + dim^depth)``
        containing the final signature for each path in the batch.

        If ``stream=True``: Tensor of shape ``(batch, length-1, dim + dim² + ... + dim^depth)``
        containing signatures at each step along each path.

    Raises
    ------
    ValueError
        If ``path`` is not three-dimensional.

    Examples
    --------
    >>> import torch
    >>> from tensorsig.classical.signatures import signature
    >>>
    >>> # Single path (add batch dimension)
    >>> path = torch.tensor([[0.0, 0.0], [1.0, 1.0], [2.0, 0.0]]).unsqueeze(0)
    >>> sig = signature(path, depth=2)
    >>> sig.shape
    torch.Size([1, 6])
    >>>
    >>> # Batched paths
    >>> batch_paths = torch.tensor([
    ...     [[0.0, 0.0], [1.0, 1.0]],
    ...     [[0.0, 0.0], [2.0, 2.0]],
    ... ])
    >>> sig = signature(batch_paths, depth=2)
    >>> sig.shape
    torch.Size([2, 6])
    >>>
    >>> # Streaming signatures
    >>> sig_stream = signature(path, depth=2, stream=True)
    >>> sig_stream.shape
    torch.Size([1, 2, 6])
    """
    if path.ndim != 3:
        msg = (
            f"Path must be of shape (batch, path_length, path_dim); got {path.shape}. "
            "Wrap a single path with path.unsqueeze(0)."
        )
        raise ValueError(msg)

    return _batch_signature(path, depth=depth, stream=stream)