Mathematically, if your input tensor shape is [Batch Size, Sequence Length, Hidden Dimension], a hidden dimension of 150 defines the size of the last axis. For example, in a tensor of shape [32, 50, 150]:
xplain this three 32 is the batch size (breadth of processing).
50 is the sequence length (number of tokens).
150 is the hidden dimension (vector length/depth of features per token).
Finished
Here is the explanation of the three dimensions in the tensor shape [32, 50, 150]:
1. 32 is the Batch Size (Breadth of Processing)
This number represents how many independent sequences (e.g., sentences, documents, or images) the model processes simultaneously in a single step.
- Function: It determines the parallelism of the computation. Instead of processing one sentence at a time, the model processes 32 different sentences together to utilize GPU efficiency.
- Analogy: If the model is a teacher grading essays, the batch size is the number of essays on their desk at once. A size of 32 means they are looking at 32 separate essays in parallel.
2. 50 is the Sequence Length (Number of Tokens)
This number represents the number of individual items (tokens/words) within each of those 32 sequences.
- Function: It defines the "width" of the input for a single sample. In this case, every sentence in the batch has been padded or truncated to contain exactly 50 tokens.
- Analogy: Continuing the teacher analogy, this is the length of each essay. Every essay on the desk has exactly 50 words. The model analyzes the relationship between word 1 and word 50, word 2 and word 49, etc., within each sequence.
3. 150 is the Hidden Dimension (Vector Length/Depth)
This number represents the number of features used to describe each single token.
- Function: It is the size of the vector embedding for every word. Each of the 50 tokens in each of the 32 sentences is converted into a list of 150 numbers. These numbers capture semantic meaning, grammatical role, and context.
- Analogy: This is the depth of analysis for each word. Instead of just seeing the word "run," the model sees a profile of 150 specific characteristics (e.g., is it a verb? is it present tense? does it imply speed?).
Summary Visualization: Imagine a stack of 32 books (Batch Size). Each book has 50 pages (Sequence Length). On each page, there is a detailed report with 150 distinct data points (Hidden Dimension).
The head dimension (often denoted as $d_k$ or $d_v$) is the size of the vector that each individual attention head processes. It represents the "slice" of the total hidden dimension assigned to a single head.
1. The Formula
The head dimension is calculated by dividing the total hidden dimension ($d_{model}$) by the number of attention heads ($h$):
Head Dimension(*dk*)=Number of Heads(*h*)Hidden Dimension(*dmodel*)
• Example: If your hidden dimension is 512 and you have 8 heads, the head dimension is $512 / 8 = \mathbf{64}$.
• Constraint: The hidden dimension must be evenly divisible by the number of heads.