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)[來源]¶
多層感知器。
如果 MLP 收到多個輸入,它會沿著最後一個維度將它們全部串連起來,然後將產生的張量傳遞到網路中。這是為了允許與以下類型的呼叫進行無縫介面:
>>> model(state, action) # compute state-action value
在未來,此功能可能會移至 ProbabilisticTDModule,儘管這需要它處理不同的情況(向量、圖像等)。
- 參數:
in_features (int, optional) – 輸入特徵的數量;
out_features (int, torch.Size 或 equivalent) – 輸出特徵的數量。 如果是整數的可迭代物件,則輸出會重新塑造成所需的形狀。
depth (int, optional) – 網路的深度。深度為 0 將產生一個具有所需輸入和輸出大小的單個線性層網路。長度為 1 將建立 2 個線性層,依此類推。如果未指示深度,則深度資訊應包含在
num_cells
參數中(請參閱下文)。如果num_cells
是一個可迭代物件且已指示深度,則兩者應匹配:len(num_cells)
必須等於depth
。num_cells (int 或 int 的序列, optional) – 輸入和輸出之間每層的 cell 數量。 如果提供整數,則每一層將具有相同數量的 cell。 如果提供可迭代物件,則線性層的
out_features
將與num_cells
的內容匹配。預設值為32
;activation_class (Type[nn.Module] 或 callable, optional) – 要使用的啟動類別或建構函式。預設值為
Tanh
。activation_kwargs (dict 或 dicts 的列表, optional) – 要與啟動類別一起使用的 kwargs。也接受長度為
depth + int(activate_last_layer)
的 kwargs 列表。norm_class (Type 或 callable, optional) – 正規化類別或建構函式(如果有的話)。
norm_kwargs (dict 或 dicts 的列表, optional) – 要與正規化層一起使用的 kwargs。也接受長度為
depth + int(activate_last_layer)
的 kwargs 列表。dropout (float, optional) – dropout 概率。預設值為
None
(無 dropout);bias_last_layer (bool) – 如果
True
,則最後一個線性層將具有一個 bias 參數。預設值:True;single_bias_last_layer (bool) – 如果
True
,則最後一層 bias 的最後一個維度將是一個單例維度。預設值:True;layer_class (Type[nn.Module] 或 callable, optional) – 用於線性層的類別;
layer_kwargs (dict 或 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) )