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)[源代码]¶
一个多层感知机。
如果 MLP 接收多个输入,它会将它们全部沿最后一个维度连接起来,然后再将结果张量传递给网络。这是为了方便调用时能无缝处理,例如:
>>> model(state, action) # compute state-action value
将来,此功能可能会移至 ProbabilisticTDModule,尽管这样它将需要处理不同的情况(向量、图像等)
- 参数:
in_features (int, optional) – 输入特征的数量;
out_features (int, torch.Size 或 等价形式) – 输出特征的数量。如果为整数的可迭代对象,则输出将被重塑为所需的形状。
depth (int, optional) – 网络的深度。深度为 0 将生成一个具有所需输入和输出大小的单个线性层网络。长度为 1 将创建 2 个线性层,依此类推。如果未指定深度,则深度信息应包含在
num_cells
参数中(见下文)。如果num_cells
是可迭代对象且指定了深度,两者应匹配:len(num_cells)
必须等于depth
。默认为0
(无深度 - 网络包含单个线性层)。num_cells (int 或 整数序列, optional) – 输入和输出之间的每层的单元数。如果提供整数,则每层将具有相同的单元数。如果提供可迭代对象,则线性层的
out_features
将与num_cells
的内容匹配。默认为32
;activation_class (Type[nn.Module] 或 callable, optional) – 要使用的激活类或构造函数。默认为
Tanh
。activation_kwargs (dict 或 dict 列表, optional) – 要与激活类一起使用的 kwargs。也接受长度为
depth + int(activate_last_layer)
的 kwargs 列表。norm_class (Type 或 callable, optional) – 归一化类或构造函数(如果有)。
norm_kwargs (dict 或 dict 列表, optional) – 要与归一化层一起使用的 kwargs。也接受长度为
depth + int(activate_last_layer)
的 kwargs 列表。dropout (
float
, optional) – dropout 概率。默认为None
(无 dropout);bias_last_layer (bool) – 如果为
True
,则最后一个 Linear 层将具有 bias 参数。默认值:True;single_bias_last_layer (bool) – 如果为
True
,则最后一个层的 bias 的最后一个维度将是一个单一维度。默认值:True;layer_class (Type[nn.Module] 或 callable, optional) – 要用于线性层的类;
layer_kwargs (dict 或 dict 列表, 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 [源代码]¶
定义每次调用时执行的计算。
所有子类都应重写此方法。
注意
虽然 forward pass 的实现需要在此函数中定义,但应该之后调用
Module
实例而不是这个函数,因为前者会处理已注册钩子,而后者会默默忽略它们。