快捷方式

ConvNet

class torchrl.modules.ConvNet(in_features: int | None = None, depth: int | None = None, num_cells: Sequence[int] | int = None, kernel_sizes: Union[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)[source]

卷積神經網路。

引數:
  • 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) – 用於啟用類的關鍵字引數。也可以傳遞一個長度等於 depth 的關鍵字引數列表,每層一個元素。

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

  • norm_kwargs (dict or list of dicts, optional) – 用於歸一化層的關鍵字引數。也可以傳遞一個長度等於 depth 的關鍵字引數列表,每層一個元素。

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

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

  • aggregator_kwargs (dict, optional) – aggregator_class 的關鍵字引數。

  • 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)[source]

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

引數:

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

forward(inputs: Tensor) Tensor[source]

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

所有子類都應覆蓋此方法。

注意

儘管正向傳播的方法需要在該函式內定義,但後續應呼叫 Module 例項而非直接呼叫此函式,因為前者負責執行註冊的鉤子,而後者會靜默忽略它們。

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源