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
。num_cells (int 或 int 序列, 可选) – 输入和输出之间每一层的单元格数量。如果提供整数,则每一层将具有相同数量的单元格。如果提供迭代器,则线性层
out_features
将与num_cells
的内容匹配。默认为32
;activation_class (Type[nn.Module] 或 可调用, 可选) – 要使用的激活类或构造函数。默认为
Tanh
.activation_kwargs (dict 或 dict 列表, 可选) – 要与激活类一起使用的 kwargs。还可以接受长度为
depth + int(activate_last_layer)
的 kwargs 列表。norm_class (Type 或 可调用, 可选) – 正则化类或构造函数(如果有)。
norm_kwargs (dict 或 dict 列表, 可选) – 要与正则化层一起使用的 kwargs。还可以接受长度为
depth + int(activate_last_layer)
的 kwargs 列表。dropout (float, 可选) – 丢弃概率。默认为
None
(不丢弃);bias_last_layer (bool) – 如果为
True
,则最后一个线性层将具有偏差参数。默认值:True;single_bias_last_layer (bool) – 如果为
True
,则最后一层的偏差的最后一个维度将是单例维度。默认值:True;layer_class (Type[nn.Module] 或 可调用, 可选) – 要用于线性层的类;
layer_kwargs (dict 或 dict 列表, 可选) – 线性层的 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) )