spd_learn.modules.Vech#

class spd_learn.modules.Vech(preserve_norm: bool = True, upper: bool = True, device: device | None = None, dtype: dtype | None = None)[source]#

Bases: Module

Vectorize Triangular Part Layer.

This layer vectorizes the triangular part of a batch of symmetric matrices. By default, extracts the upper triangular with norm-preserving scaling.

Parameters:
  • preserve_norm (bool, default=True) – If True, applies sqrt(2) scaling to off-diagonal elements so that ||vec(X)||_2 = ||X||_F. If False, extracts raw triangular elements.

  • upper (bool, default=True) – If True, extracts upper triangular elements. If False, extracts lower triangular elements.

  • device (torch.device, optional) – Device to place the output tensor on.

  • dtype (torch.dtype, optional) – Data type of the output tensor.

forward(X: Tensor) → Tensor[source]#

Forward pass of the Vech layer.

Parameters:

X (torch.Tensor) – A batch of symmetric matrices with shape (…, n, n).

Returns:

A batch of vectorized matrices with shape (…, n * (n + 1) // 2).

Return type:

torch.Tensor

inverse_transform(X: Tensor) → Tensor[source]#

Inverse transform of the Vech layer.

Parameters:

X (torch.Tensor) – A batch of vectorized matrices with shape (…, n * (n + 1) // 2).

Returns:

A batch of symmetric matrices with shape (…, n, n).

Return type:

torch.Tensor