waxmorph.torch.mlp.MLP

Contents

waxmorph.torch.mlp.MLP#

class waxmorph.torch.mlp.MLP(input_dim, output_dim, hidden_dim=128, num_layers=2, activation='silu', layer_norm=True)[source]#

Bases: Module

MLP with one or more linear layers.

num_layers counts all linear layers. Depth one is a single affine map; deeper networks apply the selected activation after each hidden layer. Optional LayerNorm follows the final projection. Activations are relu, silu, gelu, and tanh. Boolean, nonintegral, and nonpositive depths are rejected.

Examples

>>> import torch
>>> mlp = MLP(3, 2, hidden_dim=4, num_layers=1, layer_norm=False)
>>> print(tuple(mlp(torch.ones(5, 3)).shape))
(5, 2)
Parameters:
  • input_dim (int)

  • output_dim (int)

  • hidden_dim (int)

  • num_layers (int)

  • activation (str)

  • layer_norm (bool)

forward(x)[source]#

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.