Skip to content

Instantly share code, notes, and snippets.

@pythonlessons
Created August 16, 2023 12:45
Show Gist options
  • Select an option

  • Save pythonlessons/29770f3823e7f76e2dc11d8d080cd76d to your computer and use it in GitHub Desktop.

Select an option

Save pythonlessons/29770f3823e7f76e2dc11d8d080cd76d to your computer and use it in GitHub Desktop.
transformer_attention
class GlobalSelfAttention(BaseAttention):
"""
A class that implements the global self-attention layer by inheriting from the BaseAttention class.
This layer is used to process a single sequence and attends to all the tokens in the sequence.
Methods:
call: Performs the forward pass of the layer.
Attributes:
mha (tf.keras.layers.MultiHeadAttention): The MultiHeadAttention layer.
layernorm (tf.keras.layers.LayerNormalization): The LayerNormalization layer.
add (tf.keras.layers.Add): The Add layer.
"""
def call(self, x: tf.Tensor) -> tf.Tensor:
"""
The call function that performs the global self-attention operation.
Args:
x (tf.Tensor): The input sequence of shape (batch_size, seq_length, d_model).
Returns:
tf.Tensor: The output sequence of shape (batch_size, seq_length, d_model).
"""
attn_output = self.mha(query=x, value=x, key=x)
x = self.add([x, attn_output])
x = self.layernorm(x)
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment