Skip to content

LocalLinear

from tensorsig.offset_coded_signatures.layers import LocalLinear

Bases: Module

Apply a learned linear map to a compiled local feature plan.

Source code in src/tensorsig/offset_coded_signatures/layers.py
def __init__(
    self,
    plan: FeaturePlan,
    out_features: int,
    stride: int = 1,
    bias: bool = True,
    *,
    backend: str = "auto",
    chunk_size: int | None = None,
    device: torch.device | str | None = None,
    dtype: torch.dtype | None = None,
) -> None:
    super().__init__()
    if (
        not isinstance(out_features, int)
        or isinstance(out_features, bool)
        or out_features < 1
    ):
        raise ValueError("out_features must be a positive integer.")
    if not isinstance(bias, bool):
        raise TypeError("bias must be a boolean.")
    if dtype is not None and not dtype.is_floating_point:
        raise ValueError("dtype must be floating-point.")

    self.features = LocalFeatures(
        plan,
        stride=stride,
        backend=backend,
        chunk_size=chunk_size,
    )
    if device is not None or dtype is not None:
        self.features.to(device=device, dtype=dtype)

    parameter_device = device
    parameter_dtype = dtype
    if not isinstance(plan, PolynomialPlan):
        plan_matrix = plan._matrix_tensor()
        if parameter_device is None:
            parameter_device = plan_matrix.device
        if parameter_dtype is None:
            parameter_dtype = plan_matrix.dtype

    self.in_features = plan.feature_count
    self.out_features = out_features
    self.weight = torch.nn.Parameter(
        torch.empty(
            out_features,
            self.in_features,
            device=parameter_device,
            dtype=parameter_dtype,
        )
    )
    if bias:
        self.bias = torch.nn.Parameter(
            torch.empty(
                out_features,
                device=parameter_device,
                dtype=parameter_dtype,
            )
        )
    else:
        self.register_parameter("bias", None)
    self.reset_parameters()

plan property

plan: FeaturePlan

The compiled feature plan.

stride property

stride: int

Window stride measured in increments.

matrix property

matrix: Tensor | None

Registered code/projection matrix, if the plan has one.

reset_parameters

reset_parameters() -> None

Initialize the linear readout like torch.nn.Linear.

Source code in src/tensorsig/offset_coded_signatures/layers.py
def reset_parameters(self) -> None:
    """Initialize the linear readout like ``torch.nn.Linear``."""
    if self.in_features:
        torch.nn.init.kaiming_uniform_(self.weight, a=math.sqrt(5))
        bound = self.in_features**-0.5
        if self.bias is not None:
            torch.nn.init.uniform_(self.bias, -bound, bound)
    else:
        torch.nn.init.zeros_(self.weight)
        if self.bias is not None:
            torch.nn.init.zeros_(self.bias)

forward

forward(path: Tensor) -> Tensor

Return (batch, windows, out_features) local readouts.

Source code in src/tensorsig/offset_coded_signatures/layers.py
def forward(self, path: Tensor) -> Tensor:
    """Return ``(batch, windows, out_features)`` local readouts."""
    if isinstance(self.plan, PolynomialPlan):
        windows = increment_windows(
            path,
            size=self.plan.window,
            stride=self.stride,
        )
        if (
            windows.device != self.weight.device
            or windows.dtype != self.weight.dtype
        ):
            raise RuntimeError(
                "path dtype/device must match the module readout; move the "
                "module and input explicitly before evaluation"
            )
        output = self.plan.contract(
            windows,
            self.weight,
            chunk_size=self.features.chunk_size,
        )
        if self.bias is not None:
            output = output + self.bias
        return output
    features = self.features(path)
    return F.linear(features, self.weight, self.bias)