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) | |
| encoder_layer_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_input_size = 100 | |
| decoder_input_size = 110 | |
| encoder_vocab_size = 1000 | |
| decoder_vocab_size = 1000 | |
| model = Transformer( | |
| input_vocab_size=encoder_vocab_size, | |
| target_vocab_size=decoder_vocab_size, | |
| encoder_input_size=encoder_input_size, |
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
| def Transformer( | |
| input_vocab_size: int, | |
| target_vocab_size: int, | |
| encoder_input_size: int = None, | |
| decoder_input_size: int = None, | |
| num_layers: int=6, | |
| d_model: int=512, | |
| num_heads: int=8, | |
| dff: int=2048, | |
| dropout_rate: float=0.1, |
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
| # Test decoder layer | |
| decoder_vocab_size = 1000 | |
| d_model = 512 | |
| decoder_layer = Decoder(num_layers=2, d_model=d_model, num_heads=2, dff=2048, vocab_size=decoder_vocab_size) | |
| random_decoder_input = np.random.randint(0, decoder_vocab_size, size=(1, 100)) | |
| decoder_output = decoder_layer(random_decoder_input, encoder_output) | |
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 Decoder(tf.keras.layers.Layer): | |
| """ | |
| A custom TensorFlow layer that implements the Decoder. 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
| # Test DecoderLayer layer | |
| decoder_vocab_size = 1000 | |
| d_model = 512 | |
| dff = 2048 | |
| num_heads = 8 | |
| decoder_layer = DecoderLayer(d_model, num_heads, dff) | |
| random_decoderLayer_input = np.random.randint(0, decoder_vocab_size, size=(1, 110)) | |
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 DecoderLayer(tf.keras.layers.Layer): | |
| """ | |
| A single layer of the Decoder. Usually there are multiple layers stacked on top of each other. | |
| Methods: | |
| call: Performs the forward pass of the layer. | |
| Attributes: | |
| causal_self_attention (CausalSelfAttention): The causal self-attention layer. | |
| cross_attention (CrossAttention): The cross-attention 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_vocab_size = 1000 | |
| d_model = 512 | |
| encoder = Encoder(num_layers=2, d_model=d_model, num_heads=2, dff=2048, vocab_size=encoder_vocab_size) | |
| random_encoder_input = np.random.randint(0, encoder_vocab_size, size=(1, 100)) | |
| encoder_output = encoder(random_encoder_input) | |
| print("random_encoder_input shape", random_encoder_input.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 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) |