快捷方式

ConvNet

class torchrl.modules.ConvNet(in_features: int | None = None, depth: int | None = None, num_cells: Sequence[int] | int = None, kernel_sizes: Sequence[int] | int = 3, strides: Sequence[int] | int = 1, paddings: Sequence[int] | int = 0, activation_class: type[nn.Module] | Callable = <class 'torch.nn.modules.activation.ELU'>, activation_kwargs: dict | list[dict] | None = None, norm_class: type[nn.Module] | Callable | None = None, norm_kwargs: dict | list[dict] | None = None, bias_last_layer: bool = True, aggregator_class: type[nn.Module] | Callable | None = <class 'torchrl.modules.models.utils.SquashDims'>, aggregator_kwargs: dict | None = None, squeeze_output: bool = False, device: DEVICE_TYPING | None = None)[原始碼]

一個卷積神經網路。

引數:
  • in_features (int, optional) – 輸入特徵的數量。如果為 None,則第一個層使用 LazyConv2d 模組。

  • depth (int, optional) – 網路的深度。深度為 1 將產生一個具有所需輸入大小的單層線性網路,輸出大小等於 num_cells 引數的最後一個元素。如果未指定深度,則深度資訊應包含在 num_cells 引數中(見下文)。如果 num_cells 是一個可迭代物件,並且指定了 depth,則兩者應匹配:len(num_cells) 必須等於 depth

  • num_cells (int or Sequence of int, optional) – 輸入和輸出之間每層的單元格數量。如果提供一個整數,則每層將具有相同數量的單元格。如果提供一個可迭代物件,則線性層的 out_features 將與 num_cells 的內容匹配。預設為 [32, 32, 32]

  • kernel_sizes (int, sequence of int, optional) – 卷積網路的核大小。如果為可迭代物件,則長度必須與由 num_cells 或 depth 引數定義的深度匹配。預設為 3

  • strides (int or sequence of int, optional) – 卷積網路的步幅。如果為可迭代物件,則長度必須與由 num_cells 或 depth 引數定義的深度匹配。預設為 1

  • activation_class (Type[nn.Module] or callable, optional) – 要使用的啟用類或建構函式。預設為 Tanh

  • activation_kwargs (dict or list of dicts, optional) – 要與啟用類一起使用的 kwargs。也可以傳遞一個長度為 depth 的 kwargs 列表,每個層一個元素。

  • norm_class (Type or callable, optional) – 歸一化類或建構函式(如果存在)。

  • norm_kwargs (dict or list of dicts, optional) – 要與歸一化層一起使用的 kwargs。也可以傳遞一個長度為 depth 的 kwargs 列表,每個層一個元素。

  • bias_last_layer (bool) – 如果為 True,最後一個線性層將具有偏置引數。預設為 True

  • aggregator_class (Type[nn.Module] or callable) – 在鏈的末尾使用的聚合器類或建構函式。預設為 torchrl.modules.utils.models.SquashDims

  • aggregator_kwargs (dict, optional) – aggregator_class 的 kwargs。

  • squeeze_output (bool) – 輸出是否應壓縮其單例維度。預設為 False

  • device (torch.device, optional) – 建立模組的裝置。

示例

>>> # All of the following examples provide valid, working MLPs
>>> cnet = ConvNet(in_features=3, depth=1, num_cells=[32,]) # MLP consisting of a single 3 x 6 linear layer
>>> print(cnet)
ConvNet(
  (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))
  (1): ELU(alpha=1.0)
  (2): SquashDims()
)
>>> cnet = ConvNet(in_features=3, depth=4, num_cells=32)
>>> print(cnet)
ConvNet(
  (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))
  (1): ELU(alpha=1.0)
  (2): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1))
  (3): ELU(alpha=1.0)
  (4): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1))
  (5): ELU(alpha=1.0)
  (6): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1))
  (7): ELU(alpha=1.0)
  (8): SquashDims()
)
>>> cnet = ConvNet(in_features=3, num_cells=[32, 33, 34, 35])  # defines the depth by the num_cells arg
>>> print(cnet)
ConvNet(
  (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))
  (1): ELU(alpha=1.0)
  (2): Conv2d(32, 33, kernel_size=(3, 3), stride=(1, 1))
  (3): ELU(alpha=1.0)
  (4): Conv2d(33, 34, kernel_size=(3, 3), stride=(1, 1))
  (5): ELU(alpha=1.0)
  (6): Conv2d(34, 35, kernel_size=(3, 3), stride=(1, 1))
  (7): ELU(alpha=1.0)
  (8): SquashDims()
)
>>> cnet = ConvNet(in_features=3, num_cells=[32, 33, 34, 35], kernel_sizes=[3, 4, 5, (2, 3)])  # defines kernels, possibly rectangular
>>> print(cnet)
ConvNet(
  (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))
  (1): ELU(alpha=1.0)
  (2): Conv2d(32, 33, kernel_size=(4, 4), stride=(1, 1))
  (3): ELU(alpha=1.0)
  (4): Conv2d(33, 34, kernel_size=(5, 5), stride=(1, 1))
  (5): ELU(alpha=1.0)
  (6): Conv2d(34, 35, kernel_size=(2, 3), stride=(1, 1))
  (7): ELU(alpha=1.0)
  (8): SquashDims()
)
classmethod default_atari_dqn(num_actions: int)[原始碼]

返回 seminal DQN 論文中提出的預設 DQN。

引數:

num_actions (int) – Atari 遊戲的動作空間。

forward(inputs: Tensor) Tensor[原始碼]

定義每次呼叫時執行的計算。

所有子類都應重寫此方法。

注意

儘管前向傳播的實現需要在此函式中定義,但您應該在之後呼叫 Module 例項而不是此函式,因為前者會處理註冊的鉤子,而後者則會靜默忽略它們。

文件

訪問全面的 PyTorch 開發者文件

檢視文件

教程

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

檢視教程

資源

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

檢視資源