MultiLayerPerceptron

class pfhedge.nn.MultiLayerPerceptron(in_features=None, out_features=1, n_layers=4, n_units=32, activation=ReLU(), out_activation=Identity())[source]

Creates a multilayer perceptron.

Number of input features is lazily determined.

Parameters
  • in_features (int, optional) – Size of each input sample. If None (default), the number of input features will be will be inferred from the input.shape[-1] after the first call to forward is done. Also, before the first forward parameters in the module are of torch.nn.UninitializedParameter class.

  • out_features (int, default=1) – Size of each output sample.

  • n_layers (int, default=4) – The number of hidden layers.

  • n_units (int or tuple[int], default=32) – The number of units in each hidden layer. If tuple[int], it specifies different number of units for each layer.

  • activation (torch.nn.Module, default=torch.nn.ReLU()) – The activation module of the hidden layers. Default is a torch.nn.ReLU instance.

  • out_activation (torch.nn.Module, default=torch.nn.Identity()) – The activation module of the output layer. Default is a torch.nn.Identity instance.

Shape:
  • Input: \((N, *, H_{\text{in}})\) where \(*\) means any number of additional dimensions and \(H_{\text{in}}\) is the number of input features.

  • Output: \((N, *, H_{\text{out}})\) where all but the last dimension are the same shape as the input and \(H_{\text{out}}\) is the number of output features.

Examples

By default, in_features is lazily determined:

>>> import torch
>>> from pfhedge.nn import MultiLayerPerceptron
>>>
>>> m = MultiLayerPerceptron()
>>> m
MultiLayerPerceptron(
  (0): LazyLinear(in_features=0, out_features=32, bias=True)
  (1): ReLU()
  (2): Linear(in_features=32, out_features=32, bias=True)
  (3): ReLU()
  (4): Linear(in_features=32, out_features=32, bias=True)
  (5): ReLU()
  (6): Linear(in_features=32, out_features=32, bias=True)
  (7): ReLU()
  (8): Linear(in_features=32, out_features=1, bias=True)
  (9): Identity()
)
>>> _ = m(torch.zeros(3, 2))
>>> m
MultiLayerPerceptron(
  (0): Linear(in_features=2, out_features=32, bias=True)
  (1): ReLU()
  (2): Linear(in_features=32, out_features=32, bias=True)
  (3): ReLU()
  (4): Linear(in_features=32, out_features=32, bias=True)
  (5): ReLU()
  (6): Linear(in_features=32, out_features=32, bias=True)
  (7): ReLU()
  (8): Linear(in_features=32, out_features=1, bias=True)
  (9): Identity()
)

Specify different number of layers for each layer:

>>> m = MultiLayerPerceptron(1, 1, n_layers=2, n_units=(16, 32))
>>> m
MultiLayerPerceptron(
  (0): Linear(in_features=1, out_features=16, bias=True)
  (1): ReLU()
  (2): Linear(in_features=16, out_features=32, bias=True)
  (3): ReLU()
  (4): Linear(in_features=32, out_features=1, bias=True)
  (5): Identity()
)