class_docstr: Multi-headed attention layer.
  
  Arguments:
      units: Positive integer, output dim of hidden layer.
      num_heads: Positive integer, number of heads
          to repeat the same attention structure.
      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.
class_docstr_tok: ["Multi-headed","attention","layer",".","Arguments",":","units",":","Positive","integer",",","output","dim","of","hidden","layer",".","num_heads",":","Positive","integer",",","number","of","heads","to","repeat","the","same","attention","structure",".","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","."]
code: def __init__(
      self,
      units: int,
      num_heads: int,
      attention_dropout_rate: float = 0.0,
      density: float = 0.2,
      unidirectional: bool = False,
      use_key_relative_position: bool = False,
      use_value_relative_position: bool = False,
      max_relative_position: int = 5,
      heads_share_relative_embedding: bool = False,
  ) -> None:
      super().__init__()
  
      if units % num_heads != 0:
          rasa.shared.utils.cli.print_error_and_exit(
              f"Value Error: The given transformer size {units} should be a "
              f"multiple of the number of attention heads {num_heads}."
          )
  
      self.num_heads = num_heads
      self.units = units
      self.attention_dropout_rate = attention_dropout_rate
      self.unidirectional = unidirectional
      self.use_key_relative_position = use_key_relative_position
      self.use_value_relative_position = use_value_relative_position
      self.relative_length = max_relative_position
      self.relative_length += 1  # include current time
      self.heads_share_relative_embedding = (
          heads_share_relative_embedding
      )
  
      self._depth = units // self.num_heads
  
      # process queries
      self._query_dense_layer = RandomlyConnectedDense(
          units=units, use_bias=False, density=density
      )
      # process keys
      self._key_dense_layer = RandomlyConnectedDense(
          units=units, use_bias=False, density=density
      )
      # process values
      self._value_dense_layer = RandomlyConnectedDense(
          units=units, use_bias=False, density=density
      )
      # process attention output
      self._output_dense_layer = RandomlyConnectedDense(
          units=units, density=density
      )
  
      self._create_relative_embeddings()
  
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 1 to create a new MultiHeadAttention object from inherited base classes: tf.keras.layers with example: obj = MultiHeadAttention(units, num_heads, attention_dropout_rate, density, unidirectional, use_key_relative_position, use_value_relative_position, max_relative_position, heads_share_relative_embedding)