快捷方式

MLP

class torchrl.modules.MLP(in_features: ~typing.Optional[int] = None, out_features: ~typing.Optional[~typing.Union[int, ~torch.Size]] = None, depth: ~typing.Optional[int] = None, num_cells: ~typing.Optional[~typing.Union[~typing.Sequence[int], int]] = None, activation_class: ~typing.Union[~typing.Type[~torch.nn.modules.module.Module], ~typing.Callable] = <class 'torch.nn.modules.activation.Tanh'>, activation_kwargs: ~typing.Optional[~typing.Union[dict, ~typing.List[dict]]] = None, norm_class: ~typing.Optional[~typing.Union[~typing.Type[~torch.nn.modules.module.Module], ~typing.Callable]] = None, norm_kwargs: ~typing.Optional[~typing.Union[dict, ~typing.List[dict]]] = None, dropout: ~typing.Optional[float] = None, bias_last_layer: bool = True, single_bias_last_layer: bool = False, layer_class: ~typing.Union[~typing.Type[~torch.nn.modules.module.Module], ~typing.Callable] = <class 'torch.nn.modules.linear.Linear'>, layer_kwargs: ~typing.Optional[dict] = None, activate_last_layer: bool = False, device: ~typing.Optional[~typing.Union[~torch.device, str, int]] = 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

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

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

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

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

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

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

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

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

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

  • layer_kwargs (dictdicts 列表, 可选) – 线性层的 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 的全面开发者文档

查看文档

教程

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

查看教程

资源

查找开发资源并获得解答

查看资源