What is the main purpose of a Transformer model in machine learning?
A Transformer model is designed to process sequences of data, like sentences, by paying attention to all parts of the sequence at once. It helps computers understand context and relationships in data such as language.
Click to reveal answer
beginner
What does 'self-attention' mean in a Transformer?
Self-attention is a way for the model to look at every word in a sentence and decide which other words are important to understand the meaning of each word.
Click to reveal answer
intermediate
Name the two main parts of a Transformer encoder layer.
The two main parts are: 1) Multi-head self-attention mechanism, which helps the model focus on different parts of the input, and 2) Feed-forward neural network, which processes the information further.
Click to reveal answer
intermediate
Why do Transformers use 'positional encoding'?
Because Transformers look at all words at once, they need a way to know the order of words. Positional encoding adds information about the position of each word in the sequence.
Click to reveal answer
intermediate
What is the benefit of using multi-head attention instead of single-head attention?
Multi-head attention allows the model to look at the input from different perspectives at the same time, capturing more details and relationships than single-head attention.
Click to reveal answer
What does the Transformer model primarily replace compared to older sequence models?
ARecurrent neural networks (RNNs)
BConvolutional neural networks (CNNs)
CDecision trees
DSupport vector machines
✗ Incorrect
Transformers replace RNNs by processing sequences in parallel using attention mechanisms.
In self-attention, what does the model compute to decide importance between words?
AFrequency of words
BEuclidean distance between words
CDot product between query and key vectors
DSum of word lengths
✗ Incorrect
Self-attention uses dot products between query and key vectors to measure how much focus to give each word.
What role does the feed-forward network play in a Transformer encoder layer?
ACalculates positional encoding
BProcesses each position independently after attention
CGenerates output tokens
DNormalizes input data
✗ Incorrect
The feed-forward network processes each position separately to transform the attended information.
Why is positional encoding necessary in Transformers?
ATo provide word order information
BTo reduce model size
CTo speed up training
DTo increase vocabulary size
✗ Incorrect
Positional encoding adds information about word order since Transformers process all words simultaneously.
How does multi-head attention improve model understanding?
ABy reducing training time
BBy increasing the number of layers
CBy using larger batch sizes
DBy attending to different parts of the input simultaneously
✗ Incorrect
Multi-head attention allows the model to focus on multiple aspects of the input at once.
Explain how self-attention works in a Transformer model and why it is important.
Think about how the model decides which words to focus on when reading a sentence.
You got /4 concepts.
Describe the role of positional encoding in Transformers and how it helps the model.
Remember that Transformers see all words at once, so they need a way to know the order.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the attention mechanism in a Transformer model?
easy
A. To reduce the number of layers
B. To increase the size of the model
C. To focus on important parts of the input sequence
D. To generate random outputs
Solution
Step 1: Understand attention in Transformers
The attention mechanism helps the model look at important words or parts in the input sequence.
Step 2: Identify the main goal of attention
Its goal is to give more weight to relevant parts, improving understanding of context.
Final Answer:
To focus on important parts of the input sequence -> Option C
Quick Check:
Attention = Focus on important parts [OK]
Hint: Attention means focusing on key input parts [OK]
Common Mistakes:
Thinking attention increases model size
Confusing attention with layer count
Believing attention creates random outputs
2. Which of the following is the correct way to import the MultiHeadAttention layer in TensorFlow?
easy
A. from tensorflow.keras.layers import MultiHeadAttention
B. import MultiHeadAttention from tensorflow.layers
C. from tensorflow import MultiHeadAttention
D. import MultiHeadAttention from keras.layers
Solution
Step 1: Recall TensorFlow Keras layer imports
TensorFlow's Keras layers are imported from tensorflow.keras.layers module.
Step 2: Check correct import syntax
The correct syntax is: from tensorflow.keras.layers import MultiHeadAttention.
Final Answer:
from tensorflow.keras.layers import MultiHeadAttention -> Option A
Quick Check:
Correct import = from tensorflow.keras.layers import MultiHeadAttention [OK]
Hint: Use tensorflow.keras.layers for Keras layers [OK]
Common Mistakes:
Using incorrect import paths
Confusing tensorflow and keras imports
Using wrong import syntax
3. Given the following TensorFlow code snippet, what is the shape of the output tensor?
MultiHeadAttention needs query and value arguments; value is missing here.
Step 2: Understand why value is needed
Without value, the layer cannot compute attention properly, causing an error.
Final Answer:
MultiHeadAttention requires 'value' argument -> Option A
Quick Check:
Missing value argument = MultiHeadAttention requires 'value' argument [OK]
Hint: Always provide query and value arguments to MultiHeadAttention [OK]
Common Mistakes:
Assuming value defaults when only query is provided
Ignoring required arguments in call
Thinking positional encoding is mandatory here
5. You want to build a Transformer block in TensorFlow that includes MultiHeadAttention followed by a feed-forward network with ReLU activation. Which code snippet correctly implements this?