Created
August 16, 2023 12:45
-
-
Save pythonlessons/bbab4fb403955e0fa922d0002e7362d9 to your computer and use it in GitHub Desktop.
transformer_attention
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BaseAttention(tf.keras.layers.Layer): | |
| """ | |
| Base class for all attention layers. It contains the common functionality of all attention layers. | |
| This layer contains a MultiHeadAttention layer, a LayerNormalization layer and an Add layer. | |
| It is used as a base class for the GlobalSelfAttention, CausalSelfAttention and CrossAttention layers. | |
| And it is not intended to be used directly. | |
| 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 __init__(self, **kwargs: dict): | |
| """ Constructor of the BaseAttention layer. | |
| Args: | |
| **kwargs: Additional keyword arguments that are passed to the MultiHeadAttention layer, e. g. | |
| num_heads (number of heads), key_dim (dimensionality of the key space), etc. | |
| """ | |
| super().__init__() | |
| self.mha = tf.keras.layers.MultiHeadAttention(**kwargs) | |
| self.layernorm = tf.keras.layers.LayerNormalization() | |
| self.add = tf.keras.layers.Add() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment