class_docstr: A multi-layer perceptron with N dense layers.
  
  All layers (except for an optional additional extra output layer) share the same
  activation function, bias setup (use bias or not), and LayerNorm setup
  (use layer normalization or not).
  
  If `output_dim` (int) is not None, an additional, extra output dense layer is added,
  which might have its own activation function (e.g. "linear"). However, the output
  layer does NOT use layer normalization.
class_docstr_tok: ["A","multi-layer","perceptron","with","N","dense","layers",".","All","layers","(","except","for","an","optional","additional","extra","output","layer",")","share","the","same","activation","function",",","bias","setup","(","use","bias","or","not",")",",","and","LayerNorm","setup","(","use","layer","normalization","or","not",")",".","If","`","output_dim","`","(","int",")","is","not","None",",","an","additional",",","extra","output","dense","layer","is","added",",","which","might","have","its","own","activation","function","(","e.g",".","``","linear","''",")",".","However",",","the","output","layer","does","NOT","use","layer","normalization","."]
docstr: Initialize a TfMLP object.
  
  Args:
      input_dim: The input dimension of the network. Must not be None.
      hidden_layer_dims: The sizes of the hidden layers. If an empty list, only a
          single layer will be built of size `output_dim`.
      hidden_layer_use_layernorm: Whether to insert a LayerNormalization
          functionality in between each hidden layer's output and its activation.
      hidden_layer_use_bias: Whether to use bias on all dense layers (excluding
          the possible separate output layer).
      hidden_layer_activation: The activation function to use after each layer
          (except for the output). Either a tf.nn.[activation fn] callable or a
          string that's supported by tf.keras.layers.Activation(activation=...),
          e.g. "relu", "ReLU", "silu", or "linear".
      hidden_layer_weights_initializer: The initializer function or class to use
          for weights initialization in the hidden layers. If `None` the default
          initializer of the respective dense layer is used. Note, all
          initializers defined in `tf.keras.initializers` are allowed.
      hidden_layer_weights_initializer_config: Configuration to pass into the
          initializer defined in `hidden_layer_weights_initializer`.
      hidden_layer_bias_initializer: The initializer function or class to use for
          bias initialization in the hidden layers. If `None` the default
          initializer of the respective dense layer is used. Note, all
          initializers defined in `tf.keras.initializers` are allowed.
      hidden_layer_bias_initializer_config: Configuration to pass into the
          initializer defined in `hidden_layer_bias_initializer`.
      output_dim: The output dimension of the network. If None, no specific output
          layer will be added and the last layer in the stack will have
          size=`hidden_layer_dims[-1]`.
      output_use_bias: Whether to use bias on the separate output layer,
          if any.
      output_activation: The activation function to use for the output layer
          (if any). Either a tf.nn.[activation fn] callable or a string that's
          supported by tf.keras.layers.Activation(activation=...), e.g. "relu",
          "ReLU", "silu", or "linear".
      output_layer_weights_initializer: The initializer function or class to use
          for weights initialization in the output layers. If `None` the default
          initializer of the respective dense layer is used. Note, all
          initializers defined in `tf.keras.initializers` are allowed.
      output_layer_weights_initializer_config: Configuration to pass into the
          initializer defined in `output_layer_weights_initializer`.
      output_layer_bias_initializer: The initializer function or class to use for
          bias initialization in the output layers. If `None` the default
          initializer of the respective dense layer is used. Note, all
          initializers defined in `tf.keras.initializers` are allowed.
      output_layer_bias_initializer_config: Configuration to pass into the
          initializer defined in `output_layer_bias_initializer`.
docstr_tok: ["Initialize","a","TfMLP","object",".","Args",":","input_dim",":","The","input","dimension","of","the","network",".","Must","not","be","None",".","hidden_layer_dims",":","The","sizes","of","the","hidden","layers",".","If","an","empty","list",",","only","a","single","layer","will","be","built","of","size","`","output_dim","`",".","hidden_layer_use_layernorm",":","Whether","to","insert","a","LayerNormalization","functionality","in","between","each","hidden","layer","'s","output","and","its","activation",".","hidden_layer_use_bias",":","Whether","to","use","bias","on","all","dense","layers","(","excluding","the","possible","separate","output","layer",")",".","hidden_layer_activation",":","The","activation","function","to","use","after","each","layer","(","except","for","the","output",")",".","Either","a","tf.nn",".","[","activation","fn","]","callable","or","a","string","that","'s","supported","by","tf.keras.layers.Activation","(","activation=","...",")",",","e.g",".","``","relu","''",",","``","ReLU","''",",","``","silu","''",",","or","``","linear","''",".","hidden_layer_weights_initializer",":","The","initializer","function","or","class","to","use","for","weights","initialization","in","the","hidden","layers",".","If","`","None","`","the","default","initializer","of","the","respective","dense","layer","is","used",".","Note",",","all","initializers","defined","in","`","tf.keras.initializers","`","are","allowed",".","hidden_layer_weights_initializer_config",":","Configuration","to","pass","into","the","initializer","defined","in","`","hidden_layer_weights_initializer","`",".","hidden_layer_bias_initializer",":","The","initializer","function","or","class","to","use","for","bias","initialization","in","the","hidden","layers",".","If","`","None","`","the","default","initializer","of","the","respective","dense","layer","is","used",".","Note",",","all","initializers","defined","in","`","tf.keras.initializers","`","are","allowed",".","hidden_layer_bias_initializer_config",":","Configuration","to","pass","into","the","initializer","defined","in","`","hidden_layer_bias_initializer","`",".","output_dim",":","The","output","dimension","of","the","network",".","If","None",",","no","specific","output","layer","will","be","added","and","the","last","layer","in","the","stack","will","have","size=","`","hidden_layer_dims","[","-1","]","`",".","output_use_bias",":","Whether","to","use","bias","on","the","separate","output","layer",",","if","any",".","output_activation",":","The","activation","function","to","use","for","the","output","layer","(","if","any",")",".","Either","a","tf.nn",".","[","activation","fn","]","callable","or","a","string","that's","supported","by","tf.keras.layers.Activation","(","activation=","...",")",",","e.g",".","``","relu","''",",","``","ReLU","''",",","``","silu","''",",","or","``","linear","''",".","output_layer_weights_initializer",":","The","initializer","function","or","class","to","use","for","weights","initialization","in","the","output","layers",".","If","`","None","`","the","default","initializer","of","the","respective","dense","layer","is","used",".","Note",",","all","initializers","defined","in","`","tf.keras.initializers","`","are","allowed",".","output_layer_weights_initializer_config",":","Configuration","to","pass","into","the","initializer","defined","in","`","output_layer_weights_initializer","`",".","output_layer_bias_initializer",":","The","initializer","function","or","class","to","use","for","bias","initialization","in","the","output","layers",".","If","`","None","`","the","default","initializer","of","the","respective","dense","layer","is","used",".","Note",",","all","initializers","defined","in","`","tf.keras.initializers","`","are","allowed",".","output_layer_bias_initializer_config",":","Configuration","to","pass","into","the","initializer","defined","in","`","output_layer_bias_initializer","`","."]
code: def __init__(
      self,
      *,
      input_dim: int,
      hidden_layer_dims: List[int],
      hidden_layer_use_layernorm: bool = False,
      hidden_layer_use_bias: bool = True,
      hidden_layer_activation: Optional[Union[str, Callable]] = "relu",
      hidden_layer_weights_initializer: Optional[
          Union[str, Callable]
      ] = None,
      hidden_layer_weights_initializer_config: Optional[Dict] = None,
      hidden_layer_bias_initializer: Optional[
          Union[str, Callable]
      ] = None,
      hidden_layer_bias_initializer_config: Optional[Dict] = None,
      output_dim: Optional[int] = None,
      output_use_bias: bool = True,
      output_activation: Optional[Union[str, Callable]] = "linear",
      output_weights_initializer: Optional[Union[str, Callable]] = None,
      output_weights_initializer_config: Optional[Dict] = None,
      output_bias_initializer: Optional[Union[str, Callable]] = None,
      output_bias_initializer_config: Optional[Dict] = None,
  ):
      """Initialize a TfMLP object.
  
      Args:
          input_dim: The input dimension of the network. Must not be None.
          hidden_layer_dims: The sizes of the hidden layers. If an empty list, only a
              single layer will be built of size `output_dim`.
          hidden_layer_use_layernorm: Whether to insert a LayerNormalization
              functionality in between each hidden layer's output and its activation.
          hidden_layer_use_bias: Whether to use bias on all dense layers (excluding
              the possible separate output layer).
          hidden_layer_activation: The activation function to use after each layer
              (except for the output). Either a tf.nn.[activation fn] callable or a
              string that's supported by tf.keras.layers.Activation(activation=...),
              e.g. "relu", "ReLU", "silu", or "linear".
          hidden_layer_weights_initializer: The initializer function or class to use
              for weights initialization in the hidden layers. If `None` the default
              initializer of the respective dense layer is used. Note, all
              initializers defined in `tf.keras.initializers` are allowed.
          hidden_layer_weights_initializer_config: Configuration to pass into the
              initializer defined in `hidden_layer_weights_initializer`.
          hidden_layer_bias_initializer: The initializer function or class to use for
              bias initialization in the hidden layers. If `None` the default
              initializer of the respective dense layer is used. Note, all
              initializers defined in `tf.keras.initializers` are allowed.
          hidden_layer_bias_initializer_config: Configuration to pass into the
              initializer defined in `hidden_layer_bias_initializer`.
          output_dim: The output dimension of the network. If None, no specific output
              layer will be added and the last layer in the stack will have
              size=`hidden_layer_dims[-1]`.
          output_use_bias: Whether to use bias on the separate output layer,
              if any.
          output_activation: The activation function to use for the output layer
              (if any). Either a tf.nn.[activation fn] callable or a string that's
              supported by tf.keras.layers.Activation(activation=...), e.g. "relu",
              "ReLU", "silu", or "linear".
          output_layer_weights_initializer: The initializer function or class to use
              for weights initialization in the output layers. If `None` the default
              initializer of the respective dense layer is used. Note, all
              initializers defined in `tf.keras.initializers` are allowed.
          output_layer_weights_initializer_config: Configuration to pass into the
              initializer defined in `output_layer_weights_initializer`.
          output_layer_bias_initializer: The initializer function or class to use for
              bias initialization in the output layers. If `None` the default
              initializer of the respective dense layer is used. Note, all
              initializers defined in `tf.keras.initializers` are allowed.
          output_layer_bias_initializer_config: Configuration to pass into the
              initializer defined in `output_layer_bias_initializer`.
      """
      super().__init__()
      assert input_dim > 0
  
      layers = []
      # Input layer.
      layers.append(tf.keras.Input(shape=(input_dim,)))
  
      hidden_activation = get_activation_fn(
          hidden_layer_activation, framework="tf2"
      )
      hidden_weights_initializer = get_initializer_fn(
          hidden_layer_weights_initializer, framework="tf2"
      )
      hidden_bias_initializer = get_initializer_fn(
          hidden_layer_bias_initializer, framework="tf2"
      )
  
      for i in range(len(hidden_layer_dims)):
          # Dense layer with activation (or w/o in case we use LayerNorm, in which
          # case the activation is applied after the layer normalization step).
          layers.append(
              tf.keras.layers.Dense(
                  hidden_layer_dims[i],
                  activation=(
                      hidden_activation
                      if not hidden_layer_use_layernorm
                      else None
                  ),
                  # Note, if the initializer is `None`, we want TensorFlow
                  # to use its default one. So we pass in `None`.
                  kernel_initializer=(
                      hidden_weights_initializer(
                          **hidden_layer_weights_initializer_config
                      )
                      if hidden_layer_weights_initializer_config
                      else hidden_weights_initializer
                  ),
                  use_bias=hidden_layer_use_bias,
                  bias_initializer=(
                      hidden_bias_initializer(
                          **hidden_layer_bias_initializer_config
                      )
                      if hidden_layer_bias_initializer_config
                      else hidden_bias_initializer
                  ),
              )
          )
          # Add LayerNorm and activation.
          if hidden_layer_use_layernorm:
              # Use epsilon=1e-5 here (instead of default 1e-3) to be unified
              # with torch.
              layers.append(
                  tf.keras.layers.LayerNormalization(epsilon=1e-5)
              )
              layers.append(
                  tf.keras.layers.Activation(hidden_activation)
              )
  
      output_weights_initializer = get_initializer_fn(
          output_weights_initializer, framework="tf2"
      )
      output_bias_initializer = get_initializer_fn(
          output_bias_initializer, framework="tf2"
      )
  
      if output_dim is not None:
          output_activation = get_activation_fn(
              output_activation, framework="tf2"
          )
          layers.append(
              tf.keras.layers.Dense(
                  output_dim,
                  activation=output_activation,
                  # Note, if the initializer is `None`, we want TensorFlow
                  # to use its default one. So we pass in `None`.
                  kernel_initializer=(
                      output_weights_initializer(
                          **output_weights_initializer_config
                      )
                      if output_weights_initializer_config
                      else output_weights_initializer
                  ),
                  use_bias=output_use_bias,
                  bias_initializer=(
                      output_bias_initializer(
                          **output_bias_initializer_config
                      )
                      if output_bias_initializer_config
                      else output_bias_initializer
                  ),
              )
          )
  
      self.network = tf.keras.Sequential(layers)
  
file_path: ray/python/ray/rllib/core/models/tf/primitives.py
imports: from typing import Callable, Dict, List, Optional, Tuple, Union
  from ray.rllib.models.utils import get_activation_fn, get_initializer_fn
  from ray.rllib.utils.framework import try_import_tf
label_desc: Use image node_id 1 to create a new TfMLP object from inherited base classes: tf.keras with example: obj = TfMLP()