fovi.arch.mlp

class fovi.arch.mlp.LayerBlock(*args)[source]

Bases: Sequential

Simple wrapper that turns a list into a Sequential layer, dropping None’s.

This class filters out None values from the input arguments and creates a Sequential layer with the remaining valid layers.

__init__(*args)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

fovi.arch.mlp.get_mlp(repr_size=9216, mlp='8192-8192-8192', inp=None, linear=nn.Linear, act=partial(nn.ReLU, inplace=True), norm=nn.BatchNorm1d, dropout=partial(nn.Dropout, p=.5), dropout_all=False, proj_relu=False, output_bias=False, l2norm=False, out=None, offset_idx=5)[source]

Create a multi-layer perceptron (MLP) with configurable architecture.

Parameters:
  • repr_size (int, optional) – Input representation size. Defaults to 9216.

  • mlp (str or int, optional) – MLP architecture specification as a string of layer sizes separated by dashes (e.g., ‘8192-8192-8192’) or a single integer. Defaults to ‘8192-8192-8192’.

  • inp (callable, optional) – Input layer function that takes (idx, is_output) and returns a layer or None. Defaults to None.

  • linear (nn.Module, optional) – Linear layer class to use. Defaults to nn.Linear.

  • act (callable, optional) – Activation function to use. Defaults to partial(nn.ReLU, inplace=True).

  • norm (nn.Module, optional) – Normalization layer class to use. Defaults to nn.BatchNorm1d.

  • dropout (callable, optional) – Dropout layer function. Defaults to partial(nn.Dropout, p=.5).

  • dropout_all (bool, optional) – Whether to apply dropout to all layers including the output layer. Defaults to False.

  • proj_relu (bool, optional) – Whether to apply ReLU to the projection (output) layer. Defaults to False.

  • output_bias (bool, optional) – Whether to use bias in the output layer. Defaults to False.

  • l2norm (bool, optional) – Whether to apply L2 normalization to the output. Defaults to False.

  • out (callable, optional) – Output layer function that takes (idx, is_output) and returns a layer or None. Defaults to None.

  • offset_idx (int, optional) – Offset for block naming. Defaults to 5.

Returns:

A wrapped MLP module that can return embeddings and

intermediate layer outputs.

Return type:

MLPWrapper

class fovi.arch.mlp.MLPWrapper(mlp)[source]

Bases: Module

Wrapper for MLP that provides additional functionality for layer outputs.

This wrapper allows the MLP to return both the final embeddings and intermediate layer outputs for analysis or visualization purposes.

__init__(mlp)[source]

Initialize the MLP wrapper.

Parameters:

mlp (nn.Module) – The MLP module to wrap.

forward(x, return_layer_outputs=True)[source]

Forward pass through the MLP.

Parameters:
  • x (torch.Tensor) – Input tensor to process.

  • return_layer_outputs (bool, optional) – Whether to return intermediate layer outputs along with the final embeddings. Defaults to True.

Returns:

If return_layer_outputs is True, returns a tuple

of (embeddings, list_outputs) where embeddings is the final output and list_outputs contains all intermediate layer outputs. If False, returns only the final embeddings.

Return type:

torch.Tensor or tuple