Skip to content

Instantly share code, notes, and snippets.

@pythonlessons
Created August 22, 2023 14:47
Show Gist options
  • Save pythonlessons/05805538085a19670b0898506ceaad58 to your computer and use it in GitHub Desktop.
Save pythonlessons/05805538085a19670b0898506ceaad58 to your computer and use it in GitHub Desktop.
build_transformer
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.
num_layers (int): The number of layers in the encoder.
pos_embedding (PositionalEmbedding): The positional embedding layer.
enc_layers (list): The list of encoder layers.
dropout (tf.keras.layers.Dropout): The dropout layer.
"""
def __init__(self, num_layers: int, d_model: int, num_heads: int, dff: int, vocab_size: int, dropout_rate: float=0.1):
"""
Constructor of the Encoder.
Args:
num_layers (int): The number of layers in the encoder.
d_model (int): The dimensionality of the model.
num_heads (int): The number of heads in the multi-head attention layer.
dff (int): The dimensionality of the feed-forward layer.
vocab_size (int): The size of the vocabulary.
dropout_rate (float): The dropout rate.
"""
super().__init__()
self.d_model = d_model
self.num_layers = num_layers
self.pos_embedding = PositionalEmbedding(vocab_size=vocab_size, d_model=d_model)
self.enc_layers = [
EncoderLayer(d_model=d_model,
num_heads=num_heads,
dff=dff,
dropout_rate=dropout_rate)
for _ in range(num_layers)]
self.dropout = tf.keras.layers.Dropout(dropout_rate)
def call(self, x: tf.Tensor) -> tf.Tensor:
"""
The call function that performs the forward pass of the layer.
Args:
x (tf.Tensor): The input sequence of shape (batch_size, seq_length).
Returns:
tf.Tensor: The output sequence of shape (batch_size, seq_length, d_model).
"""
x = self.pos_embedding(x)
# here x has shape `(batch_size, seq_len, d_model)`
# Add dropout.
x = self.dropout(x)
for i in range(self.num_layers):
x = self.enc_layers[i](x)
return x # Shape `(batch_size, seq_len, d_model)`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment