Data Science & ML

Transformers & Attention

Attention mechanism, self-attention, multi-head attention, Transformer architecture, positional encoding

24 interview questionsยท
Senior
1

What is the main advantage of the attention mechanism over RNNs for sequence processing?

Answer

The attention mechanism allows direct access to any position in the sequence, eliminating the sequential bottleneck of RNNs. Unlike RNNs that must propagate information step by step, attention computes direct connections between all positions, enabling massive parallelization and capturing long-range dependencies without gradient degradation.

2

In the attention mechanism, what do the Query (Q), Key (K) and Value (V) vectors represent?

Answer

Query represents what the token is looking for, Key represents what each token can offer as a match, and Value contains the information to retrieve. The attention score is computed between Q and K to determine relative importance, then used to weight the V. This analogy comes from information retrieval systems where a query is compared to keys to retrieve values.

3

What is the formula for scaled dot-product attention and why divide by the square root of dk?

Answer

The formula is Attention(Q,K,V) = softmax(QK^T / sqrt(dk)) * V. Dividing by sqrt(dk) is crucial because dot products of high-dimensional vectors tend to have large magnitudes, pushing softmax into regions with very small gradients. This normalization maintains stable variance of attention scores, ensuring efficient learning.

4

What is the fundamental difference between attention and self-attention?

5

Why use multi-head attention rather than a single attention head?

+21 interview questions

Master Data Science & ML for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free