Skip to content
elevatedevco

LLMs & Agents

Attention and embeddings

Learn Attention and embeddings: 34 practice questions and answers with an interactive quiz — from the offline AI Learning app.

Advanced34 questions34 quiz questions

Questions & Answers

Tap a question to reveal its answer. Written for self-study and interview preparation.

What is a word embedding, and why is it more useful than representing words as one-hot vectors?

A word embedding is a dense, relatively low-dimensional vector of real numbers that represents a word so that similar meanings end up close together in the vector space. Unlike one-hot vectors, which are huge, sparse, and treat every word as equally different from every other, embeddings capture semantic relationships and let models generalize across related words. They are usually learned from data rather than assigned by hand.

In an embedding space, what does it usually mean geometrically when two word vectors have a high cosine similarity?

A high cosine similarity (close to 1) means the two vectors point in nearly the same direction, which the model has learned to associate with words used in similar contexts or with related meanings. Cosine similarity focuses on direction rather than magnitude, so even words appearing at different frequencies can be judged as semantically close. For example, 'king' and 'queen' typically have higher cosine similarity than 'king' and 'banana'.

The famous analogy 'king - man + woman ≈ queen' demonstrates what property of learned word embeddings?

It demonstrates that embeddings can capture certain relationships as consistent directions (vector offsets) in the space, so adding and subtracting vectors performs a kind of analogical reasoning. The gender difference between 'man' and 'woman' corresponds to roughly the same offset as between 'king' and 'queen'. This linear structure emerges naturally when embeddings are trained on large text corpora.

In one sentence, what core idea underlies an attention mechanism in neural networks?

Attention lets a model decide, for each position it is processing, which other parts of the input are most relevant and weight them accordingly. Instead of treating all inputs equally, the model computes a set of weights that sum to one and forms a weighted combination of values. This allows it to focus on the most informative pieces of context.

In the attention mechanism, what are the roles of the Query, Key, and Value vectors?

The Query represents what the current position is looking for, the Keys describe what each input position offers, and the Values carry the actual information to be retrieved. Attention compares the Query against all Keys to produce relevance scores, then uses those scores to take a weighted sum of the Values. This is loosely analogous to looking up information in a database by matching a search query against keys.

Write the formula for scaled dot-product attention and explain why the scaling factor is used.

Scaled dot-product attention is computed as softmax(QKᵀ / √dₖ) · V, where dₖ is the dimension of the key vectors. The division by √dₖ keeps the dot products from growing too large in magnitude, which would otherwise push the softmax into regions with extremely small gradients. This scaling keeps training stable and gradients useful.

import numpy as np

def attention(Q, K, V):
    dk = Q.shape[-1]
    scores = Q @ K.T / np.sqrt(dk)
    weights = np.exp(scores - scores.max(axis=-1, keepdims=True))
    weights /= weights.sum(axis=-1, keepdims=True)
    return weights @ V
Why is the softmax function applied to attention scores before they are used to combine the value vectors?

Softmax turns the raw relevance scores into a set of non-negative weights that sum to one, so they behave like a probability distribution over the input positions. This makes the output a proper weighted average of the values, where larger scores get proportionally larger influence. It also makes the mechanism differentiable so it can be trained with gradient descent.

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

In self-attention, the Queries, Keys, and Values all come from the same sequence, so each position attends to other positions within the same input. In cross-attention, the Queries come from one sequence while the Keys and Values come from a different sequence, such as a decoder attending to an encoder's output. Self-attention builds internal context; cross-attention links two different sequences.

What is multi-head attention, and what advantage does it provide over a single attention head?

Multi-head attention runs several attention operations in parallel, each with its own learned projections of the Queries, Keys, and Values, and then concatenates their outputs. This lets different heads focus on different types of relationships, such as one tracking syntax and another tracking long-range references. The result is a richer representation than any single head could produce.

Why do Transformer models need positional encodings added to their input embeddings?

Self-attention treats its inputs as an unordered set and has no built-in notion of sequence order, so without extra information it cannot tell whether a word came first or last. Positional encodings add order information to each token's embedding so the model can distinguish positions and understand sequence structure. This is essential for language, where word order changes meaning.

How does the Word2Vec skip-gram model learn word embeddings?

Skip-gram trains a model to predict the surrounding context words given a single center word, sliding this task across a large corpus. The weights that the network learns for each word become its embedding, so words appearing in similar contexts develop similar vectors. This reflects the distributional hypothesis: words with similar contexts tend to have similar meanings.

What is the distributional hypothesis, and how does it relate to embeddings?

The distributional hypothesis states that words appearing in similar contexts tend to have similar meanings, often summarized as 'a word is known by the company it keeps.' Embedding methods exploit this by learning vectors from the contexts in which words occur, so context-sharing words end up nearby in the vector space. It is the core linguistic justification for learning meaning from raw text.

What is the key difference between static embeddings (like Word2Vec) and contextual embeddings (like those from BERT)?

Static embeddings assign each word a single fixed vector regardless of the sentence it appears in, so 'bank' has the same vector whether it means a riverbank or a financial bank. Contextual embeddings are produced by a model that reads the whole sentence, so the same word gets different vectors depending on context. This lets contextual models handle polysemy (multiple meanings) far better.

In attention, what is an 'attention weight' and what is its typical range of values?

An attention weight is the normalized score, produced by softmax, that tells how much a given input position contributes to the output for a particular query. Because softmax outputs are non-negative and sum to one across all positions, each individual weight lies between 0 and 1. A weight near 1 means the model is focusing almost entirely on that position.

Why is the dot product a natural choice for measuring similarity between a Query and a Key in attention?

The dot product is large and positive when two vectors point in similar directions and have substantial magnitude, so it acts as an unnormalized similarity measure. This lets attention score a Query against each Key cheaply using fast matrix multiplication. Higher dot products lead to higher attention weights after softmax, focusing the model on better-matching keys.

What is a causal (masked) self-attention mask, and why is it used in language-generation models?

A causal mask prevents each position from attending to positions that come later in the sequence, typically by setting their scores to negative infinity before softmax. This is needed in autoregressive generation so that when predicting a token, the model cannot 'peek' at future tokens it is supposed to predict. It enforces that the model only uses past and present context.

What does the embedding dimension (e.g., 300 or 768) represent, and what is the trade-off in choosing it?

The embedding dimension is the number of components in each word's vector, determining how much information the representation can hold. Larger dimensions can capture more nuanced relationships but require more memory and data and risk overfitting, while smaller ones are efficient but may lose detail. Choosing it balances expressiveness against computational cost.

Why is cosine similarity often preferred over Euclidean distance when comparing word embeddings?

Cosine similarity measures the angle between vectors, ignoring their magnitude, which often correlates with word frequency rather than meaning. This makes it focus on the direction that encodes semantic content, so frequent and rare words can still be compared fairly. Euclidean distance, by contrast, can be dominated by magnitude differences that are not semantically meaningful.

If the attention scores for three positions are [2.0, 1.0, 0.0], roughly what attention weights does softmax produce?

Softmax exponentiates each score and normalizes, giving e², e¹, e⁰ which are about 7.39, 2.72, and 1.0, summing to roughly 11.1. Dividing gives approximately 0.67, 0.24, and 0.09. So the first position receives the most attention, reflecting its highest score.

import numpy as np
scores = np.array([2.0, 1.0, 0.0])
w = np.exp(scores) / np.exp(scores).sum()
print(np.round(w, 2))  # [0.67 0.24 0.09]
What is an embedding layer in a neural network, and how are its values typically obtained?

An embedding layer is essentially a lookup table that maps each token's integer ID to a learnable vector. During training, these vectors are updated by backpropagation just like any other weights, so they adapt to the task. At inference time, the layer simply retrieves the stored vector for each input token.

import torch
import torch.nn as nn

emb = nn.Embedding(num_embeddings=10000, embedding_dim=300)
tokens = torch.tensor([1, 42, 7])
vectors = emb(tokens)  # shape: (3, 300)
How did attention mechanisms help solve the long-range dependency problem that plagued earlier RNNs?

RNNs process tokens sequentially and compress everything into a hidden state, so information from far-back tokens can fade or get distorted before it is needed. Attention lets any position directly access any other position in a single step, regardless of distance, giving a constant-length path between tokens. This makes capturing long-range relationships much easier and more reliable.

What does it mean that self-attention is 'permutation equivariant', and why does this matter?

Permutation equivariant means that if you reorder the input tokens, the self-attention outputs are reordered in exactly the same way, with no inherent sense of position. This matters because it shows the mechanism ignores order by itself, which is why positional information must be injected separately. Without added position signals, the model would treat 'dog bites man' and 'man bites dog' identically.

What is the approximate computational complexity of standard self-attention with respect to sequence length n, and why?

Standard self-attention is O(n²) in both time and memory because every one of the n positions computes a score against every other position, forming an n×n attention matrix. This quadratic cost is why very long sequences become expensive. It motivates research into more efficient 'sparse' or 'linear' attention variants.

In the original 'Attention Is All You Need' Transformer, why was attention preferred over recurrence and convolution?

Attention allows all positions to be processed in parallel rather than step by step, dramatically speeding up training compared to sequential RNNs. It also creates short, direct paths between any pair of tokens, making it easier to learn long-range dependencies than with convolutions that have limited receptive fields. These advantages let Transformers scale to large datasets effectively.

What is the difference between token embeddings and sentence/document embeddings?

Token embeddings represent individual words or subwords as vectors, while sentence or document embeddings represent an entire span of text as a single vector. Sentence embeddings are often produced by pooling or combining token embeddings, capturing overall meaning for tasks like semantic search or clustering. They operate at different granularities of language.

Why might attention weights NOT always be a reliable explanation of a model's reasoning?

Although attention weights show where the model placed focus, high weight does not necessarily mean that information caused the final prediction, since other components and residual paths also shape the output. Research has shown that different attention distributions can yield the same predictions, so attention is suggestive but not a definitive explanation. They should be interpreted with caution rather than as ground-truth reasoning.

What is subword tokenization (e.g., Byte-Pair Encoding), and how does it relate to embeddings?

Subword tokenization breaks words into smaller frequent units, like 'playing' into 'play' and 'ing', so the vocabulary stays manageable while still covering rare or new words. Each subword unit gets its own embedding, and a word's representation is built from its pieces. This lets models handle out-of-vocabulary words gracefully instead of mapping them all to a single 'unknown' token.

In cross-attention within an encoder-decoder Transformer, where do the Query, Key, and Value come from?

In the decoder's cross-attention layer, the Queries come from the decoder's own current representations, while the Keys and Values come from the encoder's output. This lets each decoding step look back at the encoded source sequence and pull in relevant information. It is the bridge that connects the input being processed to the output being generated, as in machine translation.

Why are word embeddings often visualized in 2D using techniques like t-SNE or PCA?

Embeddings live in high-dimensional spaces (hundreds of dimensions) that humans cannot directly see, so dimensionality-reduction methods like PCA or t-SNE project them down to 2D or 3D. These plots reveal clusters of related words and help us inspect whether the embedding captured meaningful structure. The visualization is approximate, since some information is inevitably lost in the reduction.

What problem can arise if a word embedding model is trained on biased text, and why is this important?

If the training text contains social biases, the embeddings can absorb and reproduce them, for example associating certain professions more strongly with one gender. Because downstream systems use these vectors, the bias can propagate into hiring tools, search, or recommendations and cause unfair outcomes. This makes auditing and mitigating bias in embeddings an important ethical concern.

In attention, what is the purpose of the learned weight matrices that produce Q, K, and V from the input embeddings?

Each input embedding is multiplied by separate learned matrices (W_Q, W_K, W_V) to project it into Query, Key, and Value spaces tailored to the attention task. These projections let the model decide what aspects of each token matter for asking, matching, and carrying information. Because they are learned, the model can adapt how it forms relevance comparisons during training.

How does GloVe differ in approach from Word2Vec when learning embeddings?

GloVe learns embeddings by factorizing a global word-word co-occurrence count matrix, directly using corpus-wide statistics about how often words appear together. Word2Vec instead learns from local prediction tasks over sliding context windows. Both aim to place similar words nearby, but GloVe emphasizes global counts while Word2Vec emphasizes local context prediction.

Why does masked attention set blocked scores to negative infinity (rather than zero) before applying softmax?

Setting a score to negative infinity ensures that after softmax its exponential becomes effectively zero, giving that position exactly zero attention weight. Setting the raw score to zero instead would still produce a nonzero softmax weight (e⁰ = 1), so the position would leak some influence. Negative infinity is the clean way to fully exclude positions like future tokens.

What does it mean to use 'pre-trained embeddings' via transfer learning, and what is the benefit?

Using pre-trained embeddings means starting with vectors already learned on a large general corpus and reusing them in a new, often smaller, task. This transfers general knowledge about word meanings so the new model needs less data and trains faster than learning embeddings from scratch. It is a common and effective form of transfer learning in NLP.

Put it into practice with AI Learning

This topic — and hundreds more — comes from our free, offline AI Learning app. Practice Q&A, quizzes and cheat sheets anywhere, no account needed.

  • Free
  • No account
  • Works offline
Coming soon toGoogle Play