cosine_score: Average cosine similarity to a baseline of embeddings

View source: R/dfr_cosine.R

cosine_scoreR Documentation

Average cosine similarity to a baseline of embeddings

Description

Scores every new observation (an embedding vector, such as the sentence embedding of a text) by its average cosine similarity with the embeddings of a baseline, the data drift check of the custom Amazon SageMaker Model Monitor for NLP models of Nigenda et al. (2022):

s(x) = \frac{1}{N}\sum_{i=1}^{N} \frac{x \cdot e_i}{\|x\| \|e_i\|}.

Inputs far from the baseline distribution get low scores.

Usage

cosine_score(reference, x)

Arguments

reference

The baseline embeddings: a numeric matrix or data frame, one row per embedding.

x

The embeddings to score, with the same columns (or a single embedding as a numeric vector).

Details

The average of the cosine similarities equals the inner product of the normalised input with the mean of the normalised baseline embeddings, so scoring costs one inner product per input instead of one per baseline embedding (the reference code loops over the baseline). Baseline embeddings with zero norm are left out, and an input with zero norm gets NA (the cosine is undefined; the reference code gets NaN).

Value

A numeric vector with the score of every row of x.

References

Nigenda, D., Karnin, Z., Zafar, M. B., Ramesha, R., Tan, A., Donini, M. and Kenthapadi, K. (2022). Amazon SageMaker Model Monitor: A system for real-time insights into deployed machine learning models. In Proceedings of the 28th ACM SIGKDD Conference on Knowledge Discovery and Data Mining, 3671-3681. doi:10.1145/3534678.3539145

See Also

dfr_cosine() to monitor a stream.

Examples

set.seed(1)
baseline <- matrix(stats::rnorm(2000, mean = 1), ncol = 20)
near <- matrix(stats::rnorm(100, mean = 1), ncol = 20)
far <- matrix(stats::rnorm(100, mean = -1), ncol = 20)
cosine_score(baseline, near)
cosine_score(baseline, far)

heimdall documentation built on Dec. 2, 2026, 5:08 p.m.