mr2.nn.nets.MLP

class mr2.nn.nets.MLP[source]

Bases: Sequential

Multi-layer perceptron.

A series of linear layers, normalization and activation. Allows FiLM conditioning. Order is Linear -> Norm (optional) -> FiLM (optional) -> Activation.

If you need more flexibility, use Sequential directly.

__init__(n_channels_in: int, n_channels_out: int, norm: Literal['layer', 'none'] = 'none', activation: Literal['gelu', 'relu', 'silu', 'leaky_relu'] = 'gelu', n_features: Sequence[int] = (256, 256), cond_dim: int = 0, features_last: bool = True)[source]

Initialize a MLP.

Parameters:
  • n_channels_in (int) – The number of input channels.

  • n_channels_out (int) – The number of output channels.

  • norm (Literal['layer', 'none'], default: 'none') – The type of normalization to use. If layer, use layer normalization. If none, use no normalization.

  • activation (Literal['gelu', 'relu', 'silu', 'leaky_relu'], default: 'gelu') – The type of activation to use. If gelu, use GELU. If relu, use ReLU. If silu, use SiLU. If leaky_relu, use LeakyReLU.

  • n_features (Sequence[int], default: (256, 256)) – The number of features in the hidden layers. The length of this sequence determines the number of hidden layers. The total number of linear layers is len(n_features) + 1.

  • cond_dim (int, default: 0) – The dimension of the condition tensor. If 0, no FiLM conditioning is applied. Otherwise, between linear layers, after normalization, FiLM conditioning is applied.

  • features_last (bool, default: True) – Whether the features are in the last dimension, as common in transformer models, or in the second dimension, as common in image models.

__call__(x: Tensor, *, cond: Tensor | None = None) → Tensor[source]

Apply the MLP to the input tensor.

Parameters:
  • x (Tensor) – The input tensor.

  • cond (Tensor | None, default: None) – The condition tensor. If None, no FiLM conditioning is applied.

Returns:

The output tensor.

forward(*x: Tensor, cond: Tensor | None = None) → Tensor[source]

Apply the MLP to the input tensor.

append(module: Module) → Self

Append a given module to the end.

Parameters:

module (nn.Module) – module to append

Example:

>>> import torch.nn as nn
>>> n = nn.Sequential(nn.Linear(1, 2), nn.Linear(2, 3))
>>> n.append(nn.Linear(3, 4))
Sequential(
    (0): Linear(in_features=1, out_features=2, bias=True)
    (1): Linear(in_features=2, out_features=3, bias=True)
    (2): Linear(in_features=3, out_features=4, bias=True)
)
extend(sequential: Iterable[Module]) → Self

Extends the current Sequential container with layers from another Sequential container.

Parameters:

sequential (Sequential) – A Sequential container whose layers will be added to the current container.

Example:

>>> import torch.nn as nn
>>> n = nn.Sequential(nn.Linear(1, 2), nn.Linear(2, 3))
>>> other = nn.Sequential(nn.Linear(3, 4), nn.Linear(4, 5))
>>> n.extend(other) # or `n + other`
Sequential(
    (0): Linear(in_features=1, out_features=2, bias=True)
    (1): Linear(in_features=2, out_features=3, bias=True)
    (2): Linear(in_features=3, out_features=4, bias=True)
    (3): Linear(in_features=4, out_features=5, bias=True)
)
insert(index: int, module: Module) → Self

Inserts a module into the Sequential container at the specified index.

Parameters:
  • index (int) – The index to insert the module.

  • module (Module) – The module to be inserted.

Example:

>>> import torch.nn as nn
>>> n = nn.Sequential(nn.Linear(1, 2), nn.Linear(2, 3))
>>> n.insert(0, nn.Linear(3, 4))
Sequential(
    (0): Linear(in_features=3, out_features=4, bias=True)
    (1): Linear(in_features=1, out_features=2, bias=True)
    (2): Linear(in_features=2, out_features=3, bias=True)
)
pop(key: int | slice) → Module

Pop key from self.