KatInterview

Generalization Gaps and the Mechanics of Transformer Attention

Machine LearningModel answer
MediumModel answerMachine Learningoverfittingregularizationtransformers

Last updated

Diagnose and fix overfitting in linear and deep models, then explain self-attention (queries, keys, values, softmax weights) and why and how Transformers inject position information.

You are preparing for a machine learning engineering screen. Answer the five fundamentals questions below. Be precise, justify why each technique works, and point out trade-offs.

  1. Diagnosis. Define overfitting. Which training and validation metrics or plots would you inspect to tell whether a model is overfitting, as opposed to underfitting?
  2. Linear models. Suppose a linear or logistic regression model generalizes poorly. What would you change to reduce overfitting?
  3. Deep networks. Suppose a deep neural network generalizes poorly. What techniques would you apply, and how does your approach differ from the linear case?
  4. Self-attention. Walk through how a Transformer self-attention layer works. What are queries, keys, and values, and how are the attention weights computed and used?
  5. Position. Why does a Transformer need explicit positional information? Describe at least two ways to supply it.

Structure your answer by question number. Use formulas where they clarify, and mention common pitfalls.

Hints

Hint 1

For question 1, think about what happens to the two loss curves over epochs, and what a small gap with both errors high would indicate instead. Also consider whether your validation split could hide the problem (leakage, time ordering).

Hint 2

For questions 2 and 3, group techniques by what they limit: model capacity, weight magnitude, the amount of effective data, or training duration. Then ask which of these groups are explicit penalties and which are implicit.

Hint 3

For question 4, write the formula softmax(QK^T / sqrt(d_k)) V and explain each piece: what each projection is for, why the scaling is there, what the softmax is normalized over, and what the output for one token is.

Hint 4

For question 5, ask what happens to the output if you permute the input tokens. Then list schemes that add position to the embeddings versus schemes that modify the attention scores.