ConvNet¶
- class torchrl.modules.ConvNet(in_features: ~typing.Optional[int] = None, depth: ~typing.Optional[int] = None, num_cells: ~typing.Optional[~typing.Union[~typing.Sequence[int], int]] = None, kernel_sizes: ~typing.Union[~typing.Sequence[int], int] = 3, strides: ~typing.Union[~typing.Sequence[int], int] = 1, paddings: ~typing.Union[~typing.Sequence[int], int] = 0, activation_class: ~typing.Union[~typing.Type[~torch.nn.modules.module.Module], ~typing.Callable] = <class 'torch.nn.modules.activation.ELU'>, 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, bias_last_layer: bool = True, aggregator_class: ~typing.Optional[~typing.Union[~typing.Type[~torch.nn.modules.module.Module], ~typing.Callable]] = <class 'torchrl.modules.models.utils.SquashDims'>, aggregator_kwargs: ~typing.Optional[dict] = None, squeeze_output: bool = False, device: ~typing.Optional[~typing.Union[~torch.device, str, int]] = 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 或 int 序列, optional) – 输入和输出之间每层的单元数。如果提供整数,则每层将具有相同数量的单元。如果提供可迭代对象,则线性层
out_features
将与 num_cells 的内容匹配。默认为[32, 32, 32]
。kernel_sizes (int, int 序列, optional) – 卷积网络的内核大小。如果可迭代,则长度必须与深度匹配,深度由
num_cells
或 depth 参数定义。默认为3
。strides (int 或 int 序列, optional) – 卷积网络的步幅。如果可迭代,则长度必须与深度匹配,深度由
num_cells
或 depth 参数定义。默认为1
。activation_class (Type[nn.Module] 或 callable, optional) – 要使用的激活类或构造函数。默认为
Tanh
。activation_kwargs (dict 或 dicts 列表, optional) – 与激活类一起使用的 kwargs。也可以传递长度为
depth
的 kwargs 列表,每层一个元素。norm_class (Type 或 callable, optional) – 归一化类或构造函数(如果有)。
norm_kwargs (dict 或 dicts 列表, optional) – 与归一化层一起使用的 kwargs。也可以传递长度为
depth
的 kwargs 列表,每层一个元素。bias_last_layer (bool) – 如果为
True
,则最后一个线性层将具有偏置参数。默认为True
。aggregator_class (Type[nn.Module] 或 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() )