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 Jaccard Distance #196

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ DISTANCES
~dot_product.DotProduct
~euclidean.Euclidean
~manhattan.Manhattan
~jaccard.Jaccard

EVAL
----
Expand Down
3 changes: 3 additions & 0 deletions quaterion/distances/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from quaterion.distances.cosine import Cosine
from quaterion.distances.dot_product import DotProduct
from quaterion.distances.euclidean import Euclidean
from quaterion.distances.jaccard import Jaccard
from quaterion.distances.manhattan import Manhattan


Expand All @@ -14,6 +15,7 @@ class Distance(str, Enum):
COSINE = "cosine"
DOT_PRODUCT = "dot_product"
MANHATTAN = "manhattan"
JACCARD = "jaccard"

@staticmethod
def get_by_name(name: str) -> BaseDistance:
Expand All @@ -26,6 +28,7 @@ def get_by_name(name: str) -> BaseDistance:
"euclidean": Euclidean,
"manhattan": Manhattan,
"dot_product": DotProduct,
"jaccard": Jaccard,
}

try:
Expand Down
37 changes: 37 additions & 0 deletions quaterion/distances/jaccard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import Optional

import torch
import torch.nn.functional as F
from torch import Tensor

from quaterion.distances.base_distance import BaseDistance


class Jaccard(BaseDistance):
"""Compute Weighted Jaccard distances (and its interpretation as similarities).

Note:
The implementation of Weighted Jaccard
(https://en.wikipedia.org/wiki/Jaccard_index#Weighted_Jaccard_similarity_and_distance)
supports Tensors with positive float values.
"""

@staticmethod
def distance(x: Tensor, y: Tensor) -> Tensor:
return 1 - Jaccard.similarity(x, y)

@staticmethod
def similarity(x: Tensor, y: Tensor) -> Tensor:
min_sum = torch.minimum(x, y).sum(dim=-1)
max_sum = torch.maximum(x, y).sum(dim=-1)
return min_sum / max_sum

@staticmethod
def distance_matrix(x: Tensor, y: Optional[Tensor] = None) -> Tensor:
return 1 - Jaccard.similarity_matrix(x.unsqueeze(1), y.unsqueeze(0))

@staticmethod
def similarity_matrix(x: Tensor, y: Optional[Tensor] = None) -> Tensor:
if y is None:
y = x
return Jaccard.similarity(x.unsqueeze(1), y.unsqueeze(0))
11 changes: 11 additions & 0 deletions tests/test_distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class TestDistances:
]
)

x_2 = torch.tensor(
[
[1.0, 1.5, 2.0, 3.0],
[0.5, 2.5, 2.5, 1.0],
]
)

x_dim = x.size()[0]
expected = {
"cosine": {
Expand All @@ -30,6 +37,10 @@ class TestDistances:
"similarity_matrix": torch.tensor([[16.25, -16.25], [-16.25, 16.25]]),
"distance_matrix": torch.tensor([[-16.25, 16.25], [16.25, -16.25]]),
},
"jaccard": {
"similarity_matrix": torch.tensor([[1.0000, 0.5556], [0.5556, 1.0000]]),
"distance_matrix": torch.tensor([[0.0000, 0.4444], [0.4444, 0.0000]]),
},
}

@pytest.mark.parametrize(
Expand Down