Bird
Raised Fist0
PyTorchml~8 mins

Transformer encoder in PyTorch - Model Metrics & Evaluation

Choose your learning style10 modes available
Metrics & Evaluation - Transformer encoder
Which metric matters for Transformer encoder and WHY

The Transformer encoder is often used for tasks like text classification, translation, or feature extraction. The key metrics depend on the task:

  • Accuracy for classification tasks: shows how many predictions match the true labels.
  • Precision and Recall when classes are imbalanced or some errors cost more.
  • F1 score balances precision and recall, useful when both matter.
  • Loss (Cross-Entropy) during training: tells how well the model fits the data.

Choosing the right metric helps understand if the Transformer encoder is learning useful patterns or not.

Confusion matrix example

For a binary classification task using a Transformer encoder, suppose we have 100 samples:

      | Predicted Positive | Predicted Negative |
      |--------------------|--------------------|
      | True Positive (TP) = 40 | False Positive (FP) = 5 |
      | False Negative (FN) = 10 | True Negative (TN) = 45 |
    

Calculations:

  • Precision = TP / (TP + FP) = 40 / (40 + 5) = 0.89
  • Recall = TP / (TP + FN) = 40 / (40 + 10) = 0.80
  • Accuracy = (TP + TN) / Total = (40 + 45) / 100 = 0.85
  • F1 Score = 2 * (Precision * Recall) / (Precision + Recall) ≈ 0.84
Precision vs Recall tradeoff with examples

Transformer encoders can be tuned to favor precision or recall depending on the task:

  • High Precision: Important when false positives are costly. For example, in spam detection, marking a good email as spam is bad. So, the model should be very sure before labeling spam.
  • High Recall: Important when missing positive cases is costly. For example, in medical diagnosis, missing a disease case is dangerous. The model should catch as many positives as possible, even if some false alarms happen.

Adjusting thresholds or training strategies on the Transformer encoder affects this balance.

What good vs bad metric values look like for Transformer encoder

Good metrics mean the Transformer encoder understands the data well:

  • Good: Accuracy > 85%, Precision and Recall both above 80%, F1 score close to these values.
  • Bad: Accuracy near random guess (e.g., 50% for binary), Precision or Recall very low (below 50%), F1 score low showing imbalance.

Good metrics show the model predicts correctly and balances errors well.

Common pitfalls in metrics for Transformer encoder
  • Accuracy paradox: High accuracy can be misleading if classes are imbalanced. For example, 90% accuracy if 90% samples belong to one class but model ignores the other.
  • Data leakage: If test data leaks into training, metrics look unrealistically good.
  • Overfitting indicators: Training loss very low but validation loss high means model memorizes training data but fails on new data.
  • Ignoring class imbalance: Not using precision, recall, or F1 when classes are uneven can hide poor performance on minority class.
Self-check question

Your Transformer encoder model has 98% accuracy but only 12% recall on the positive class (e.g., fraud detection). Is it good for production? Why or why not?

Answer: No, it is not good. The high accuracy is misleading because the model misses 88% of positive cases (fraud). In fraud detection, recall is critical to catch as many frauds as possible. Low recall means many frauds go undetected, which is risky.

Key Result
For Transformer encoder tasks, balance precision and recall using F1 score to ensure meaningful performance beyond accuracy.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To process sequences by attending to all positions simultaneously -> Option C
  4. 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

  1. Step 1: Identify the correct PyTorch class for Transformer encoder layer

    The class to create a Transformer encoder layer is torch.nn.TransformerEncoderLayer.
  2. Step 2: Check the parameters

    d_model and nhead are required parameters for this class, matching torch.nn.TransformerEncoderLayer(d_model=512, nhead=8).
  3. Final Answer:

    torch.nn.TransformerEncoderLayer(d_model=512, nhead=8) -> Option B
  4. 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?
import torch
from torch.nn import TransformerEncoderLayer

layer = TransformerEncoderLayer(d_model=16, nhead=4)
input_tensor = torch.rand(10, 32, 16)  # (seq_len, batch_size, features)
output = layer(input_tensor)
medium
A. (32, 10, 16)
B. (32, 16, 10)
C. (10, 16, 32)
D. (10, 32, 16)

Solution

  1. Step 1: Understand input shape format

    The input tensor shape is (sequence length, batch size, features) = (10, 32, 16).
  2. Step 2: TransformerEncoderLayer output shape

    The output shape matches the input shape for TransformerEncoderLayer.
  3. Final Answer:

    (10, 32, 16) -> Option D
  4. Quick Check:

    Output shape = input shape (seq_len, batch, features) [OK]
Hint: Output shape matches input shape for Transformer encoder layers [OK]
Common Mistakes:
  • Swapping batch size and sequence length
  • Confusing feature dimension order
  • Assuming output shape changes
4. What is wrong with this code snippet that tries to create a Transformer encoder with 2 layers?
import torch
from torch.nn import TransformerEncoder, TransformerEncoderLayer

layer = TransformerEncoderLayer(d_model=64, nhead=8)
encoder = TransformerEncoder(layer, num_layers=2)
input_tensor = torch.rand(20, 16, 64)
output = encoder(input_tensor)
medium
A. The input tensor shape is correct; no error
B. The number of heads (nhead) must be 1
C. The input tensor shape should be (batch_size, seq_len, features)
D. TransformerEncoderLayer requires an additional dropout parameter

Solution

  1. Step 1: Check input tensor shape

    Input shape is (seq_len=20, batch_size=16, features=64), which is correct for TransformerEncoder.
  2. Step 2: Verify other parameters

    nhead=8 is valid as 64 is divisible by 8; dropout is optional; no error in code.
  3. Final Answer:

    The input tensor shape is correct; no error -> Option A
  4. 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?
hard
A. import torch from torch.nn import TransformerEncoder, TransformerEncoderLayer layer = TransformerEncoderLayer(d_model=32, nhead=4) encoder = TransformerEncoder(layer, num_layers=3) input_tensor = torch.rand(15, 8, 32) output = encoder(input_tensor)
B. import torch from torch.nn import TransformerEncoder, TransformerEncoderLayer layer = TransformerEncoderLayer(d_model=32, nhead=3) encoder = TransformerEncoder(layer, num_layers=4) input_tensor = torch.rand(8, 15, 32) output = encoder(input_tensor)
C. import torch from torch.nn import TransformerEncoder, TransformerEncoderLayer layer = TransformerEncoderLayer(d_model=15, nhead=4) encoder = TransformerEncoder(layer, num_layers=3) input_tensor = torch.rand(15, 8, 32) output = encoder(input_tensor)
D. import torch from torch.nn import TransformerEncoder, TransformerEncoderLayer layer = TransformerEncoderLayer(d_model=32, nhead=4) encoder = TransformerEncoder(layer, num_layers=3) input_tensor = torch.rand(8, 15, 32) output = encoder(input_tensor)

Solution

  1. Step 1: Check d_model and nhead parameters

    d_model must match feature size (32), and nhead must divide d_model (4 divides 32). import torch from torch.nn import TransformerEncoder, TransformerEncoderLayer layer = TransformerEncoderLayer(d_model=32, nhead=4) encoder = TransformerEncoder(layer, num_layers=3) input_tensor = torch.rand(15, 8, 32) output = encoder(input_tensor) matches this.
  2. Step 2: Verify input tensor shape and num_layers

    Input shape should be (seq_len=15, batch=8, features=32). import torch from torch.nn import TransformerEncoder, TransformerEncoderLayer layer = TransformerEncoderLayer(d_model=32, nhead=4) encoder = TransformerEncoder(layer, num_layers=3) input_tensor = torch.rand(15, 8, 32) output = encoder(input_tensor) uses this shape and 3 layers as required.
  3. Final Answer:

    Code snippet in option A correctly creates and processes the input -> Option A
  4. Quick Check:

    d_model=32, nhead=4, input shape correct [OK]
Hint: Match d_model to features and input shape (seq_len, batch, features) [OK]
Common Mistakes:
  • Swapping batch and sequence dimensions
  • Using nhead that doesn't divide d_model
  • Mismatching d_model and input feature size
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