Learned Position Embeddings › Position Similarity Analysis
Language AI HandbookAll chapters Learned Position Embeddings › Position Similarity AnalysisOne section · Sections in this chapter6 / 19 ↓
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
One way to understand what position embeddings have learned is to compute similarities between positions. If the model uses position for word order and syntax, nearby positions should be more similar than distant ones.
The cosine similarity between two position embeddings and measures how aligned their directions are in the embedding space:
where:
- : the dot product of position embeddings and
- : the Euclidean norm of position embedding
- : a value of 1 means identical direction, 0 means orthogonal, -1 means opposite direction
When we compute this similarity for all pairs of positions and visualize the result as a matrix, we get a window into the geometric structure that training has imposed on the position embeddings. If positions are organized smoothly, we expect a band of high similarity along the diagonal, gradually fading to lower values as the distance between positions increases.
def cosine_similarity_matrix(embeddings):
"""Compute pairwise cosine similarities between embeddings."""
# Normalize each embedding to unit length
norms = np.linalg.norm(embeddings, axis=1, keepdims=True)
normalized = embeddings / (norms + 1e-8)
# Cosine similarity = dot product of normalized vectors
return normalized @ normalized.T
# Compute similarity for trained embeddings
sim_matrix = cosine_similarity_matrix(trained_embeddings)
The similarity matrix reveals the structure learned by position embeddings. The bright diagonal indicates that each position is most similar to itself (similarity = 1). The gradual darkening as we move away from the diagonal shows that nearby positions are more similar than distant ones. This band structure is characteristic of well-trained position embeddings. The pattern differs from sinusoidal encodings, which produce a more regular, wave-like similarity structure. Learned embeddings develop their own characteristic shape based on how positions are used in the training data.
Let's quantify how similarity decreases with distance:
def similarity_by_distance(sim_matrix, max_distance=30):
"""Compute average similarity as a function of position distance."""
n = sim_matrix.shape[0]
distances = []
avg_similarities = []
for d in range(max_distance + 1):
sims = []
for i in range(n - d):
sims.append(sim_matrix[i, i + d])
distances.append(d)
avg_similarities.append(np.mean(sims))
return np.array(distances), np.array(avg_similarities)
distances, avg_sims = similarity_by_distance(sim_matrix, max_distance=40)
The decay curve shows that similarity drops off smoothly with distance. Positions 1 apart have high similarity (around 0.9), while positions 20 or more apart have near-zero similarity. This pattern allows the model to detect relative position through embedding similarity: if two position embeddings are very similar, the positions are likely close together. Notice that this relative position information emerges implicitly from absolute position embeddings. The model does not explicitly store "distance 3" as a concept; it learns absolute positions whose geometry happens to encode relative distance as a byproduct.