Values between -1.0 and 1.0
PART 3 — Embeddings¶
An embedding converts a string into a dense vector (array of floats) so we can measure geometric distance between concepts.
Example:
Imagine a tiny 2-dimensional embedding model mapping words to [Royalty, Gender].
# Values between -1.0 and 1.0
king = [ 0.9, 0.9] # Highly royal, highly male
queen = [ 0.9, -0.9] # Highly royal, highly female
apple = [-0.8, 0.0] # Not royal, neutral gender
Calculating Semantic Similarity (Dot Product):
import numpy as np
def similarity(vec1, vec2):
return np.dot(vec1, vec2)
print(similarity(king, queen)) # (0.9*0.9) + (0.9*-0.9) = 0.81 - 0.81 = 0.0 (Different gender)
print(similarity(king, apple)) # (0.9*-0.8) + (0.9*0) = -0.72 (Very unrelated)