Skip to content

Instantly share code, notes, and snippets.

View pythonlessons's full-sized avatar

Rokas Liuberskis pythonlessons

View GitHub Profile
encoder_embeddings shape (1, 100, 512)
encoder_layer_output shape (1, 100, 512)
@pythonlessons
pythonlessons / build_transformer_9.py
Created August 22, 2023 14:47
build_transformer
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,
@pythonlessons
pythonlessons / build_transformer_8.py
Created August 22, 2023 14:47
build_transformer
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,
@pythonlessons
pythonlessons / build_transformer_7.py
Created August 22, 2023 14:47
build_transformer
# 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)
@pythonlessons
pythonlessons / build_transformer_6.py
Created August 22, 2023 14:47
build_transformer
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.
@pythonlessons
pythonlessons / build_transformer_5.py
Created August 22, 2023 14:47
build_transformer
# 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))
@pythonlessons
pythonlessons / build_transformer_4.py
Created August 22, 2023 14:47
build_transformer
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.
@pythonlessons
pythonlessons / build_transformer_3.py
Created August 22, 2023 14:47
build_transformer
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)
@pythonlessons
pythonlessons / build_transformer_2.py
Created August 22, 2023 14:47
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.
@pythonlessons
pythonlessons / build_transformer_1.py
Created August 22, 2023 14:47
build_transformer
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)