dymad.modules.mlp

Classes

IdenCatMLP(input_dim, hidden_dim, output_dim)

Identity concatenation MLP.

MLP(input_dim, hidden_dim, output_dim, *[, ...])

Fully-connected feed-forward network

ResBlockMLP(input_dim, hidden_dim, output_dim)

Residual block with MLP as the nonlinearity.

class dymad.modules.mlp.IdenCatMLP(input_dim, hidden_dim, output_dim, n_layers=2, activation=<class 'torch.nn.modules.activation.ReLU'>, weight_init=<function xavier_uniform_>, bias_init=<function zeros_>, gain=1.0, end_activation=True, dtype=None, device=None)

Bases: MLP

Identity concatenation MLP.

This MLP concatenates the input with the output of the MLP.

Note

The output dimension represents the total output features and must be greater than the input dimension.

See MLP for the arguments.

forward(x)

Forward pass through the identity concatenation MLP.

Parameters:

x (torch.Tensor) – Input tensor of shape (…, input_dim).

Returns:

Output tensor of shape (…, output_dim).

Return type:

torch.Tensor

class dymad.modules.mlp.MLP(input_dim, hidden_dim, output_dim, *, n_layers=2, activation=<class 'torch.nn.modules.activation.ReLU'>, weight_init=<function xavier_uniform_>, bias_init=<function zeros_>, gain=1.0, end_activation=True, dtype=None, device=None)

Bases: Module

Fully-connected feed-forward network

Assuming the following architecture:

in_dim -> (Linear -> Act) x n_hidden -> Linear -> out_dim

Parameters:
  • input_dim (int) – Dimension of the input features.

  • hidden_dim (int) – Width of every hidden layer.

  • output_dim (int) – Dimension of the network output.

  • n_layers (int, default = 2) –

    Number of total layers.

    • If 0, same as Identity, or TakeFirst.

    • If 1, same as Linear.

    • If 2, same as Linear -> activation -> Linear.

    • Otherwise, hidden layers are inserted.

  • activation (nn.Module or Callable[[], nn.Module], default = nn.ReLU) – Non-linearity to insert after every hidden Linear. Pass either a class (e.g. nn.Tanh) or an already-constructed module.

  • weight_init (Callable[[torch.Tensor, float], None], default = nn.init.kaiming_uniform_) – Function used to initialise each Linear layer’s weight tensor. Must accept (tensor, gain) signature like the functions in torch.nn.init.

  • bias_init (Callable[[torch.Tensor], None], default = nn.init.zeros_) – Function used to initialise each Linear layer’s bias tensor.

  • gain (Optional[float], default = 1.0) – In the linear layers, the weights are initialised with the standard nn.init.calculate_gain(<nonlinearity>) Gain is multiplied to the calculated gain. By default gain=1, so no change.

  • end_activation (bool, default = True) –

    • If True, the last layer is followed by an activation function.

    • Otherwise, the last layer is a plain Linear layer.

diagnostic_info()
Return type:

str

forward(x)

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class dymad.modules.mlp.ResBlockMLP(input_dim, hidden_dim, output_dim, n_layers=2, activation=<class 'torch.nn.modules.activation.ReLU'>, weight_init=<function xavier_uniform_>, bias_init=<function zeros_>, gain=1.0, end_activation=True, dtype=None, device=None)

Bases: MLP

Residual block with MLP as the nonlinearity.

See MLP for the arguments.

forward(x)

Forward pass through the residual block.

Parameters:

x (torch.Tensor) – Input tensor of shape (…, input_dim).

Returns:

Output tensor of shape (…, output_dim).

Return type:

torch.Tensor