garage.torch.modules.cnn_module

CNN Module.

class CNNModule(spec, image_format, hidden_channels, *, kernel_sizes, strides, paddings=0, padding_mode='zeros', hidden_nonlinearity=nn.ReLU, hidden_w_init=nn.init.xavier_uniform_, hidden_b_init=nn.init.zeros_, max_pool=False, pool_shape=None, pool_stride=1, layer_normalization=False, enable_cudnn_benchmarks=True)

Bases: torch.nn.Module

Inheritance diagram of garage.torch.modules.cnn_module.CNNModule

Convolutional neural network (CNN) model in pytorch.

Parameters
  • spec (garage.InOutSpec) – Specification of inputs and outputs. The input should be in ‘NCHW’ format: [batch_size, channel, height, width]. Will print a warning if the channel size is not 1 or 3. If output_space is specified, then a final linear layer will be inserted to map to that dimensionality. If output_space is None, it will be filled in with the computed output space.

  • image_format (str) – Either ‘NCHW’ or ‘NHWC’. Should match the input specification. Gym uses NHWC by default, but PyTorch uses NCHW by default.

  • hidden_channels (tuple[int]) – Number of output channels for CNN. For example, (3, 32) means there are two convolutional layers. The filter for the first conv layer outputs 3 channels and the second one outputs 32 channels.

  • kernel_sizes (tuple[int]) – Dimension of the conv filters. For example, (3, 5) means there are two convolutional layers. The filter for first layer is of dimension (3 x 3) and the second one is of dimension (5 x 5).

  • strides (tuple[int]) – The stride of the sliding window. For example, (1, 2) means there are two convolutional layers. The stride of the filter for first layer is 1 and that of the second layer is 2.

  • paddings (tuple[int]) – Amount of zero-padding added to both sides of the input of a conv layer.

  • padding_mode (str) – The type of padding algorithm to use, i.e. ‘constant’, ‘reflect’, ‘replicate’ or ‘circular’ and by default is ‘zeros’.

  • hidden_nonlinearity (callable or torch.nn.Module) – Activation function for intermediate dense layer(s). It should return a torch.Tensor. Set it to None to maintain a linear activation.

  • hidden_b_init (callable) – Initializer function for the bias of intermediate dense layer(s). The function should return a torch.Tensor.

  • max_pool (bool) – Bool for using max-pooling or not.

  • pool_shape (tuple[int]) – Dimension of the pooling layer(s). For example, (2, 2) means that all pooling layers are of the same shape (2, 2).

  • pool_stride (tuple[int]) – The strides of the pooling layer(s). For example, (2, 2) means that all the pooling layers have strides (2, 2).

  • layer_normalization (bool) – Bool for using layer normalization or not.

  • hidden_w_init (callable) – Initializer function for the weight of intermediate dense layer(s). The function should return a torch.Tensor.

  • enable_cudnn_benchmarks (bool) – Whether to enable cudnn benchmarks in torch. If enabled, the backend selects the CNN benchamark algorithm with the best performance.

forward(x)

Forward method.

Parameters

x (torch.Tensor) – Input values. Should match image_format specified at construction (either NCHW or NCWH).

Returns

Output values

Return type

List[torch.Tensor]