Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is Cosine-Similarity of Embeddings Really About Similarity? #54

Open
shnakazawa opened this issue Aug 26, 2024 · 2 comments
Open

Is Cosine-Similarity of Embeddings Really About Similarity? #54

shnakazawa opened this issue Aug 26, 2024 · 2 comments

Comments

@shnakazawa
Copy link
Contributor

Steck, Harald, et al. “Is Cosine-Similarity of Embeddings Really About Similarity?” arXiv [cs.IR], 8 Mar. 2024, http://arxiv.org/abs/2403.05440. arXiv.

  • LLMやレコメンデーションにおいて、コサイン類似度はデータや文章の類似度を測る尺度として一般に使われている。
  • コサイン類似度が低次元の特徴埋め込みにおいて意味のある「類似性」を表現しない場合がある。
  • 本論文ではコサイン類似度が本来の意味的な類似性を適切に捉えられないケースがあることを、理論的な解析とシミュレーション実験で示した。
  • コサイン類似度の使用に対し警鐘を鳴らす論文。

Abstract

Cosine-similarity is the cosine of the angle between two vectors, or equivalently the dot product between their normalizations. A popular application is to quantify semantic similarity between high-dimensional objects by applying cosine-similarity to a learned low-dimensional feature embedding. This can work better but sometimes also worse than the unnormalized dot-product between embedded vectors in practice. To gain insight into this empirical observation, we study embeddings derived from regularized linear models, where closed-form solutions facilitate analytical insights. We derive analytically how cosine-similarity can yield arbitrary and therefore meaningless `similarities.' For some linear models the similarities are not even unique, while for others they are implicitly controlled by the regularization. We discuss implications beyond linear models: a combination of different regularizations are employed when learning deep models; these have implicit and unintended effects when taking cosine-similarities of the resulting embeddings, rendering results opaque and possibly arbitrary. Based on these insights, we caution against blindly using cosine-similarity and outline alternatives.

(DeepL翻訳)

コサイン類似度とは、2つのベクトル間の角度の余弦、または等価的にそれらの正規化間のドット積である。よく使われるアプリケーションは、学習された低次元の特徴埋め込みにコサイン類似度を適用することで、高次元のオブジェクト間の意味的類似性を定量化することである。この方法は、埋め込みベクトル間の正規化されていないドット積よりもうまくいくこともあるが、時には悪くなることもある。この経験的観察に対する洞察を得るために、我々は、閉形式解が解析的洞察を容易にする正則化線形モデルから得られる埋め込みを研究する。コサイン類似度がどのように任意の、したがって無意味な「類似度」をもたらすかを解析的に導出する。ある線形モデルでは、類似度は一意でさえなく、他のモデルでは正則化によって暗黙のうちに制御される。ディープモデルを学習する際には、様々な正則化の組み合わせが採用されるが、これらの正則化は、結果として得られる埋め込みデータの余弦類似度を取る際に、暗黙的かつ意図しない効果をもたらし、結果を不透明で恣意的なものにする。これらの洞察に基づき、我々はコサイン類似度を盲目的に使用することに注意を促し、代替案を概説する。

解決した課題/先行研究との比較

  • LLMやレコメンデーションにおいて、単語やアイテム、ユーザーは類似度をもって埋め込みされている。その類似度の指標として、一般に使われるものがコサイン類似度。
  • コサイン類似度がうまく機能しない例が報告されていたが、その原因は明らかになっていなかった。
  • 本論文では、線形モデルを使用して学習された埋め込みにおいて、解析的解とシミュレーションを用いてコサイン類似度が機能しない理由の考察を行った。

結果

  • 線形行列分解 Matrix Factorization のモデルを学習

    • 行列分解:与えられた行列XをできるだけよくXAB^Tで近似するためのA,Bを求めるモデル。
    • Xは、例えばユーザー×アイテムの行列で、ユーザーがどのアイテムを購入/視聴したかを表すデータ。
    • A, Bはそれぞれユーザー、アイテムの因子行列。
  • 元の行列と、分解された行列から再構成された行列との間の誤差を定量化する目的関数を2種類用意。

    • Image from Gyazo

    • 正則化手法が異なる。

  • 解析解を求めると、正則化手法がスケーリングに異なる影響を与えていることが分かった。

    • 1つ目の目的関数ではベクトルの各次元のスケーリングが任意になる。
    • 2つ目ではスケーリングが一意に定まる。
  • シミュレーションを行うと、コサイン類似度がアイテム間の真の類似度を表現できていないケースが示された (Fig.1 )。

    • 左:真のアイテムの類似度。
    • 中央3つ:目的関数1で学習されたベクトルに対し、異なる方法でスケーリングした結果。
      • 任意に決められるスケーリングの方法次第でコサイン類似度の値が大きく変わってしまっている。
    • 右:目的関数2で学習されたベクトルに対するコサイン類似度。
      - スケーリングが一意に定まる式2でも、コサイン類似度は真の類似度と大きく異なっている
      - (正則化項などの影響により、ベクトル間のコサイン類似度が元のデータの類似度を反映しているとは限らない、ということ?ここの原因(の考察)は明記されていないように見える)
      Image from Gyazo
  • これらの結果より、低次元の特徴埋め込みにおけるコサイン類似度はベクトルのスケーリング、正則化手法に大きく依存する。

議論・残された課題

  • 回避策として、以下が提案されている。
    • Layer normalization
      • (行列分解におけるlayer normalizationとは?)
    • 低次元埋め込み空間でコサイン類似度を計算するのではなく、元の行列空間に戻したうえでコサイン類似度を計算する。
    • 何かしらの正規化を学習前や学習中に適用する。
      • 一般的なのはデータの標準化 (standardization)
      • 深層学習においてはNegative SamplingやInverse Propensity Scaling (IPS) に相当
  • 一方、本論文では線形行列分解のみを検証しており、深層学習などの非線形のモデルでの検証は行われていない。
    • 「線形モデルよりも複雑かつ様々な正則化手法が組み合わされているため、コサイン類似度の問題はより深刻になる可能性がある」と議論されている。

重要な引用

  • R. Jin, D. Li, J. Gao, Z. Liu, L. Chen, and Y. Zhou. Towards a better understanding of linear models for recommendation. In ACM Conference on Knowledge Discovery and Data Mining (KDD), 2021.
    • 目的関数(1)の由来
  • S. Zheng, C. Ding, and F. Nie. Regularized singular value decomposition and application to recommender system, 2018. arXiv:1804.05090.
    • 目的関数(2)の由来
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants
@shnakazawa and others