In simple terms, what does the self-attention mechanism help a Transformer model do?
Think about how the model understands relationships between words in a sentence.
Self-attention allows the model to weigh the importance of each word relative to others, capturing context effectively.
Given the following TensorFlow code snippet creating a Transformer encoder layer, what is the shape of the output tensor?
import tensorflow as tf from tensorflow.keras.layers import MultiHeadAttention, LayerNormalization, Dense class SimpleTransformerEncoder(tf.keras.layers.Layer): def __init__(self, embed_dim, num_heads, ff_dim): super().__init__() self.mha = MultiHeadAttention(num_heads=num_heads, key_dim=embed_dim) self.ffn = tf.keras.Sequential([ Dense(ff_dim, activation='relu'), Dense(embed_dim), ]) self.layernorm1 = LayerNormalization(epsilon=1e-6) self.layernorm2 = LayerNormalization(epsilon=1e-6) def call(self, inputs): attn_output = self.mha(inputs, inputs) out1 = self.layernorm1(inputs + attn_output) ffn_output = self.ffn(out1) return self.layernorm2(out1 + ffn_output) sample_input = tf.random.uniform((32, 10, 64)) # batch=32, seq_len=10, embed_dim=64 encoder = SimpleTransformerEncoder(embed_dim=64, num_heads=4, ff_dim=128) output = encoder(sample_input) output.shape
Check the input shape and whether the embedding dimension changes after the encoder layer.
The Transformer encoder keeps the sequence length and embedding dimension the same, so output shape matches input shape.
What is the most likely effect of increasing the number of attention heads in a Transformer model while keeping the total embedding dimension fixed?
Think about how the embedding dimension is split among heads.
Increasing heads splits the embedding dimension into smaller parts per head, enabling the model to focus on different aspects of the input.
You trained a Transformer-based language model to predict the next word in a sentence. Which metric best measures how well your model predicts the sequence?
Consider metrics commonly used in language modeling tasks.
Perplexity is the standard metric for language models, showing how surprised the model is by the actual words.
Consider this error during training a Transformer model: ValueError: Shapes (None, 10, 64) and (None, 10, 128) are incompatible. What is the most likely cause?
Check the dimensions of layers involved in residual connections.
Residual connections require matching dimensions; if the feed-forward layer outputs a different size, this error occurs.
