Skip to content

lit_sb3.modules.mlp

source module lit_sb3.modules.mlp

Classes

  • MlpExtractor — Constructs an MLP that receives the output from a previous features extractor (i.e. a CNN) or directly.

Functions

  • create_mlp — Create a multi layer perceptron (MLP), which is.

  • get_actor_critic_arch — Get the actor and critic network architectures for off-policy actor-critic algorithms (SAC, TD3, DDPG).

source create_mlp(input_dim: int, output_dim: int, net_arch: list[int], activation_fn: type[nn.Module] = nn.ReLU, squash_output: bool = False, with_bias: bool = True, pre_linear_modules: list[type[nn.Module]] | None = None, post_linear_modules: list[type[nn.Module]] | None = None) → list[nn.Module]

Create a multi layer perceptron (MLP), which is.

a collection of fully-connected layers each followed by an activation function.

Parameters

  • input_dim : int — Dimension of the input vector

  • output_dim : int — Dimension of the output (last layer, for instance, the number of actions)

  • net_arch : list[int] — Architecture of the neural net It represents the number of units per layer. The length of this list is the number of layers.

  • activation_fn : type[nn.Module] — The activation function to use after each layer.

  • squash_output : bool — Whether to squash the output using a Tanh activation function

  • with_bias : bool — If set to False, the layers will not learn an additive bias

  • pre_linear_modules : list[type[nn.Module]] | None — List of nn.Module to add before the linear layers. These modules should maintain the input tensor dimension (e.g. BatchNorm). The number of input features is passed to the module's constructor. Compared to post_linear_modules, they are used before the output layer (output_dim > 0).

  • post_linear_modules : list[type[nn.Module]] | None — List of nn.Module to add after the linear layers (and before the activation function). These modules should maintain the input tensor dimension (e.g. Dropout, LayerNorm). They are not used after the output layer (output_dim > 0). The number of input features is passed to the module's constructor.

Returns

  • list[nn.Module] — The list of layers of the neural network

source get_actor_critic_arch(net_arch: list[int] | dict[str, list[int]]) → tuple[list[int], list[int]]

Get the actor and critic network architectures for off-policy actor-critic algorithms (SAC, TD3, DDPG).

The net_arch parameter allows to specify the amount and size of the hidden layers, which can be different for the actor and the critic. It is assumed to be a list of ints or a dict.

  1. If it is a list, actor and critic networks will have the same architecture. The architecture is represented by a list of integers (of arbitrary length (zero allowed)) each specifying the number of units per layer. If the number of ints is zero, the network will be linear.
  2. If it is a dict, it should have the following structure: dict(qf=[<critic network architecture>], pi=[<actor network architecture>]). where the network architecture is a list as described in 1.

For example, to have actor and critic that share the same network architecture, you only need to specify net_arch=[256, 256] (here, two hidden layers of 256 units each).

If you want a different architecture for the actor and the critic, then you can specify net_arch=dict(qf=[400, 300], pi=[64, 64]).

.. note:: Compared to their on-policy counterparts, no shared layers (other than the features extractor) between the actor and the critic are allowed (to prevent issues with target networks).

Parameters

  • net_arch : list[int] | dict[str, list[int]] — The specification of the actor and critic networks. See above for details on its formatting.

Returns

  • tuple[list[int], list[int]] — The network architectures for the actor and the critic

source class MlpExtractor(feature_dim: int, net_arch: list[int] | dict[str, list[int]], activation_fn: type[nn.Module], device: torch.device | str = 'auto')

Bases : nn.Module

Constructs an MLP that receives the output from a previous features extractor (i.e. a CNN) or directly.

the observations (if no features extractor is applied) as an input and outputs a latent representation for the policy and a value network.

The net_arch parameter allows to specify the amount and size of the hidden layers. It can be in either of the following forms: 1. dict(vf=[<list of layer sizes>], pi=[<list of layer sizes>]): to specify the amount and size of the layers in the policy and value nets individually. If it is missing any of the keys (pi or vf), zero layers will be considered for that key. 2. [<list of layer sizes>]: "shortcut" in case the amount and size of the layers in the policy and value nets are the same. Same as dict(vf=int_list, pi=int_list) where int_list is the same for the actor and critic.

.. note:: If a key is not specified or an empty list is passed [], a linear network will be used.

Parameters

  • feature_dim : int — Dimension of the feature vector (can be the output of a CNN)

  • net_arch : list[int] | dict[str, list[int]] — The specification of the policy and value networks. See above for details on its formatting.

  • activation_fn : type[nn.Module] — The activation function to use for the networks.

  • device : torch.device | str — PyTorch device.

Methods

source method MlpExtractor.forward(features: torch.Tensor) → tuple[torch.Tensor, torch.Tensor]

Returns.

latent_policy, latent_value of the specified network. If all layers are shared, then latent_policy == latent_value.

source method MlpExtractor.forward_actor(features: torch.Tensor) → torch.Tensor

source method MlpExtractor.forward_critic(features: torch.Tensor) → torch.Tensor