Problems with Cosine as a Measure of Embedding Similarity for High Frequency Words (2022)

Abstract: Cosine similarity of contextual embeddings is used in many NLP tasks (e.g., QA, IR, MT) and metrics (e.g., BERTScore). Here, we uncover systematic ways in which word similarities estimated by cosine over BERT embeddings are understated and trace this effect to training data frequency. We find that relative to human judgements, cosine similarity underestimates the similarity of frequent words with other instances of the same word or other words across contexts, even after controlling for polysemy and other factors. We conjecture that this underestimation of similarity for high frequency words is due to differences in the representational geometry of high and low frequency words and provide a formal argument for the two-dimensional case.

## introduction measuring semantic similarity plays a critical role in numerous nlp tasks like qa, ir, and mt. many such metrics are based on the cosine similarity between the contextual embeddings of two words (e.g., bertscore, moverscore, bertr, semdist; @xcite @xcite . here, we demonstrate that cosine similarity when used with bert embeddings is highly sensitive to training data frequency. the impact of frequency on accuracy and reliability has mostly been studied on static word embeddings like word2vec. low frequency words have low reliability in neighbor judgements @xcite , and yield smaller inner products @xcite with higher variance @xcite . frequency also correlates with stability (overlap in nearest neighbors) @xcite , and plays a role in word analogies and bias @xcite @xcite . similar effects have been found in contextual embeddings, particularly for low-frequency senses, which seem to cause difficulties in wsd performance for bert and roberta @xcite @xcite . other works have examined how word frequency impacts the similarity of sentence embeddings @xcite . while previous work has thus mainly focused on reliability or stability of low frequency words or senses, our work asks: how does frequency impact the semantic similarity of high frequency words? we find that the cosine of bert embeddings underestimates the similarity of high frequency words (to other tokens of the same word or to different words) as compared to human judgements. in a series of regression studies, we find that this underestimation persists even after controlling for confounders like polysemy, part-of-speech, and lemma. we conjecture that word frequency induces such distortions via differences in the representational geometry. we introduce new methods for characterizing geometric properties of a word's representation in contextual embedding space, and offer a formal argument for why differences in representational geometry affect cosine similarity measurement in the two-dimensional case. 1 ## effect of frequency on cosine similarity to understand the effect of word frequency on cosine between bert embeddings @xcite , we first approximate the training data frequency of each word in the bert pre-training corpus from a combination of the march 1, 2020 wikimedia download and counts from bookcorpus @xcite . 2 we then consider two datasets that include pairs of words in context with associated human similarity judgements of words: word-in-context (wic) (expert-judged pairs of sentences with a target lemma used in either the same or different wordnet, wiktionary, or verbnet senses) and stanford contextualized word similarity dataset (scws) (non-expert judged pairs of sentences annotated with human ratings of the similarity of two target terms). using datasets with human similarity scores allows us to account for human perceived similarities when measuring the impact of frequency on cosine (pilehvar and camacho-collados, 2019; @xcite . ## minimum bounding hyperspheres in order to understand why frequency influences cosine similarity, we analyze the geometry of the contextual embeddings. unlike static vectors -where each word type is represented by a single point -the variation in contextualized embeddings depends on a word's frequency in training data. we'll call embeddings of a single word type sibling embeddings or a sibling cohort. to measure variation, we'll use the radius of the smallest hypersphere that contains a set of sibling embeddings (the minimum bounding hypersphere). we tested many ways to measure the space created by high-dimensional vectors. our results are robust to various other measures of variation, including taking the average, max, or variance of pairwise distance between sibling embeddings, the average norm of sibling embeddings, and taking the pca of these vectors and calculating the convex hull of sibling embeddings in lower dimensions (see for a sample of 39,621 words, for each word we took 10 instances of its sibling embeddings (example sentences queried from wikipedia), created contexutalized word embeddings using hugging face's bert-base-cased model, and calculated the radius of the minimum bounding hypersphere encompassing them. 8 9 as shown in figure 3 , there is a significant, strong positive correlation between frequency and size of bounding hypersphere (pearson's @xmath0.62, @xmath1.001). notably, since the radius was calculated in 768 dimensions, an increase in radius of 1% results in a hypersphere volume nearly 2084 times larger. 10 since frequency and polysemy are highly correlated, we want to measure if frequency is a significant feature for explaining the variance of bound-8 words were binned by frequency and then sampled in order to sample a range of frequencies. as a result, there is a zipfian effect causing there to be slightly more words in the lower ranges of each bin. we used https://pypi.org/ project/miniball/ 9 given the sensitivity of minimum bounding hypersphere to outliers, we'd imagine that frequency-based distortions would be even more pronounced had we chosen to use more instances of sibling embeddings. ## discussion and conclusion cosine distance underestimates compared to humans the semantic similarity of frequent words in a variety of settings (expert versus non-expert judged, and within word sense and across words). this finding has large implications for downstream tasks, given that single-point similarity metrics are used in a variety of methods and experiments @xcite @xcite @xcite . word frequency in pre-training data also affects the representational geometry of contextualized embeddings, low frequency words be-ing more concentrated geometrically. one extension of this work might examine how variables such as sentiment and similarity/dissimilarity between sentence contexts could impact both human-judged and embedding-based similarity metrics. because training data frequency is something that researchers can control, understanding these distortions is critical to training large language models. frequency-based interventions might even be able to correct for these systematic underestimations of similarity (e.g., by modifying training data), which could be important where certain words or subjects may be inaccurately represented. for example, @xcite illustrates how training data frequencies can lead to discrepancies in the representation of countries, and-since frequency is highly correlated with a country's gdp-can perpetuate historic power and wealth inequalities. future work could also examine how and if frequency effects could be mitigated by post-processing techniques which improve the correlation between human and semantic similarities (timkey and van schijndel, 2021). the semantic similarity distortions caused by the over-and under-representation of topics is another reason why documentation for datasets is critical for increasing transparency and accountability in machine learning models @xcite @xcite @xcite . as language models increase in size and training data becomes more challenging to replicate, we recommend that word frequencies and distortions be revealed to users, bringing awareness to the potential inequalities in datasets and the models that are trained on them. in the future, we hope to see research that more critically examines the downstream implications of these findings and various mitigation techniques for such distortions. david mimno and laure thompson. 2017. the strange geometry of skip-gram with negative sampling. in proceedings of the 2017 conference on empirical methods in natural language processing, pages 2873-2878, copenhagen, denmark. margaret mitchell, simone wu, andrew zaldivar, parker barnes, lucy vasserman, ben hutchinson, elena spitzer, inioluwa deborah raji, and timnit gebru. 2019. model cards for model reporting. in proceedings of the conference on fairness, accountability, and transparency, fat* '19, page 220-229, new york, ny, usa. association for computing machinery. mohammad taher pilehvar and jose camacho-collados. 2019. wic: the word-in-context dataset for evaluating context-sensitive meaning representations. in proceedings of the 2019 conference of the north american chapter of the association for computational linguistics: human language technologies, volume 1 (long and short papers), pages 1267-1273, minneapolis, minnesota. marten postma, ruben izquierdo bevia, and piek vossen. 2016. more is not always better: balancing sense distributions for all-words word sense disambiguation. in proceedings of coling 2016, the 26th international conference on computational linguistics: technical papers, pages 3496-3506, osaka, japan. the coling 2016 organizing committee. emily reif, ann yuan, martin wattenberg, fernanda b viegas, andy coenen, adam pearce, and been kim. 2019. visualizing and measuring the geometry of bert. in advances in neural information processing systems, volume 32. curran associates, inc. nils reimers and iryna gurevych. 2019. sentence-bert: sentence embeddings using siamese bertnetworks. in proceedings of the 2019 conference on empirical methods in natural language processing and the 9th international joint conference on natural language processing (emnlp-ijcnlp), pages 3982-3992, hong kong, china. william timkey and marten van schijndel. 2021. all bark and no bite: rogue dimensions in transformer language models obscure representational quality. in proceedings of the 2021 conference on empirical methods in natural language processing, pages 4527-4546, online and punta cana, dominican republic. laura wendlandt, jonathan k. kummerfeld, and rada mihalcea. 2018. factors influencing the surprising instability of word embeddings. in proceedings of the 2018 conference of the north american chapter of the association for computational linguistics: human language technologies, volume 1 (long papers), pages 2092-2102, new orleans, louisiana. tianyi zhang, varsha kishore, felix wu*, kilian q. weinberger, and yoav artzi. 2020. bertscore: evaluating text generation with bert. in international conference on learning representations. jieyu zhao, tianlu wang, mark yatskar, vicente ordonez, and kai-wei chang. 2018. gender bias in coreference resolution: evaluation and debiasing methods. in proceedings of the 2018 conference of the north american chapter of the association for computational linguistics: human language technologies, volume 2 (short papers), pages 15-20, new orleans, louisiana. wei zhao, maxime peyrard, fei liu, yang gao, christian m. meyer, and steffen eger. 2019. moverscore: text generation evaluating with contextualized embeddings and earth mover distance. in proceedings of the 2019 conference on empirical methods in natural language processing and the 9th international joint conference on natural language processing (emnlp-ijcnlp), pages 563-578, hong kong, china. kaitlyn zhou, kawin ethayarajh, and dan jurafsky. 2022. richer countries and richer representations. in proceedings of the 60th annual meeting of the association for computational linguistics. yukun zhu, ryan kiros, rich zemel, ruslan salakhutdinov, raquel urtasun, antonio torralba, and sanja fidler. 2015. aligning books and movies: towards story-like visual explanations by watching movies and reading books. in 2015 ieee international conference on computer vision (iccv), pages 19-27.