Sparse Signature
log_signatures_pytorch.sparse_signature.pad_paths_correctly(paths, max_length=None)
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 :func:signature_sparse/sparse log-signature calls,
because the padding does not affect the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
list[Tensor]
|
List of tensors shaped |
required |
max_length
|
int
|
Target padded length. If None, uses |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
|
Examples:
>>> import torch
>>> from log_signatures_pytorch.sparse_signature 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/log_signatures_pytorch/sparse_signature.py
log_signatures_pytorch.sparse_signature.signature_sparse(path, depth, eps=0.0, lengths=None, return_levels=False, stream=False)
Compute sparse path signature for paths with repeated points.
Uses Chen's identity to combine segment signatures, skipping zero increments (repeated points). For a path with M knots, computes the signature as the ordered tensor product of M-1 segment exponentials.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Tensor
|
Tensor of shape |
required |
depth
|
int
|
Maximum depth L for truncation (>=1). |
required |
eps
|
float
|
Threshold for change detection. Default is 0.0. |
0.0
|
lengths
|
Tensor
|
Tensor of shape Best practice (recommended): pad by repeating the last valid point
of each path (see :func: If you instead pad with zeros/any other values, you must pass Default is None. |
None
|
return_levels
|
bool
|
If True, return list of level tensors. If False, return flattened signature. Default is False. |
False
|
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 or list[Tensor]
|
If If |
Examples:
>>> import torch
>>> from log_signatures_pytorch.sparse_signature import signature_sparse
>>>
>>> # Path with repeats
>>> path = torch.tensor([
... [[0.0, 0.0], [0.0, 0.0], [1.0, 1.0], [1.0, 1.0], [2.0, 0.0]]
... ])
>>> sig = signature_sparse(path, depth=2)
>>> sig.shape
torch.Size([1, 6])
>>> stream_sig = signature_sparse(path, depth=2, stream=True)
>>> stream_sig.shape
torch.Size([1, 4, 6])
Source code in src/log_signatures_pytorch/sparse_signature.py
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 | |