class_docstr: Configuration for a convolutional transpose head (decoder) network.
  
  The configured Model transforms 1D-observations into an image space.
  The stack of layers is composed of an initial Dense layer, followed by a sequence
  of Conv2DTranspose layers.
  `input_dims` describes the shape of the (1D) input tensor,
  `initial_image_dims` describes the input into the first Conv2DTranspose
  layer, where the translation from `input_dim` to `initial_image_dims` is done
  via the initial Dense layer (w/o activation, w/o layer-norm, and w/ bias).
  
  Beyond that, each layer specified by `cnn_transpose_filter_specifiers`
  is followed by an activation function according to `cnn_transpose_activation`.
  
  `output_dims` is reached after the final Conv2DTranspose layer.
  Not that the last Conv2DTranspose layer is never activated and never layer-norm'd
  regardless of the other settings.
  
  An example for a single conv2d operation is as follows:
  Input "image" is (4, 4, 24) (not yet strided), padding is "same", stride=2,
  kernel=5.
  
  First, the input "image" is strided (with stride=2):
  
  Input image (4x4 (x24)):
  A B C D
  E F G H
  I J K L
  M N O P
  
  Stride with stride=2 -> (7x7 (x24))
  A 0 B 0 C 0 D
  0 0 0 0 0 0 0
  E 0 F 0 G 0 H
  0 0 0 0 0 0 0
  I 0 J 0 K 0 L
  0 0 0 0 0 0 0
  M 0 N 0 O 0 P
  
  Then this strided "image" (strided_size=7x7) is padded (exact padding values will be
  computed by the model):
  
  Padding -> (left=3, right=2, top=3, bottom=2)
  
  0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 A 0 B 0 C 0 D 0 0
  0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 E 0 F 0 G 0 H 0 0
  0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 I 0 J 0 K 0 L 0 0
  0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 M 0 N 0 O 0 P 0 0
  0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0
  
  Then deconvolution with kernel=5 yields an output "image" of 8x8 (x num output
  filters).
  
  Attributes:
      input_dims: The input dimensions of the network. This must be a 1D tensor.
      initial_image_dims: The shape of the input to the first
          Conv2DTranspose layer. We will make sure the input is transformed to
          these dims via a preceding initial Dense layer, followed by a reshape,
          before entering the Conv2DTranspose stack.
      initial_dense_weights_initializer: The initializer function or class to use for
          weight initialization in the initial dense layer. If `None` the default
          initializer of the respective dense layer of a framework (`"torch"` or
          `"tf2"`) is used. Note, all initializers defined in the framework `"tf2`)
          are allowed. For `"torch"` only the in-place initializers, i.e. ending with
          an underscore "_" are allowed.
      initial_dense_weights_initializer_config: Configuration to pass into the
          initializer defined in `initial_dense_weights_initializer`.
      initial_dense_bias_initializer: The initializer function or class to use for
          bias initialization in the initial dense layer. If `None` the default
          initializer of the respective CNN layer of a framework (`"torch"` or `"tf2"`
          ) is used. For `"torch"` only the in-place initializers, i.e. ending with an
          underscore "_" are allowed.
      initial_dense_bias_initializer_config: Configuration to pass into the
          initializer defined in `initial_dense_bias_initializer`.
      cnn_transpose_filter_specifiers: A list of lists, where each element of an inner
          list contains elements of the form
          `[number of channels/filters, [kernel width, kernel height], stride]` to
          specify a convolutional layer stacked in order of the outer list.
      cnn_transpose_use_bias: Whether to use bias on all Conv2DTranspose layers.
      cnn_transpose_activation: The activation function to use after each layer
          (except for the output).
      cnn_transpose_use_layernorm: Whether to insert a LayerNorm functionality
          in between each Conv2DTranspose layer's output and its activation.
      cnn_transpose_kernel_initializer: The initializer function or class to use for
          kernel initialization in the CNN layers. If `None` the default initializer
          of the respective CNN layer of a framework (`"torch"` or `"tf2"`) is used.
          Note, all initializers defined in the framework `"tf2`) are allowed. For
          `"torch"` only the in-place initializers, i.e. ending with an underscore "_"
          are allowed.
      cnn_transpose_kernel_initializer_config: Configuration to pass into the
          initializer defined in `cnn_transpose_kernel_initializer`.
      cnn_transpose_bias_initializer: The initializer function or class to use for
          bias initialization in the CNN layers. If `None` the default initializer of
          the respective CNN layer of a framework (`"torch"` or `"tf2"`) is used.
          For `"torch"` only the in-place initializers, i.e. ending with an underscore
          "_" are allowed.
      cnn_transpose_bias_initializer_config: Configuration to pass into the
          initializer defined in `cnn_transpose_bias_initializer`.
  
  Example:
  .. testcode::
      :skipif: True
  
      # Configuration:
      config = CNNTransposeHeadConfig(
          input_dims=[10],  # 1D input vector (possibly coming from another NN)
          initial_image_dims=[4, 4, 96],  # first image input to deconv stack
          # Initializer for TensorFlow.
          initial_dense_weights_initializer="HeNormal",
          initial_dense_weights_initializer={"seed": 334},
          cnn_transpose_filter_specifiers=[
              [48, [4, 4], 2],
              [24, [4, 4], 2],
              [3, [4, 4], 2],
          ],
          cnn_transpose_activation="silu",  # or "swish", which is the same
          cnn_transpose_use_layernorm=False,
          cnn_use_bias=True,
      )
      model = config.build(framework="torch)
  
      # Resulting stack in pseudocode:
      # Linear(10, 4*4*24)
      # Conv2DTranspose(
      #   in_channels=96, out_channels=48,
      #   kernel_size=[4, 4], stride=2, bias=True,
      # )
      # Swish()
      # Conv2DTranspose(
      #   in_channels=48, out_channels=24,
      #   kernel_size=[4, 4], stride=2, bias=True,
      # )
      # Swish()
      # Conv2DTranspose(
      #   in_channels=24, out_channels=3,
      #   kernel_size=[4, 4], stride=2, bias=True,
      # )
  
  Example:
  .. testcode::
      :skipif: True
  
      # Configuration:
      config = CNNTransposeHeadConfig(
          input_dims=[128],  # 1D input vector (possibly coming from another NN)
          initial_image_dims=[4, 4, 32],  # first image input to deconv stack
          cnn_transpose_filter_specifiers=[
              [16, 4, 2],
              [3, 4, 2],
          ],
          cnn_transpose_activation="relu",
          cnn_transpose_use_layernorm=True,
          cnn_use_bias=False,
          # Initializer for `framework="tf2"`.
          # Note, for Torch only in-place initializers are allowed.
          cnn_transpose_kernel_initializer="xavier_normal_",
          cnn_transpose_kernel_initializer_config={"gain": 0.8},
      )
      model = config.build(framework="torch)
  
      # Resulting stack in pseudocode:
      # Linear(128, 4*4*32, bias=True)  # bias always True for initial dense layer
      # Conv2DTranspose(
      #   in_channels=32, out_channels=16,
      #   kernel_size=[4, 4], stride=2, bias=False,
      # )
      # LayerNorm((-3, -2, -1))  # layer normalize over last 3 axes
      # ReLU()
      # Conv2DTranspose(
      #   in_channels=16, out_channels=3,
      #   kernel_size=[4, 4], stride=2, bias=False,
      # )
class_docstr_tok: ["Configuration","for","a","convolutional","transpose","head","(","decoder",")","network",".","The","configured","Model","transforms","1D-observations","into","an","image","space",".","The","stack","of","layers","is","composed","of","an","initial","Dense","layer",",","followed","by","a","sequence","of","Conv2DTranspose","layers",".","`","input_dims","`","describes","the","shape","of","the","(","1D",")","input","tensor",",","`","initial_image_dims","`","describes","the","input","into","the","first","Conv2DTranspose","layer",",","where","the","translation","from","`","input_dim","`","to","`","initial_image_dims","`","is","done","via","the","initial","Dense","layer","(","w\/o","activation",",","w\/o","layer-norm",",","and","w\/","bias",")",".","Beyond","that",",","each","layer","specified","by","`","cnn_transpose_filter_specifiers","`","is","followed","by","an","activation","function","according","to","`","cnn_transpose_activation","`",".","`","output_dims","`","is","reached","after","the","final","Conv2DTranspose","layer",".","Not","that","the","last","Conv2DTranspose","layer","is","never","activated","and","never","layer-norm'd","regardless","of","the","other","settings",".","An","example","for","a","single","conv2d","operation","is","as","follows",":","Input","``","image","''","is","(","4",",","4",",","24",")","(","not","yet","strided",")",",","padding","is","``","same","''",",","stride=2",",","kernel=5",".","First",",","the","input","``","image","''","is","strided","(","with","stride=2",")",":","Input","image","(","4x4","(","x24",")",")",":","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Stride","with","stride=2","-",">","(","7x7","(","x24",")",")","A","0","B","0","C","0","D","0","0","0","0","0","0","0","E","0","F","0","G","0","H","0","0","0","0","0","0","0","I","0","J","0","K","0","L","0","0","0","0","0","0","0","M","0","N","0","O","0","P","Then","this","strided","``","image","''","(","strided_size=7x7",")","is","padded","(","exact","padding","values","will","be","computed","by","the","model",")",":","Padding","-",">","(","left=3",",","right=2",",","top=3",",","bottom=2",")","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","A","0","B","0","C","0","D","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","E","0","F","0","G","0","H","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","I","0","J","0","K","0","L","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","M","0","N","0","O","0","P","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","Then","deconvolution","with","kernel=5","yields","an","output","``","image","''","of","8x8","(","x","num","output","filters",")",".","Attributes",":","input_dims",":","The","input","dimensions","of","the","network",".","This","must","be","a","1D","tensor",".","initial_image_dims",":","The","shape","of","the","input","to","the","first","Conv2DTranspose","layer",".","We","will","make","sure","the","input","is","transformed","to","these","dims","via","a","preceding","initial","Dense","layer",",","followed","by","a","reshape",",","before","entering","the","Conv2DTranspose","stack",".","initial_dense_weights_initializer",":","The","initializer","function","or","class","to","use","for","weight","initialization","in","the","initial","dense","layer",".","If","`","None","`","the","default","initializer","of","the","respective","dense","layer","of","a","framework","(","`","``","torch","''","`","or","`","``","tf2","''","`",")","is","used",".","Note",",","all","initializers","defined","in","the","framework","`","``","tf2","`",")","are","allowed",".","For","`","``","torch","''","`","only","the","in-place","initializers",",","i.e",".","ending","with","an","underscore","``","_","''","are","allowed",".","initial_dense_weights_initializer_config",":","Configuration","to","pass","into","the","initializer","defined","in","`","initial_dense_weights_initializer","`",".","initial_dense_bias_initializer",":","The","initializer","function","or","class","to","use","for","bias","initialization","in","the","initial","dense","layer",".","If","`","None","`","the","default","initializer","of","the","respective","CNN","layer","of","a","framework","(","`","``","torch","''","`","or","`","``","tf2","''","`",")","is","used",".","For","`","``","torch","''","`","only","the","in-place","initializers",",","i.e",".","ending","with","an","underscore","``","_","''","are","allowed",".","initial_dense_bias_initializer_config",":","Configuration","to","pass","into","the","initializer","defined","in","`","initial_dense_bias_initializer","`",".","cnn_transpose_filter_specifiers",":","A","list","of","lists",",","where","each","element","of","an","inner","list","contains","elements","of","the","form","`","[","number","of","channels\/filters",",","[","kernel","width",",","kernel","height","]",",","stride","]","`","to","specify","a","convolutional","layer","stacked","in","order","of","the","outer","list",".","cnn_transpose_use_bias",":","Whether","to","use","bias","on","all","Conv2DTranspose","layers",".","cnn_transpose_activation",":","The","activation","function","to","use","after","each","layer","(","except","for","the","output",")",".","cnn_transpose_use_layernorm",":","Whether","to","insert","a","LayerNorm","functionality","in","between","each","Conv2DTranspose","layer","'s","output","and","its","activation",".","cnn_transpose_kernel_initializer",":","The","initializer","function","or","class","to","use","for","kernel","initialization","in","the","CNN","layers",".","If","`","None","`","the","default","initializer","of","the","respective","CNN","layer","of","a","framework","(","`","``","torch","''","`","or","`","``","tf2","''","`",")","is","used",".","Note",",","all","initializers","defined","in","the","framework","`","``","tf2","`",")","are","allowed",".","For","`","``","torch","''","`","only","the","in-place","initializers",",","i.e",".","ending","with","an","underscore","``","_","''","are","allowed",".","cnn_transpose_kernel_initializer_config",":","Configuration","to","pass","into","the","initializer","defined","in","`","cnn_transpose_kernel_initializer","`",".","cnn_transpose_bias_initializer",":","The","initializer","function","or","class","to","use","for","bias","initialization","in","the","CNN","layers",".","If","`","None","`","the","default","initializer","of","the","respective","CNN","layer","of","a","framework","(","`","``","torch","''","`","or","`","``","tf2","''","`",")","is","used",".","For","`","``","torch","''","`","only","the","in-place","initializers",",","i.e",".","ending","with","an","underscore","``","_","''","are","allowed",".","cnn_transpose_bias_initializer_config",":","Configuration","to","pass","into","the","initializer","defined","in","`","cnn_transpose_bias_initializer","`",".","Example",":","..","testcode",":",":",":","skipif",":","True","#","Configuration",":","config","=","CNNTransposeHeadConfig","(","input_dims=","[","10","]",",","#","1D","input","vector","(","possibly","coming","from","another","NN",")","initial_image_dims=","[","4",",","4",",","96","]",",","#","first","image","input","to","deconv","stack","#","Initializer","for","TensorFlow",".","initial_dense_weights_initializer=","''","HeNormal","''",",","initial_dense_weights_initializer=","{","``","seed","''",":","334","}",",","cnn_transpose_filter_specifiers=","[","[","48",",","[","4",",","4","]",",","2","]",",","[","24",",","[","4",",","4","]",",","2","]",",","[","3",",","[","4",",","4","]",",","2","]",",","]",",","cnn_transpose_activation=","''","silu","''",",","#","or","``","swish","''",",","which","is","the","same","cnn_transpose_use_layernorm=False",",","cnn_use_bias=True",",",")","model","=","config.build","(","framework=","''","torch",")","#","Resulting","stack","in","pseudocode",":","#","Linear","(","10",",","4","*","4","*","24",")","#","Conv2DTranspose","(","#","in_channels=96",",","out_channels=48",",","#","kernel_size=","[","4",",","4","]",",","stride=2",",","bias=True",",","#",")","#","Swish","(",")","#","Conv2DTranspose","(","#","in_channels=48",",","out_channels=24",",","#","kernel_size=","[","4",",","4","]",",","stride=2",",","bias=True",",","#",")","#","Swish","(",")","#","Conv2DTranspose","(","#","in_channels=24",",","out_channels=3",",","#","kernel_size=","[","4",",","4","]",",","stride=2",",","bias=True",",","#",")","Example",":","..","testcode",":",":",":","skipif",":","True","#","Configuration",":","config","=","CNNTransposeHeadConfig","(","input_dims=","[","128","]",",","#","1D","input","vector","(","possibly","coming","from","another","NN",")","initial_image_dims=","[","4",",","4",",","32","]",",","#","first","image","input","to","deconv","stack","cnn_transpose_filter_specifiers=","[","[","16",",","4",",","2","]",",","[","3",",","4",",","2","]",",","]",",","cnn_transpose_activation=","''","relu","''",",","cnn_transpose_use_layernorm=True",",","cnn_use_bias=False",",","#","Initializer","for","`","framework=","''","tf2","''","`",".","#","Note",",","for","Torch","only","in-place","initializers","are","allowed",".","cnn_transpose_kernel_initializer=","''","xavier_normal_","''",",","cnn_transpose_kernel_initializer_config=","{","``","gain","''",":","0.8","}",",",")","model","=","config.build","(","framework=","''","torch",")","#","Resulting","stack","in","pseudocode",":","#","Linear","(","128",",","4","*","4","*","32",",","bias=True",")","#","bias","always","True","for","initial","dense","layer","#","Conv2DTranspose","(","#","in_channels=32",",","out_channels=16",",","#","kernel_size=","[","4",",","4","]",",","stride=2",",","bias=False",",","#",")","#","LayerNorm","(","(","-3",",","-2",",","-1",")",")","#","layer","normalize","over","last","3","axes","#","ReLU","(",")","#","Conv2DTranspose","(","#","in_channels=16",",","out_channels=3",",","#","kernel_size=","[","4",",","4","]",",","stride=2",",","bias=False",",","#",")"]
code: def output_dims(self):
      # Infer output dims, layer by layer.
      dims = self.initial_image_dims
      for filter_spec in self.cnn_transpose_filter_specifiers:
          # Same padding.
          num_filters, kernel, stride = filter_spec
          # Compute stride output size first (striding is performed first in a
          # conv transpose layer.
          stride_w, stride_h = (
              (stride, stride) if isinstance(stride, int) else stride
          )
          dims = [
              dims[0] * stride_w - (stride_w - 1),
              dims[1] * stride_h - (stride_h - 1),
              num_filters,
          ]
          # TODO (Sven): Support "valid" padding for Conv2DTranspose layers, too.
          #  Analogous to Conv2D Layers in a CNNEncoder.
          # Apply the correct padding. Note that this might be asymetrical, meaning
          # left padding might be != right padding, same for top/bottom.
          _, padding_out_size = same_padding_transpose_after_stride(
              (dims[0], dims[1]), kernel, stride
          )
          # Perform conv transpose operation with the kernel.
          kernel_w, kernel_h = (
              (kernel, kernel) if isinstance(kernel, int) else kernel
          )
          dims = [
              padding_out_size[0] - (kernel_w - 1),
              padding_out_size[1] - (kernel_h - 1),
              num_filters,
          ]
      return tuple(dims)
  
imports: import abc
  from dataclasses import dataclass, field
  import functools
  from typing import Callable, Dict, List, Optional, Tuple, TYPE_CHECKING, Union
  import numpy
  from ray.rllib.models.torch.misc import same_padding, same_padding_transpose_after_stride, valid_padding
  from ray.rllib.models.utils import get_activation_fn, get_initializer_fn
  from ray.rllib.utils.annotations import ExperimentalAPI
label_desc: Use image node_id 1 for calling the CNNTransposeHeadConfig obj's underlying member method code with example usage: obj.output_dims() and returns: tuple