Query, Key, Value › Key Parameters
Language AI HandbookAll chapters Query, Key, Value › Key ParametersOne section · Sections in this chapter9 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
When implementing QKV self-attention, these parameters control the mechanism's capacity and computational cost:
-
embed_dim(d): The dimension of input token embeddings. This determines the input size to the projection matrices. Common values range from 256 to 4096 in production transformers. -
d_k(query/key dimension): The dimension of query and key vectors after projection. Smaller values reduce computation (since attention scores require operations) but limit the expressiveness of query-key matching. Typically set toembed_dim / num_headsin multi-head attention. -
d_v(value dimension): The dimension of value vectors after projection. This determines the output dimension of the attention layer. Usually equalsd_kfor simplicity, but can differ when the output needs a different size than the matching space. -
Initialization scale: The projection matrices are typically initialized with small random values. Xavier/Glorot initialization scales weights by , where is the input dimension and is the output dimension. This scaling helps maintain stable gradient magnitudes during training by keeping the variance of activations approximately constant across layers.
The ratio of d_k to embed_dim represents a compression factor. Setting d_k < embed_dim forces the model to learn a compressed representation for matching, which can act as a form of regularization. Setting d_k = embed_dim preserves full expressiveness but increases memory and computation.