Skip to content
RED SAGSkills
easyMachine Learning Engineer interview question

Explain in simple terms how self-attention works in a transformer.

What the interviewer wants to hear

  • Each token creates query, key and value vectors
  • Scores are query-key dot products scaled by sqrt(d_k)
  • Softmax turns scores into weights over all tokens
  • Output is the weighted sum of value vectors
  • Multiple heads learn different relationships; a causal mask hides future tokens

Model answer

Self-attention lets every token look at every other token and decide which ones matter for it. Each token embedding is projected into three vectors: a query, a key and a value. To update one token, we take the dot product of its query with every key, divide by the square root of the key size to keep values stable, and apply softmax to get attention weights that sum to one. The new representation is the weighted sum of the value vectors. So in "the animal did not cross the street because it was tired", the token "it" can put high weight on "animal". Multi-head attention runs several of these in parallel so different heads can capture different relations. In decoder models a causal mask stops tokens from attending to future positions.

Can you answer it under time pressure?

Take a timed easy round and get scored on every answer.

Start mock interview

More easy Machine Learning Engineer questions