OpenMined / TenSEAL

A library for doing homomorphic encryption operations on tensors
Apache License 2.0
830 stars 158 forks source link

Interface for cosine similarity (or sqrt()) #390

Open Klingefjord opened 2 years ago

Klingefjord commented 2 years ago

Feature Description

I would love to have the option to calculate either the cosine similarity between two vectors, or the norm of a vector (so as to be able to manually calculate the cosine similarity).

Is your feature request related to a problem?

I'm working with encrypted word embeddings and would love the ability to perform similarity checks against encrypted word embedding vectors.

What alternatives have you considered?

There are no other libraries or alternatives that offer this as far as I'm aware.

Additional Context

Klingefjord commented 2 years ago

I see that there are interfaces for pow in https://github.com/OpenMined/TenSEAL/blob/main/tenseal/tensors/abstract_tensor.py, which means the only missing component for being able to calculate cosine similarity is an abs function.

Klingefjord commented 2 years ago
def abs(a):
    return a.square().pow(0.5)

def norm(a):
    return abs(a).square().sum().pow(0.5)

def cosine_similarity(a, b):
    return a * b / (norm(a) * norm(b))

Something like this could work, but unfortunately .pow(0.5) is not allowed, as the function takes an integer value.

If there was a .sqrt() interface, that would be solved.

wcappel commented 1 year ago

Hi, were you able to find a way around this?

Klingefjord commented 1 year ago

@wcappel unfortunately not

Klingefjord commented 1 year ago

Although I think this might not be possible with current FHE algorithms