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

Add similarity cosinus #117

Open
ClicShopping opened this issue May 9, 2024 · 1 comment
Open

Add similarity cosinus #117

ClicShopping opened this issue May 9, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@ClicShopping
Copy link

ClicShopping commented May 9, 2024

Hello,
It will be great if we can choose between euclidean and cosinus similarity in LLPhant\Embeddings\VectorStores\DistancesL2Utils.

This 2 approaches have advantage and disadvantage in function what we want to do.

Example :

 public function cosineSimilarity($vector1, $vector2): float|int
  {
    // Calculate the dot product of the two vectors
    $dotProduct = array_sum(array_map(function ($a, $b) {
      return $a * $b;
    }, $vector1, $vector2));

    // Calculate the magnitudes of each vector
    $magnitude1 = sqrt(array_sum(array_map(function ($a) {
      return $a * $a;
    }, $vector1)));

    $magnitude2 = sqrt(array_sum(array_map(function ($a) {
      return $a * $a;
    }, $vector2)));

    // Avoid division by zero
    if ($magnitude1 * $magnitude2 == 0) {
      return 0;
    }

    // Calculate the cosine similarity
    $cosineSimilarity = $dotProduct / ($magnitude1 * $magnitude2);

    return $cosineSimilarity;
  }
@MaximeThoonsen MaximeThoonsen added the enhancement New feature or request label May 19, 2024
@MaximeThoonsen
Copy link
Contributor

Yes I agree 👍️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants