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 (int 或 int 序列, 可選) – 輸入和輸出之間每一層的單元格數量。如果提供整數,則每一層將具有相同數量的單元格。如果提供可迭代物件,則線性層的
out_features將匹配num_cells的內容。預設為32;activation_class (Type[nn.Module] 或 callable, 可選) – 要使用的啟用類或建構函式。預設為
Tanh。activation_kwargs (dict 或 dict 列表, 可選) – 與啟用類一起使用的 kwargs。也接受長度為
depth + int(activate_last_layer)的 kwargs 列表。norm_class (Type 或 callable, 可選) – 歸一化類或建構函式(如果有)。
norm_kwargs (dict 或 dict 列表, 可選) – 與歸一化層一起使用的 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 (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) )