SubtractEmbeddings
Subtracts embedding vector v2 from v1 and returns the result as a normalized vector.
Format
SubtractEmbeddings ( v1 ; v2 )
Parameters
v1
and v2
- any text expression, text field, or container field that contains embedding vectors with the same dimensions.
Data type returned
text, container
Originated in version
22.0
Description
This function performs vector subtraction (v1
- v2
) on two embedding vectors and returns the result as a normalized vector. Normalization focuses the resulting vector on its semantic direction rather than its magnitude, which is standard practice for comparing or manipulating embeddings.
Use this function to isolate or modify semantic concepts by removing the meaning of one vector (v2
) from another (v1
). For example, subtracting the vector for "cold" from the vector for "winter" might result in a vector representing "winter aspects unrelated to cold," potentially leaning towards concepts like "mild winter" or simply "season." A famous analogy is that the vector for "king" minus the vector for "man" plus the vector for "woman" results in a vector very close to "queen."
If v1
and v2
are text, they must be in the form of JSON arrays. Typically, though, using embedding vectors as binary container data improves performance.
Notes
-
Embedding vectors must be generated from the same model to ensure compatibility and performance; mixing embedding vectors from different models isn't supported.
-
This function returns "?" if:
-
If
v1
andv2
have different dimensions -
Or the result is a zero vector (which can happen if v1 and v2 are identical), because the function can't normalize a zero vector
-
Example 1
SubtractEmbeddings ( "[1, 2, 3]" ; "[4, 5, 6]" )
returns [-0.57735026918962573106, -0.57735026918962573106, -0.57735026918962573106]. The subtraction is [1-4, 2-5, 3-6] = [-3, -3, -3]. Then the function normalizes this vector and returns it as a JSON array because both inputs were text.
Example 2
SubtractEmbeddings ( Concepts::Winter_Embedding ; Concepts::Cold_Embedding )
returns a container object containing the normalized vector representing the concept of "winter" with the concept of "cold" removed.
This example assumes Concepts::Winter_Embedding contains the embedding vector for "winter" and Concepts::Cold_Embedding contains the embedding vector for "cold". The resulting vector could be used with the Perform Semantic Find script step to find records discussing winter in contexts where cold is not the primary focus (for example, winter fashion, winter holidays).
Example 3
To demonstrate the "king - man + woman ≈ queen" analogy, you can combine the use of SubtractEmbeddings and AddEmbeddings. Assume you have a Concepts table with fields ConceptName and ConceptVector containing embeddings for "King," "Man," "Woman," and "Queen".
Set Variable [ $kingMinusMan ; Value: SubtractEmbeddings ( Concepts::King_Embedding ; Concepts::Man_Embedding ) ]
Set Variable [ $queenAnalogyEmbedding ; Value: AddEmbeddings ( $kingMinusMan ; Concepts::Woman_Embedding ) ]
The variable $queenAnalogyEmbedding now holds a container object with the normalized vector resulting from the analogy calculation. You could then test the analogy using:
CosineSimilarity ( $queenAnalogyEmbedding ; Concepts::Vector_Queen )
A result close to 1 would indicate the analogy holds well for the embedding model used.