class_docstr: Transformer encoder.
  
  Encoder stack is made up of `num_layers` identical encoder layers.
  
  Arguments:
      num_layers: Positive integer, number of encoder layers.
      units: Positive integer, output dim of hidden layer.
      num_heads: Positive integer, number of heads
          to repeat the same attention structure.
      filter_units: Positive integer, output dim of the first ffn hidden layer.
      reg_lambda: Float, regularization factor.
      dropout_rate: Float between 0 and 1; fraction of the input units to drop.
      attention_dropout_rate: Float, dropout rate inside attention for training.
      density: Approximate fraction of trainable weights (in
          `RandomlyConnectedDense` layers).
      unidirectional: Boolean, use a unidirectional or bidirectional encoder.
      use_key_relative_position: Boolean, if 'True' use key
          relative embeddings in attention.
      use_value_relative_position: Boolean, if 'True' use value
          relative embeddings in attention.
      max_relative_position: Positive integer, max position for relative embeddings.
      heads_share_relative_embedding: Boolean, if 'True'
          heads will share relative embeddings.
      name: Optional name of the layer.
class_docstr_tok: ["Transformer","encoder",".","Encoder","stack","is","made","up","of","`","num_layers","`","identical","encoder","layers",".","Arguments",":","num_layers",":","Positive","integer",",","number","of","encoder","layers",".","units",":","Positive","integer",",","output","dim","of","hidden","layer",".","num_heads",":","Positive","integer",",","number","of","heads","to","repeat","the","same","attention","structure",".","filter_units",":","Positive","integer",",","output","dim","of","the","first","ffn","hidden","layer",".","reg_lambda",":","Float",",","regularization","factor",".","dropout_rate",":","Float","between","0","and","1",";","fraction","of","the","input","units","to","drop",".","attention_dropout_rate",":","Float",",","dropout","rate","inside","attention","for","training",".","density",":","Approximate","fraction","of","trainable","weights","(","in","`","RandomlyConnectedDense","`","layers",")",".","unidirectional",":","Boolean",",","use","a","unidirectional","or","bidirectional","encoder",".","use_key_relative_position",":","Boolean",",","if","'True","'","use","key","relative","embeddings","in","attention",".","use_value_relative_position",":","Boolean",",","if","'True","'","use","value","relative","embeddings","in","attention",".","max_relative_position",":","Positive","integer",",","max","position","for","relative","embeddings",".","heads_share_relative_embedding",":","Boolean",",","if","'True'","heads","will","share","relative","embeddings",".","name",":","Optional","name","of","the","layer","."]
docstr: Apply transformer encoder.
  
  Arguments:
      x: A tensor with shape [batch_size, length, input_size].
      pad_mask: Float tensor with shape broadcastable
          to (..., length, length). Defaults to None.
      training: A bool, whether in training mode or not.
  
  Returns:
      Transformer encoder output with shape [batch_size, length, units]
docstr_tok: ["Apply","transformer","encoder",".","Arguments",":","x",":","A","tensor","with","shape","[","batch_size",",","length",",","input_size","]",".","pad_mask",":","Float","tensor","with","shape","broadcastable","to","(","...",",","length",",","length",")",".","Defaults","to","None",".","training",":","A","bool",",","whether","in","training","mode","or","not",".","Returns",":","Transformer","encoder","output","with","shape","[","batch_size",",","length",",","units","]"]
code: def call(
      self,
      x: tf.Tensor,
      pad_mask: Optional[tf.Tensor] = None,
      training: Optional[Union[tf.Tensor, bool]] = None,
  ) -> Tuple[tf.Tensor, tf.Tensor]:
      """Apply transformer encoder.
  
      Arguments:
          x: A tensor with shape [batch_size, length, input_size].
          pad_mask: Float tensor with shape broadcastable
              to (..., length, length). Defaults to None.
          training: A bool, whether in training mode or not.
  
      Returns:
          Transformer encoder output with shape [batch_size, length, units]
      """
      # adding embedding and position encoding.
      x = self._embedding(x)  # (batch_size, length, units)
      x *= tf.math.sqrt(tf.cast(self.units, tf.float32))
      x += self._positional_encoding(tf.shape(x)[1])
      x = self._dropout(x, training=training)
  
      if pad_mask is not None:
          pad_mask = tf.squeeze(pad_mask, -1)  # (batch_size, length)
          pad_mask = pad_mask[:, tf.newaxis, tf.newaxis, :]
          # pad_mask.shape = (batch_size, 1, 1, length)
          if self.unidirectional:
              # add look ahead pad mask to emulate unidirectional behavior
              pad_mask = tf.minimum(
                  1.0,
                  pad_mask
                  + self._look_ahead_pad_mask(tf.shape(pad_mask)[-1]),
              )  # (batch_size, 1, length, length)
  
      layer_attention_weights = []
  
      for layer in self._enc_layers:
          x, attn_weights = layer(
              x, pad_mask=pad_mask, training=training
          )
          layer_attention_weights.append(attn_weights)
  
      # if normalization is done in encoding layers, then it should also be done
      # on the output, since the output can grow very large, being the sum of
      # a whole stack of unnormalized layer outputs.
      x = self._layer_norm(x)  # (batch_size, length, units)
  
      # Keep the batch dimension on the first axis
      attention_weights_as_output = tf.transpose(
          tf.stack(layer_attention_weights), (1, 0, 2, 3, 4)
      )
  
      # (batch_size, length, units),
      # (batch_size, num_layers, num_heads, length, length)
      return x, attention_weights_as_output
  
imports: from typing import Optional, Text, Tuple, Union
  import numpy
  import tensorflow
  from keras.utils.control_flow_util import smart_cond
  from tensorflow.keras import backend
  import rasa.shared.utils.cli
  from rasa.utils.tensorflow.layers import RandomlyConnectedDense
label_desc: Use image node_id 5 for calling the TransformerEncoder obj's underlying member method code with example usage: obj.call(x, pad_mask, training) and returns: x,  attention_weights_as_output