快捷方式

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 開發者文件

檢視文件

教程

為初學者和高階開發者提供深入的教程

檢視教程

資源

查詢開發資源並讓您的問題得到解答

檢視資源