mr2.nn.nets.BasicCNN

class mr2.nn.nets.BasicCNN[source]

Bases: Sequential

Basic CNN.

A series of convolutions (window 3, stride 1, padding 1), normalization and activation. Allows to use FiLM conditioning. Order is Conv -> Norm (optional) -> FiLM (optional) -> Activation.

If you need more flexibility, use Sequential directly.

__init__(n_dim: int, n_channels_in: int, n_channels_out: int, norm: Literal['batch', 'group', 'instance', 'none', 'layer'] = 'none', activation: Literal['relu', 'silu', 'leaky_relu'] = 'relu', n_features: Sequence[int] = (64, 64, 64), cond_dim: int = 0)[source]

Initialize a basic CNN.

Parameters:
  • n_dim (int) – The number of spatial dimensions of the input tensor.

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

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

  • norm (Literal['batch', 'group', 'instance', 'none', 'layer'], default: 'none') – The type of normalization to use. If ‘batch’, use batch normalization. If ‘group’, use group normalization, if ‘instance’, use instance normalization, and if layer, use layer normalization. If ‘none’, use no normalization.

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

  • n_features (Sequence[int], default: (64, 64, 64)) – The number of features in the hidden layers. The length of this sequence determines the number of hidden layers. The total number of convolutions 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 convolutions, after normalization, FiLM conditioning is applied.

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

Apply the basic CNN to the input tensor.

Parameters:
  • x (Tensor) – The input tensor. Should be of shape (batch_size, channels_in, *spatial dimensions) with spatial dimensions being of length dim.

  • 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 all modules in series to the input.

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.