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 Encoder(tf.keras.layers.Layer): | |
| """ | |
| A custom TensorFlow layer that implements the Encoder. This layer is mostly used in the Transformer models | |
| for natural language processing tasks, such as machine translation, text summarization or text classification. | |
| Methods: | |
| call: Performs the forward pass of the layer. | |
| Attributes: | |
| d_model (int): The dimensionality of the model. |
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
| encoder_vocab_size = 1000 | |
| d_model = 512 | |
| encoder_embedding_layer = PositionalEmbedding(vocab_size, d_model) | |
| random_encoder_input = np.random.randint(0, encoder_vocab_size, size=(1, 100)) | |
| encoder_embeddings = encoder_embedding_layer(random_encoder_input) | |
| print("encoder_embeddings shape", encoder_embeddings.shape) |
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 EncoderLayer(tf.keras.layers.Layer): | |
| """ | |
| A single layer of the Encoder. Usually there are multiple layers stacked on top of each other. | |
| Methods: | |
| call: Performs the forward pass of the layer. | |
| Attributes: | |
| self_attention (GlobalSelfAttention): The global self-attention layer. | |
| ffn (FeedForward): The feed-forward layer. |
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
| encoder_embeddings shape (1, 100, 512) | |
| feed_forward_output shape (1, 100, 512) |
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
| decoder_embeddings shape (1, 110, 512) | |
| causal_self_attention_output shape (1, 110, 512) | |
| Difference between the two outputs: 0.0 |
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
| encoder_embeddings shape (1, 100, 512) | |
| global_self_attention_output shape (1, 100, 512) |
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
| encoder_embeddings shape (1, 100, 512) | |
| decoder_embeddings shape (1, 110, 512) | |
| cross_attention_output shape (1, 110, 512) |
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
| encoder_vocab_size = 1000 | |
| d_model = 512 | |
| encoder_embedding_layer = PositionalEmbedding(vocab_size, d_model) | |
| random_encoder_input = np.random.randint(0, encoder_vocab_size, size=(1, 100)) | |
| encoder_embeddings = encoder_embedding_layer(random_encoder_input) | |
| print("encoder_embeddings shape", encoder_embeddings.shape) |
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 FeedForward(tf.keras.layers.Layer): | |
| """ | |
| A class that implements the feed-forward layer. | |
| Methods: | |
| call: Performs the forward pass of the layer. | |
| Attributes: | |
| seq (tf.keras.Sequential): The sequential layer that contains the feed-forward layers. It applies the two feed-forward layers and the dropout layer. | |
| add (tf.keras.layers.Add): The Add layer. |
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
| decoder_vocab_size = 1100 | |
| d_model = 512 | |
| decoder_embedding_layer = PositionalEmbedding(vocab_size, d_model) | |
| random_decoder_input = np.random.randint(0, decoder_vocab_size, size=(1, 110)) | |
| decoder_embeddings = decoder_embedding_layer(random_decoder_input) | |
| print("decoder_embeddings shape", decoder_embeddings.shape) |