快捷方式

MLP

class torchrl.modules.MLP(in_features: int | None = None, out_features: int | torch.Size = None, depth: int | None = None, num_cells: Sequence[int] | int | None = None, activation_class: Type[nn.Module] | Callable = <class 'torch.nn.modules.activation.Tanh'>, activation_kwargs: dict | List[dict] | None = None, norm_class: Type[nn.Module] | Callable | None = None, norm_kwargs: dict | List[dict] | None = None, dropout: float | None = None, bias_last_layer: bool = True, single_bias_last_layer: bool = False, layer_class: Type[nn.Module] | Callable = <class 'torch.nn.modules.linear.Linear'>, layer_kwargs: dict | None = None, activate_last_layer: bool = False, device: DEVICE_TYPING | None = None)[source]

一个多层感知机。

如果 MLP 接收多个输入,它会在将结果张量传递到网络之前,沿着最后一个维度将它们全部串联起来。这旨在实现与以下类型调用的无缝接口:

>>> model(state, action)  # compute state-action value

将来,此功能可能会移至 ProbabilisticTDModule,尽管这需要它处理不同的情况(向量、图像等)。

参数:
  • in_features (int, 可选) – 输入特征的数量;

  • out_features (int, torch.Size等效类型) – 输出特征的数量。如果为整数的可迭代对象,则输出将被重塑为所需的形状。

  • depth (int, 可选) – 网络的深度。深度为 0 将生成一个具有所需输入和输出大小的单个线性层网络。深度为 1 将创建 2 个线性层等。如果未指定深度,则深度信息应包含在 num_cells 参数中(见下文)。如果 num_cells 是可迭代的且指定了深度,则两者应匹配:len(num_cells) 必须等于 depth。默认为 0(无深度 - 网络包含一个单个线性层)。

  • num_cells (intint 序列, 可选) – 输入和输出之间每一层的单元格数量。如果提供整数,则每一层将具有相同数量的单元格。如果提供可迭代对象,则线性层的 out_features 将匹配 num_cells 的内容。默认为 32

  • activation_class (Type[nn.Module] 或 callable, 可选) – 要使用的激活类或构造函数。默认为 Tanh

  • activation_kwargs (dictdict 列表, 可选) – 与激活类一起使用的 kwargs。也接受长度为 depth + int(activate_last_layer) 的 kwargs 列表。

  • norm_class (Typecallable, 可选) – 归一化类或构造函数(如果有)。

  • norm_kwargs (dictdict 列表, 可选) – 与归一化层一起使用的 kwargs。也接受长度为 depth + int(activate_last_layer) 的 kwargs 列表。

  • dropout (float, 可选) – dropout 概率。默认为 None(无 dropout);

  • bias_last_layer (bool) – 如果为 True,则最后一个 Linear 层将具有偏置参数。默认值:True;

  • single_bias_last_layer (bool) – 如果为 True,则最后一层的偏置的最后一个维度将是单例维度。默认值:True;

  • layer_class (Type[nn.Module] 或 callable, 可选) – 用于线性层的类;

  • layer_kwargs (dictdict 列表, 可选) – 线性层的 kwargs。也接受长度为 depth + 1 的 kwargs 列表。

  • activate_last_layer (bool) – 是否应激活 MLP 输出。当 MLP 输出用作另一个模块的输入时,这很有用。默认值:False。

  • device (torch.device, 可选) – 创建模块时使用的设备。

示例

>>> # All of the following examples provide valid, working MLPs
>>> mlp = MLP(in_features=3, out_features=6, depth=0) # MLP consisting of a single 3 x 6 linear layer
>>> print(mlp)
MLP(
  (0): Linear(in_features=3, out_features=6, bias=True)
)
>>> mlp = MLP(in_features=3, out_features=6, depth=4, num_cells=32)
>>> print(mlp)
MLP(
  (0): Linear(in_features=3, out_features=32, bias=True)
  (1): Tanh()
  (2): Linear(in_features=32, out_features=32, bias=True)
  (3): Tanh()
  (4): Linear(in_features=32, out_features=32, bias=True)
  (5): Tanh()
  (6): Linear(in_features=32, out_features=32, bias=True)
  (7): Tanh()
  (8): Linear(in_features=32, out_features=6, bias=True)
)
>>> mlp = MLP(out_features=6, depth=4, num_cells=32)  # LazyLinear for the first layer
>>> print(mlp)
MLP(
  (0): LazyLinear(in_features=0, out_features=32, bias=True)
  (1): Tanh()
  (2): Linear(in_features=32, out_features=32, bias=True)
  (3): Tanh()
  (4): Linear(in_features=32, out_features=32, bias=True)
  (5): Tanh()
  (6): Linear(in_features=32, out_features=32, bias=True)
  (7): Tanh()
  (8): Linear(in_features=32, out_features=6, bias=True)
)
>>> mlp = MLP(out_features=6, num_cells=[32, 33, 34, 35])  # defines the depth by the num_cells arg
>>> print(mlp)
MLP(
  (0): LazyLinear(in_features=0, out_features=32, bias=True)
  (1): Tanh()
  (2): Linear(in_features=32, out_features=33, bias=True)
  (3): Tanh()
  (4): Linear(in_features=33, out_features=34, bias=True)
  (5): Tanh()
  (6): Linear(in_features=34, out_features=35, bias=True)
  (7): Tanh()
  (8): Linear(in_features=35, out_features=6, bias=True)
)
>>> mlp = MLP(out_features=(6, 7), num_cells=[32, 33, 34, 35])  # returns a view of the output tensor with shape [*, 6, 7]
>>> print(mlp)
MLP(
  (0): LazyLinear(in_features=0, out_features=32, bias=True)
  (1): Tanh()
  (2): Linear(in_features=32, out_features=33, bias=True)
  (3): Tanh()
  (4): Linear(in_features=33, out_features=34, bias=True)
  (5): Tanh()
  (6): Linear(in_features=34, out_features=35, bias=True)
  (7): Tanh()
  (8): Linear(in_features=35, out_features=42, bias=True)
)
>>> from torchrl.modules import NoisyLinear
>>> mlp = MLP(out_features=(6, 7), num_cells=[32, 33, 34, 35], layer_class=NoisyLinear)  # uses NoisyLinear layers
>>> print(mlp)
MLP(
  (0): NoisyLazyLinear(in_features=0, out_features=32, bias=False)
  (1): Tanh()
  (2): NoisyLinear(in_features=32, out_features=33, bias=True)
  (3): Tanh()
  (4): NoisyLinear(in_features=33, out_features=34, bias=True)
  (5): Tanh()
  (6): NoisyLinear(in_features=34, out_features=35, bias=True)
  (7): Tanh()
  (8): NoisyLinear(in_features=35, out_features=42, bias=True)
)
forward(*inputs: Tuple[Tensor]) Tensor[source]

定义每次调用时执行的计算。

应由所有子类覆盖。

注意

尽管前向传播的实现需要在本函数内定义,但之后应该调用 Module 实例而不是本函数,因为前者负责运行已注册的钩子,而后者会默默忽略它们。

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获得问题解答

查看资源