Bird
Raised Fist0
PyTorchml~10 mins

Why attention revolutionized deep learning in PyTorch - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a simple attention score using dot product.

PyTorch
import torch

query = torch.tensor([[1, 0, 1]], dtype=torch.float32)
key = torch.tensor([[0, 1, 0]], dtype=torch.float32)

attention_score = torch.matmul(query, [1].T)
print(attention_score)
Drag options to blanks, or click blank then click option'
Akey
Bquery
Ctorch.tensor([[1, 1, 1]])
Dtorch.eye(3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using query instead of key for the dot product.
Not transposing the key tensor before multiplication.
2fill in blank
medium

Complete the code to apply softmax to the attention scores.

PyTorch
import torch
import torch.nn.functional as F

scores = torch.tensor([[1.0, 2.0, 3.0]])
attention_weights = F.[1](scores, dim=1)
print(attention_weights)
Drag options to blanks, or click blank then click option'
Asigmoid
Brelu
Csoftmax
Dtanh
Attempts:
3 left
💡 Hint
Common Mistakes
Using sigmoid which does not normalize across the dimension.
Using relu or tanh which do not produce probability distributions.
3fill in blank
hard

Fix the error in the scaled dot-product attention calculation.

PyTorch
import torch
import math

query = torch.randn(1, 4)
key = torch.randn(1, 4)
scale = math.sqrt(query.shape[1])
scores = torch.matmul(query, key.T) / [1]
print(scores)
Drag options to blanks, or click blank then click option'
Aquery.shape[0]
Bkey.shape[1]
Ckey.shape[0]
Dquery.shape[1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using batch size dimension instead of feature dimension for scaling.
Using key shape dimension incorrectly.
4fill in blank
hard

Fill both blanks to complete the attention output calculation.

PyTorch
import torch
import torch.nn.functional as F

query = torch.randn(1, 3)
key = torch.randn(1, 3)
value = torch.randn(1, 3)

scores = torch.matmul(query, key.T) / torch.sqrt(torch.tensor(query.shape[[1]], dtype=torch.float32))
weights = F.softmax(scores, dim=[2])
output = torch.matmul(weights, value)
print(output)
Drag options to blanks, or click blank then click option'
A1
B0
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong shape index for scaling.
Applying softmax on wrong dimension.
5fill in blank
hard

Fill all three blanks to implement a simple attention mechanism output.

PyTorch
import torch
import torch.nn.functional as F

query = torch.randn(2, 4)
key = torch.randn(2, 4)
value = torch.randn(2, 4)

scores = torch.matmul(query, key.T) / torch.sqrt(torch.tensor(query.shape[[1]], dtype=torch.float32))
weights = F.softmax(scores, dim=[2])
attention_output = torch.matmul(weights, [3])
print(attention_output)
Drag options to blanks, or click blank then click option'
A1
B0
Cvalue
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using key instead of value for final multiplication.
Incorrect dimension indices for scaling or softmax.

Practice

(1/5)
1. What is the main reason attention mechanisms changed deep learning models?
easy
A. They make models run faster on CPUs.
B. They reduce the size of the training data needed.
C. They let models focus on important parts of the input data.
D. They remove the need for any training.

Solution

  1. Step 1: Understand attention's role

    Attention helps models decide which parts of the input are most important to look at.
  2. Step 2: Compare options

    Only They let models focus on important parts of the input data. correctly states this key benefit; others are incorrect or unrelated.
  3. Final Answer:

    They let models focus on important parts of the input data. -> Option C
  4. Quick Check:

    Attention = Focus on important data [OK]
Hint: Attention means focusing on key input parts [OK]
Common Mistakes:
  • Thinking attention reduces training data size
  • Believing attention speeds up CPU processing
  • Assuming attention removes training need
2. Which PyTorch code snippet correctly creates a simple attention layer using nn.MultiheadAttention?
easy
A. attn = nn.MultiheadAttention(embed_dim=64, num_heads=8)
B. attn = nn.MultiheadAttention(num_heads=64, embed_dim=8)
C. attn = nn.MultiheadAttention(8, 64)
D. attn = nn.MultiheadAttention(embed_dim=8, num_heads=64)

Solution

  1. Step 1: Check PyTorch docs for nn.MultiheadAttention

    The correct order is embed_dim first, then num_heads as named arguments.
  2. Step 2: Validate options

    Only attn = nn.MultiheadAttention(embed_dim=64, num_heads=8) uses correct argument names and values; others swap values between embed_dim and num_heads or use positional arguments incorrectly.
  3. Final Answer:

    attn = nn.MultiheadAttention(embed_dim=64, num_heads=8) -> Option A
  4. Quick Check:

    embed_dim first, num_heads second [OK]
Hint: Remember embed_dim before num_heads in nn.MultiheadAttention [OK]
Common Mistakes:
  • Swapping embed_dim and num_heads
  • Using positional args in wrong order
  • Confusing parameter names
3. Given this PyTorch code using attention, what is the shape of the output tensor?
import torch
import torch.nn as nn

attn = nn.MultiheadAttention(embed_dim=16, num_heads=4)
query = torch.rand(5, 3, 16)  # (seq_len, batch, embed_dim)
key = torch.rand(5, 3, 16)
value = torch.rand(5, 3, 16)
out, weights = attn(query, key, value)
print(out.shape)
medium
A. (3, 16, 5)
B. (5, 3, 16)
C. (16, 3, 5)
D. (3, 5, 16)

Solution

  1. Step 1: Understand input shapes for nn.MultiheadAttention

    Inputs are (seq_len, batch_size, embed_dim). Output shape matches query shape.
  2. Step 2: Check given query shape

    Query shape is (5, 3, 16), so output shape is the same.
  3. Final Answer:

    (5, 3, 16) -> Option B
  4. Quick Check:

    Output shape = query shape = (5, 3, 16) [OK]
Hint: Output shape matches query shape in MultiheadAttention [OK]
Common Mistakes:
  • Mixing batch and sequence dimensions
  • Confusing embed_dim position
  • Assuming output shape changes
4. This PyTorch code throws an error. What is the mistake?
import torch
import torch.nn as nn

attn = nn.MultiheadAttention(embed_dim=32, num_heads=4)
query = torch.rand(10, 2, 16)
key = torch.rand(10, 2, 16)
value = torch.rand(10, 2, 16)
out, weights = attn(query, key, value)
medium
A. Number of heads must be 16
B. Batch size must be first dimension
C. Query, key, value must be 2D tensors
D. embed_dim in MultiheadAttention does not match input embed_dim

Solution

  1. Step 1: Check embed_dim consistency

    MultiheadAttention expects input embed_dim to match embed_dim parameter (32), but inputs have embed_dim=16.
  2. Step 2: Confirm input shape format

    Input shape (seq_len, batch, embed_dim) is correct, batch is second dimension, so no error there.
  3. Final Answer:

    embed_dim in MultiheadAttention does not match input embed_dim -> Option D
  4. Quick Check:

    embed_dim mismatch causes error [OK]
Hint: embed_dim must match between layer and inputs [OK]
Common Mistakes:
  • Mixing batch and sequence dimensions
  • Ignoring embed_dim mismatch
  • Expecting 2D tensors instead of 3D
5. Why did attention mechanisms especially revolutionize natural language processing (NLP) compared to older models?
hard
A. Because attention allows models to weigh all words in a sentence dynamically, capturing context better.
B. Because attention removes the need for any word embeddings.
C. Because attention makes models ignore word order completely.
D. Because attention reduces the number of layers needed to one.

Solution

  1. Step 1: Understand attention's effect on context in NLP

    Attention lets models look at all words and decide which are important for each output word, capturing context dynamically.
  2. Step 2: Evaluate options

    Because attention allows models to weigh all words in a sentence dynamically, capturing context better. correctly describes this. The other options are incorrect: attention still requires word embeddings; it preserves word order via positional encodings; it does not reduce the number of layers to one.
  3. Final Answer:

    Because attention allows models to weigh all words in a sentence dynamically, capturing context better. -> Option A
  4. Quick Check:

    Attention = dynamic context weighting [OK]
Hint: Attention captures context by weighing all words [OK]
Common Mistakes:
  • Thinking attention removes embeddings
  • Believing attention ignores word order
  • Assuming attention reduces layers to one
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard