torchoutil.nn.functional.segments module

activity_to_segments(
x: Tensor,
) LongTensor[source]

Extracts segments start and end positions from a boolean activity/mask tensor.

Example 1

>>> x = torch.as_tensor([0, 1, 1, 0, 0, 1, 1, 1, 1, 0]).bool()
>>> starts, ends = extract_segments(x)
>>> starts
... tensor([1, 5])
>>> ends
... tensor([3, 9])

Example 2

>>> x = torch.as_tensor([[1, 1, 1, 0], [1, 0, 0, 1]]).bool()
>>> indices, starts, ends = extract_segments(x)
>>> indices
... tensor([0, 1, 1])
>>> starts
... tensor([0, 0, 3])
>>> ends
... tensor([3, 1, 4])
Args:

x: (…, N) bool tensor containing D dims

Returns:
segments: (D+1, M) tensor, where M is the total number of segments

When D > 1, segments also contains indices of the source column for each start and end value. See Example 2 for details.

activity_to_segments_list(
x: Tensor,
) List[Tuple[int, int]] | list[source]
extract_segments(
x: Tensor,
) LongTensor[source]

Extracts segments start and end positions from a boolean activity/mask tensor.

Example 1

>>> x = torch.as_tensor([0, 1, 1, 0, 0, 1, 1, 1, 1, 0]).bool()
>>> starts, ends = extract_segments(x)
>>> starts
... tensor([1, 5])
>>> ends
... tensor([3, 9])

Example 2

>>> x = torch.as_tensor([[1, 1, 1, 0], [1, 0, 0, 1]]).bool()
>>> indices, starts, ends = extract_segments(x)
>>> indices
... tensor([0, 1, 1])
>>> starts
... tensor([0, 0, 3])
>>> ends
... tensor([3, 1, 4])
Args:

x: (…, N) bool tensor containing D dims

Returns:
segments: (D+1, M) tensor, where M is the total number of segments

When D > 1, segments also contains indices of the source column for each start and end value. See Example 2 for details.

segments_list_to_activity(
segments_list: List[Tuple[int, int]] | Tensor | list,
maxsize: int | None = None,
device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
) BoolTensor[source]
segments_to_activity(
x: Tensor,
) BoolTensor[source]
segments_to_list(
segments: Tensor,
maxsize: int | Tuple[int, ...] | None = None,
) List[Tuple[int, int]] | list[source]

Converts segments starts and ends to a list of (start, end) positions.

segments_to_segments_list(
segments: Tensor,
maxsize: int | Tuple[int, ...] | None = None,
) List[Tuple[int, int]] | list[source]

Converts segments starts and ends to a list of (start, end) positions.