Weight Tying › The Two Embedding Matrices
Language AI HandbookAll chapters Weight Tying › The Two Embedding MatricesOne section · Sections in this chapter2 / 15 ↓
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
Every language model that processes text through a transformer architecture faces the same fundamental challenge at its boundaries. Transformers operate on continuous vectors, but text consists of discrete tokens. Bridging this gap requires learned mappings in both directions: from tokens to vectors at the input, and from vectors back to tokens at the output. These two mappings are implemented as the input embedding matrix and the output projection matrix, and understanding them precisely is the first step toward appreciating why tying them together makes sense.
Think of the transformer as a black box that maps sequences of dense vectors into new sequences of dense vectors. Before the transformer can do its work, you need to convert your token indices into vectors it can process. After the transformer produces its output, you need to convert those output vectors back into probabilities over the vocabulary. The two embedding matrices handle these conversion tasks.
Both matrices have the same shape, both operate over the same vocabulary, and both are involved at every single forward pass through the model. Yet in naive implementations, they are treated as entirely separate learned objects. Weight tying questions whether this separation is necessary or even desirable.
The Input Embedding Matrix
The input embedding matrix is one of the most fundamental components in any neural language model. It stores a learned dense vector representation for each token in the vocabulary. When the model sees token index , it retrieves the corresponding row from this matrix:
where:
- : the input embedding matrix of shape , where every row contains the learned representation of one vocabulary token
- : the input token index, an integer from 0 to
- : the resulting embedding vector of dimension , which enters the transformer's processing pipeline
- : the vocabulary size (50,257 for GPT-2, 32,000 for LLaMA-2, 128,000 for newer models)
- : the embedding dimension, also called the model dimension or hidden dimension
This operation is purely a table lookup. Token 42 retrieves row 42 from the matrix. There is no computation beyond the indexing, which is one reason why embedding lookups are extremely fast in practice. The entire operation can be implemented as a gather operation on the weight matrix.
Over the course of training, the rows of are shaped by gradient descent to capture the semantic and syntactic properties of each token. Tokens that appear in similar contexts will develop similar embeddings. Tokens that serve similar grammatical roles will develop representations that are geometrically related. The embedding matrix effectively becomes a learned dictionary that maps the discrete world of tokens to the continuous world of vector spaces.
The Output Projection Matrix
The output projection matrix , also called the language model head or LM head, performs the inverse operation. After the transformer has processed the input sequence and produced a final hidden state , the model needs to decide which token should come next. This decision is made by computing a score for every token in the vocabulary:
where:
- : the output logits vector of length , one unnormalized score per vocabulary token
- : the output projection matrix of shape , where each row encodes the "target pattern" for one vocabulary token
- : the final hidden state from the last transformer layer, a vector of dimension
Each row of contains what you might call the "output embedding" for one vocabulary token. The full matrix multiplication computes the dot product between the hidden state and each token's output embedding in one vectorized operation, yielding a score for every token in the vocabulary simultaneously.
After computing these logits, we apply the softmax function to convert them into a proper probability distribution over the vocabulary for the next token prediction:
where:
- : the probability of token being the correct next token given the context
- : the logit for token , computed as where is row of
- : the exponential applied to logit , which ensures all values are positive
- : the sum of all exponentials across the full vocabulary, serving as a normalizing constant
Why does this formula make sense? Notice that taking the exponential of each logit guarantees positive values regardless of the sign of the original logit, which is a prerequisite for valid probabilities. The denominator then ensures all values sum to exactly 1. Tokens with higher logits, those whose output embeddings more closely align with the hidden state, receive higher probabilities.
The Critical Observation: Identical Shapes
Notice the dimensional symmetry: both and have exactly the same shape, . Both have rows, one per vocabulary token. Both have columns, one per hidden dimension. Both map between the same two spaces: the discrete token space with elements and the continuous embedding space with dimensions.
This symmetry is not a coincidence. It reflects the deeper fact that both matrices perform the same abstract function: they define a correspondence between tokens and points in a -dimensional vector space. The input embedding says "token lives at point ." The output projection says "a hidden state should produce token with probability proportional to how much it resembles ." These two statements describe the same geometric relationship from opposite directions.
import torch.nn as nn
# Typical model dimensions
vocab_size = 50000
hidden_dim = 768
# Without weight tying: two separate matrices
input_embedding = nn.Embedding(vocab_size, hidden_dim)
output_projection = nn.Linear(hidden_dim, vocab_size, bias=False)
# Count parameters
input_params = vocab_size * hidden_dim
output_params = hidden_dim * vocab_size
total_untied = input_params + output_paramsParameter count without weight tying: Input embedding: 38,400,000 parameters Output projection: 38,400,000 parameters Total: 76,800,000 parameters Both matrices have identical size: 50,000 x 768 That's 76.8M parameters just for the two embedding matrices.
With a vocabulary of 50,000 tokens and hidden dimension of 768, each matrix contains over 38 million parameters. Together, they account for nearly 77 million parameters. For a model like GPT-2 Small, which has about 117 million total parameters, this means over 65 percent of the model's capacity lives in these two identically sized matrices. The question weight tying asks is: do we really need both?