NN

This module contains pytorch compatible layers of the SPDNet architecture.

class anotherspdnet.nn.BiMap(*args: Any, **kwargs: Any)[source]
__init__(n_in: int, n_out: int, n_batches: tuple | None = None, manifold: str = 'stiefel', seed: int | None = None, dtype: torch.dtype = torch.float64, device: torch.device = torch.device, mm_mode: str = 'einsum', use_autograd: bool = False) None[source]
BiMap layer in a SPDnet layer according to the paper:

A Riemannian Network for SPD Matrix Learning, Huang et al AAAI Conference on Artificial Intelligence, 2017

Parameters:
  • n_in (int) – Number of input features.

  • n_out (int) – Number of output features.

  • n_batches (tuple) – Number of Batches of SPD matrices. It must be a tuple containing at least one batch dimension. Default is None.

  • manifold (str, optional) – Manifold on which the layer is initialized. Default is ‘stiefel’. choice between ‘stiefel’ and ‘sphere’.

  • seed (int, optional) – Seed for the initialization of the weight matrix. Default is None.

  • dtype (torch.dtype, optional) – Data type of the layer. Default is torch.float64.

  • device (torch.device, optional) – Device on which the layer is initialized. Default is ‘cpu’.

  • mm_mode (str, optional) – Mode for the matrix multiplication. Default is ‘einsum’. Choice between ‘einsum’ and ‘bmm’.

  • use_autograd (bool, optional) – Use torch autograd for the computation of the gradient rather than the analytical formula. Default is False.

forward(X: torch.Tensor) torch.Tensor[source]

Forward pass of the BiMap layer

Parameters:

X (torch.Tensor of shape self.n_batches + (n_matrices, n_in, n_in)) – Batches of input SPD matrices.

Returns:

Y – The output matrices is close to SPD. They need regularization with the ReEig layer especially if n_out > n_in.

Return type:

torch.Tensor of shape self.n_batches + (n_matrices, n_out, n_out)

class anotherspdnet.nn.ReEigBias(*args: Any, **kwargs: Any)[source]
__init__(dim: int, eps: float = 0.0001, use_autograd: bool = False, dtype: torch.dtype = torch.float64, device: torch.device = torch.device, seed: int | None = None) None[source]

ReEig layer with a bias term.

Parameters:
  • dim (int) – Dimension of the SPD matrices.

  • eps (float, optional) – Value of rectification of the eigenvalues. Default is 1e-4.

  • use_autograd (bool, optional) – Use torch autograd for the computation of the gradient rather than the analytical formula. Default is True. FOR NOW: without autograd, the layer is not implemented.

  • dtype (torch.dtype, optional) – Data type of the layer. Default is torch.float64.

  • device (torch.device, optional) – Device on which the layer is initialized. Default is ‘cpu’.

  • seed (int, optional) – Seed for the initialization of the bias term. Default is None.

forward(X: torch.Tensor) torch.Tensor[source]

Forward pass of the ReEig layer with bias

Parameters:

X (torch.Tensor of shape (..., n_features, n_features)) – Batches of input almost-SPD matrices.

Returns:

Y – The regularized SPD matrices.

Return type:

torch.Tensor of shape (…, n_features, n_features)

class anotherspdnet.nn.ReEig(*args: Any, **kwargs: Any)[source]
__init__(eps: float = 0.0001, use_autograd: bool = False, mm_mode: str = 'einsum', eig_function: str = 'eigh', formula: str = 'brooks', dim: int | None = None) None[source]
ReEig layer in a SPDnet layer according to the paper:

A Riemannian Network for SPD Matrix Learning, Huang et al AAAI Conference on Artificial Intelligence, 2017

Parameters:
  • eps (float, optional) – Value of rectification of the eigenvalues. Default is 1e-4.

  • use_autograd (bool, optional) – Use torch autograd for the computation of the gradient rather than the analytical formula. Default is False.

  • mm_mode (str, optional) – Mode for the matrix multiplication. Default is ‘einsum’. Choice between ‘einsum’ and ‘bmm’.

  • eig_function (str, optional) – Function used for the computation of the eigendecomposition. Default is ‘eigh’. Choice between ‘eigh’ and ‘eig’.

  • formula (str, optional) –

    Formula used for the computation of the gradient. Default is

    ’brooks’. Choice between ‘brooks’ and ‘ionescu”.

  • dim (int, optional) – Dimension of the SPD matrices. Default is None. Used for logging purposes.

forward(X: torch.Tensor) torch.Tensor[source]

Forward pass of the ReEig layer

Parameters:

X (torch.Tensor of shape (..., n_features, n_features)) – Batches of input almost-SPD matrices.

Returns:

Y – The regularized SPD matrices.

Return type:

torch.Tensor of shape (…, n_features, n_features)

class anotherspdnet.nn.LogEig(*args: Any, **kwargs: Any)[source]
__init__(use_autograd: bool = False, mm_mode: str = 'einsum', eig_function: str = 'eigh', formula: str = 'brooks') None[source]
LogEig layer in a SPDnet layer according to the paper:

A Riemannian Network for SPD Matrix Learning, Huang et al AAAI Conference on Artificial Intelligence, 2017

Parameters:
  • use_autograd (bool, optional) – Use torch autograd for the computation of the gradient rather than the analytical formula. Default is False.

  • mm_mode (str, optional) – Mode for the matrix multiplication. Default is ‘einsum’. Choice between ‘einsum’ and ‘bmm’.

  • eig_function (str, optional) – Function used for the computation of the eigendecomposition. Default is ‘eigh’. Choice between ‘eigh’ and ‘eig’.

  • formula (str, optional) – Formula used for the computation of the gradient. Default is ‘brooks’. Choice between ‘brooks’ and ‘ionescu”.

forward(X: torch.Tensor) torch.Tensor[source]

Forward pass of the ReEig layer

Parameters:

X (torch.Tensor of shape (..., n_features, n_features)) – Batches of input almost-SPD matrices.

Returns:

Y – The regularized SPD matrices.

Return type:

torch.Tensor of shape (…, n_features, n_features)

class anotherspdnet.nn.Vectorization(*args: Any, **kwargs: Any)[source]
__init__() None[source]

Vectorization of a batch of matrices according to the last two dimensions

forward(X: torch.Tensor) torch.Tensor[source]

Forward pass of the Vectorization layer

Parameters:

X (torch.Tensor of shape (..., n, k)) – Batch of matrices.

Returns:

X_vec – Batch of vectorized matrices.

Return type:

torch.Tensor of shape (…, n*k)

inverse_transform(X: torch.Tensor, n_rows: int) torch.Tensor[source]

Inverse transform of the Vectorization layer

Parameters:
  • X (torch.Tensor of shape (..., n_rows*k)) – Batch of vectorized matrices.

  • n_rows (int) – Number of rows of the original matrices.

Returns:

X_vec – Batch of matrices.

Return type:

torch.Tensor of shape (…, n_rows, k)

class anotherspdnet.nn.Vech(*args: Any, **kwargs: Any)[source]
__init__() None[source]

Vech operator of a batch of matrices according to the last two dimensions

forward(X: torch.Tensor) torch.Tensor[source]

Forward pass of the Vech layer

Parameters:

X (torch.Tensor of shape (..., n, k)) – Batch of matrices.

Returns:

X_vech – Batch of vech matrices.

Return type:

torch.Tensor of shape (…, n*(n+1)//2)

inverse_transform(X: torch.Tensor) torch.Tensor[source]

Inverse transform of the Vech layer

Parameters:

X (torch.Tensor of shape (..., n*(n+1)//2)) – Batch of vech matrices.

Returns:

X_vech – Batch of matrices.

Return type:

torch.Tensor of shape (…, n, k)