What is the main role of the Transformer decoder in a sequence-to-sequence model?
The Transformer decoder generates the output sequence step-by-step by attending to the encoder's output and previously generated tokens, enabling tasks like translation or text generation.
Click to reveal answer
beginner
Name the three main sub-layers inside a Transformer decoder block.
Why does the Transformer decoder use masked self-attention?
Masked self-attention prevents the decoder from 'seeing' future tokens during training, ensuring predictions are made only from past and current tokens, preserving the autoregressive property.
Click to reveal answer
beginner
In PyTorch, which class is commonly used to implement a Transformer decoder layer?
torch.nn.TransformerDecoderLayer is used to build a single decoder layer, and torch.nn.TransformerDecoder stacks multiple such layers.
Click to reveal answer
intermediate
What is the purpose of the cross-attention sub-layer in the Transformer decoder?
Cross-attention allows the decoder to focus on relevant parts of the encoder's output, helping it generate context-aware outputs based on the input sequence.
Click to reveal answer
Which sub-layer in the Transformer decoder prevents the model from attending to future tokens?
AFeed-forward network
BCross-attention
CMasked multi-head self-attention
DLayer normalization
✗ Incorrect
Masked multi-head self-attention blocks future tokens to maintain autoregressive generation.
What does the cross-attention layer in the Transformer decoder attend to?
AEncoder outputs
BFuture decoder tokens
CPrevious decoder outputs only
DInput embeddings
✗ Incorrect
Cross-attention attends to encoder outputs to incorporate input context.
In PyTorch, which class stacks multiple Transformer decoder layers?
Why is masking important in the Transformer decoder's self-attention?
ATo prevent attending to future tokens
BTo speed up training
CTo reduce model size
DTo normalize inputs
✗ Incorrect
Masking ensures the decoder only uses past and current tokens for prediction.
Which of these is NOT a sub-layer in a Transformer decoder block?
AMasked multi-head self-attention
BConvolutional layer
CPosition-wise feed-forward network
DMulti-head cross-attention
✗ Incorrect
Transformer decoders do not use convolutional layers in their standard architecture.
Explain the architecture and function of a Transformer decoder layer.
Think about how the decoder processes previous outputs and encoder information step-by-step.
You got /5 concepts.
Describe how masking works in the Transformer decoder and why it is necessary.
Consider the order of token generation in language models.
You got /4 concepts.
Practice
(1/5)
1. What is the main role of the Transformer decoder in a sequence-to-sequence model?
easy
A. To reduce the dimensionality of input data
B. To encode input sequences into fixed-size vectors
C. To perform image classification tasks
D. To generate output tokens step-by-step using previous outputs and encoder info
Solution
Step 1: Understand the Transformer decoder purpose
The Transformer decoder generates output sequences by attending to previous outputs and encoder outputs.
Step 2: Compare options with decoder role
Only To generate output tokens step-by-step using previous outputs and encoder info describes generating output tokens step-by-step using previous outputs and encoder info, which matches the decoder's role.
Final Answer:
To generate output tokens step-by-step using previous outputs and encoder info -> Option D
Quick Check:
Decoder generates outputs stepwise = D [OK]
Hint: Decoder creates outputs using past outputs and encoder data [OK]
Common Mistakes:
Confusing encoder and decoder roles
Thinking decoder encodes inputs
Assuming decoder reduces data size
2. Which of the following is the correct way to create a Transformer decoder layer in PyTorch?
easy
A. torch.nn.TransformerDecoderLayer(d_model=512, nhead=8)
B. torch.nn.TransformerEncoderLayer(d_model=512, nhead=8)
C. torch.nn.TransformerDecoder(d_model=512, nhead=8)
D. torch.nn.TransformerLayer(d_model=512, nhead=8)
Solution
Step 1: Identify the correct PyTorch class for a decoder layer
PyTorch provides torch.nn.TransformerDecoderLayer to create a single decoder layer.
Step 2: Check options for correct class and parameters
torch.nn.TransformerDecoderLayer(d_model=512, nhead=8) uses torch.nn.TransformerDecoderLayer with correct parameters. Others are either encoder layers or invalid classes.
Final Answer:
torch.nn.TransformerDecoderLayer(d_model=512, nhead=8) -> Option A
Quick Check:
Decoder layer class is TransformerDecoderLayer = A [OK]
Hint: Decoder layer class ends with 'DecoderLayer' in PyTorch [OK]
Common Mistakes:
Using EncoderLayer instead of DecoderLayer
Trying to instantiate TransformerDecoder directly
Using non-existent class names
3. Given the code below, what is the shape of the output tensor?
B. The target tensor 'tgt' has wrong feature dimension
C. The number of decoder layers is too low
D. The nhead parameter should be equal to batch size
Solution
Step 1: Check feature dimensions of inputs
The decoder expects target and memory tensors to have the same feature dimension (d_model=128).
Step 2: Identify mismatch in target tensor
Target tensor 'tgt' has feature size 64, which does not match d_model=128, causing an error.
Final Answer:
The target tensor 'tgt' has wrong feature dimension -> Option B
Quick Check:
Feature size mismatch in target tensor = A [OK]
Hint: Check if target and memory feature sizes match d_model [OK]
Common Mistakes:
Ignoring feature dimension mismatch
Assuming batch size must match nhead
Thinking number of layers causes this error
5. You want to build a Transformer decoder that generates text step-by-step using previous outputs and encoder memory. Which of the following is the best approach to implement the decoder's forward pass in PyTorch?
hard
A. Use a TransformerDecoder with masked target input to prevent attending to future tokens
B. Use a TransformerDecoder without masking so it sees all target tokens
C. Use only a TransformerEncoder for generation
D. Use a TransformerDecoder but feed the entire output sequence at once without masking
Solution
Step 1: Understand the need for masking in decoding
During generation, the decoder must not look at future tokens to avoid cheating, so masking is required.
Step 2: Identify correct usage in PyTorch
Using TransformerDecoder with a mask on target input prevents attending to future tokens, enabling step-by-step generation.
Final Answer:
Use a TransformerDecoder with masked target input to prevent attending to future tokens -> Option A
Quick Check:
Mask future tokens in decoder input = B [OK]
Hint: Mask future tokens in decoder input for stepwise generation [OK]
Common Mistakes:
Not using masks and allowing future token attention
Using encoder only for generation
Feeding full output sequence without masking
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.