快捷方式

MLP

class torchrl.modules.MLP(in_features: int | None = None, out_features: int | torch.Size | None = 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)[源码]

多层感知机(Multi-layer perceptron)。

如果 MLP 接收多个输入,它会将它们沿最后一个维度全部拼接起来,然后将结果张量传递给网络。这样设计是为了能够无缝地与以下类型的调用进行接口:

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

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

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

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

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

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

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

  • activation_kwargs (dict or list of dicts, optional) – 要与激活类一起使用的 kwargs。也接受长度为 depth + int(activate_last_layer) 的 kwargs 列表。

  • norm_class (Type or callable, optional) – 归一化类或构造函数(如果存在)。

  • norm_kwargs (dict or list of dicts, optional) – 要与归一化层一起使用的 kwargs。也接受长度为 depth + int(activate_last_layer) 的 kwargs 列表。

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

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

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

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

  • layer_kwargs (dict or list of dicts, optional) – 线性层的 kwargs。也接受长度为 depth + 1 的 kwargs 列表。

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

  • device (torch.device, optional) – 创建模块的设备。

示例

>>> # 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[torch.Tensor]) Tensor[源码]

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

所有子类都应重写此方法。

注意

尽管前向传播的实现需要在此函数中定义,但您应该在之后调用 Module 实例而不是此函数,因为前者会处理注册的钩子,而后者则会静默忽略它们。

文档

访问全面的 PyTorch 开发者文档

查看文档

教程

为初学者和高级开发者提供深入的教程

查看教程

资源

查找开发资源并让您的问题得到解答

查看资源