What is the main purpose of a Transformer encoder in machine learning?
A Transformer encoder processes input data by capturing relationships between all parts of the input simultaneously, helping models understand context and meaning without relying on sequence order alone.
Click to reveal answer
beginner
What is 'self-attention' in the context of a Transformer encoder?
Self-attention is a mechanism where the model looks at all parts of the input to decide which parts are important to focus on when encoding each word or token.
Click to reveal answer
intermediate
Name the two main sub-layers inside a Transformer encoder block.
The two main sub-layers are: 1) Multi-head self-attention layer, 2) Position-wise feed-forward neural network.
Click to reveal answer
intermediate
Why do Transformer encoders use 'positional encoding'?
Because Transformers process all input tokens at once, positional encoding adds information about the order of tokens so the model knows the sequence position of each token.
Click to reveal answer
beginner
In PyTorch, which class can be used to create a Transformer encoder layer?
You can use torch.nn.TransformerEncoderLayer to create a single encoder layer, and torch.nn.TransformerEncoder to stack multiple layers.
Click to reveal answer
What does the self-attention mechanism in a Transformer encoder help the model do?
AReduce the size of the input data
BFocus on important parts of the input sequence
CSort the input tokens alphabetically
DGenerate output tokens directly
✗ Incorrect
Self-attention helps the model focus on important parts of the input sequence by weighing the relevance of each token to others.
Which component adds information about token order in a Transformer encoder?
APositional encoding
BLayer normalization
CFeed-forward network
DMulti-head attention
✗ Incorrect
Positional encoding adds information about the position of tokens since the Transformer processes tokens in parallel.
What is the role of the feed-forward network inside a Transformer encoder layer?
ATo apply a simple neural network to each position independently
BTo add positional information
CTo combine outputs from multiple attention heads
DTo normalize the input data
✗ Incorrect
The feed-forward network applies a small neural network to each token's representation independently to add complexity and non-linearity.
In PyTorch, which class stacks multiple Transformer encoder layers?
Atorch.nn.TransformerEncoderLayer
Btorch.nn.MultiheadAttention
Ctorch.nn.TransformerEncoder
Dtorch.nn.Linear
✗ Incorrect
torch.nn.TransformerEncoder stacks multiple TransformerEncoderLayer instances to build a full encoder.
Why do Transformer encoders process all tokens simultaneously instead of one by one?
ABecause they cannot handle sequences
BTo generate output faster
CTo sort tokens before processing
DTo reduce training time and capture global context
✗ Incorrect
Processing all tokens simultaneously allows the model to learn relationships between all tokens and speeds up training.
Explain how self-attention works inside a Transformer encoder and why it is important.
Think about how the model decides what parts of the input to focus on.
You got /3 concepts.
Describe the main components of a Transformer encoder layer and their roles.
Consider the flow of data through the encoder block.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of a Transformer encoder in PyTorch?
easy
A. To classify images using convolutional layers
B. To generate new sequences from random noise
C. To process sequences by attending to all positions simultaneously
D. To reduce the dimensionality of input data using convolution
Solution
Step 1: Understand the Transformer encoder role
The Transformer encoder looks at all parts of the input sequence at once using self-attention, unlike sequential models.
Step 2: Compare options with this role
Only To process sequences by attending to all positions simultaneously describes this behavior correctly. Other options describe unrelated tasks.
Final Answer:
To process sequences by attending to all positions simultaneously -> Option C
Quick Check:
Transformer encoder = attends all positions [OK]
Hint: Remember: Transformer encoder uses self-attention on sequences [OK]
Common Mistakes:
Confusing encoder with decoder
Thinking it generates sequences
Mixing with convolutional networks
2. Which of the following is the correct way to create a Transformer encoder layer in PyTorch?
easy
A. torch.nn.Conv1d(in_channels=512, out_channels=8)
B. torch.nn.TransformerEncoderLayer(d_model=512, nhead=8)
C. torch.nn.TransformerDecoderLayer(d_model=512, nhead=8)
D. torch.nn.Linear(512, 8)
Solution
Step 1: Identify the correct PyTorch class for Transformer encoder layer
The class to create a Transformer encoder layer is torch.nn.TransformerEncoderLayer.
Step 2: Check the parameters
d_model and nhead are required parameters for this class, matching torch.nn.TransformerEncoderLayer(d_model=512, nhead=8).
Final Answer:
torch.nn.TransformerEncoderLayer(d_model=512, nhead=8) -> Option B
Quick Check:
Encoder layer class = TransformerEncoderLayer [OK]
Hint: Look for TransformerEncoderLayer class with d_model and nhead [OK]
Common Mistakes:
Using DecoderLayer instead of EncoderLayer
Confusing convolution or linear layers with Transformer layers
Missing required parameters
3. Given the following code, what is the shape of the output tensor?
C. The input tensor shape should be (batch_size, seq_len, features)
D. TransformerEncoderLayer requires an additional dropout parameter
Solution
Step 1: Check input tensor shape
Input shape is (seq_len=20, batch_size=16, features=64), which is correct for TransformerEncoder.
Step 2: Verify other parameters
nhead=8 is valid as 64 is divisible by 8; dropout is optional; no error in code.
Final Answer:
The input tensor shape is correct; no error -> Option A
Quick Check:
Input shape (seq_len, batch, features) is correct [OK]
Hint: Remember input shape is (seq_len, batch, features) for TransformerEncoder [OK]
Common Mistakes:
Swapping batch and sequence dimensions
Thinking dropout is mandatory
Assuming nhead must be 1
5. You want to build a Transformer encoder in PyTorch that processes sequences of length 15 with batch size 8 and feature size 32. You want 3 encoder layers, each with 4 attention heads. Which code snippet correctly creates this model and processes a random input?