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]] = None, kernel_sizes: ~typing.Union[~typing.Sequence[~typing.Union[int, ~typing.Sequence[int]]], int] = 3, strides: ~typing.Union[~typing.Sequence, int] = 1, paddings: ~typing.Union[~typing.Sequence, int] = 0, activation_class: ~typing.Type[~torch.nn.modules.module.Module] = <class 'torch.nn.modules.activation.ELU'>, activation_kwargs: ~typing.Optional[dict] = None, norm_class: ~typing.Optional[~typing.Type[~torch.nn.modules.module.Module]] = None, norm_kwargs: ~typing.Optional[dict] = None, bias_last_layer: bool = True, aggregator_class: ~typing.Optional[~typing.Type[~torch.nn.modules.module.Module]] = <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]¶
A convolutional neural network.
- Parameters:
in_features (int, optional) – number of input features;
depth (int, optional) – depth of the network. A depth of 1 will produce a single linear layer network with the desired input size, and with an output size equal to the last element of the num_cells argument. If no depth is indicated, the depth information should be contained in the num_cells argument (see below). If num_cells is an iterable and depth is indicated, both should match: len(num_cells) must be equal to the depth.
num_cells (int or Sequence[int], optional) – number of cells of every layer in between the input and output. If an integer is provided, every layer will have the same number of cells. If an iterable is provided, the linear layers out_features will match the content of num_cells. default: [32, 32, 32];
kernel_sizes (int, Sequence[Union[int, Sequence[int]]]) – Kernel size(s) of the conv network. If iterable, the length must match the depth, defined by the num_cells or depth arguments.
strides (int or Sequence[int]) – Stride(s) of the conv network. If iterable, the length must match the depth, defined by the num_cells or depth arguments.
activation_class (Type[nn.Module]) – activation class to be used. default: nn.Tanh
activation_kwargs (dict, optional) – kwargs to be used with the activation class;
norm_class (Type, optional) – normalization class, if any;
norm_kwargs (dict, optional) – kwargs to be used with the normalization layers;
bias_last_layer (bool) – if
True
, the last Linear layer will have a bias parameter. default: True;aggregator_class (Type[nn.Module]) – aggregator to use at the end of the chain. default: SquashDims;
aggregator_kwargs (dict, optional) – kwargs for the aggregator_class;
squeeze_output (bool) – whether the output should be squeezed of its singleton dimensions. default: False.
device (Optional[DEVICE_TYPING]) – device to create the module on.
Examples
>>> # 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() )
- forward(inputs: Tensor) Tensor [source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.