Yeonchan Ahn commited on
Commit
d23aa66
β€’
1 Parent(s): 3a9ccca

first test

Browse files
Files changed (3) hide show
  1. app.py +7 -0
  2. cossim.py +73 -0
  3. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import evaluate
2
+ from evaluate.utils import launch_gradio_widget
3
+
4
+
5
+ module = evaluate.load("ahnyeonchan/cosine_sim_btw_embeddings_of_same_semantics")
6
+
7
+ launch_gradio_widget(module)
cossim.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import evaluate
3
+ from typing import List, Union
4
+ import torch
5
+
6
+ import torch.nn.functional as F
7
+ import torch.nn as nn
8
+ _DESCRIPTION = """
9
+ Cosine similarity between two pairs of embeddings where each embedding represents the semantics of object .
10
+
11
+ """
12
+
13
+ _KWARGS_DESCRIPTION = """
14
+ Args:
15
+ predictions (`list` of a list of `int`): a group of embeddings
16
+ references (`list` of `int`): the other group of embeddings paired with the predictions
17
+
18
+ Returns:
19
+ cos_similarity ("float") : average cosine similarity between two pairs of embeddings
20
+
21
+ Examples:
22
+
23
+ Example 1-A simple example
24
+ >>> cos_similarity_metrics = evaluate.load("ahnyeonchan/cosine_sim_btw_embeddings_of_same_semantics")
25
+ >>> results = accuracy_metric.compute(references=[[1.0, 1.0], [0.0, 1.0]], predictions=[[1.0, 1.0], [0.0, 1.0]])
26
+ >>> print(results)
27
+ {'cos_similarity': 1.0}
28
+ """
29
+
30
+ _CITATION = """"""
31
+
32
+ @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
33
+ class CosSim(evaluate.Metric):
34
+ def __init__(self, *args, **kwargs):
35
+ super(CosSim, self).__init__(*args, **kwargs)
36
+ self.cossim = nn.CosineSimilarity()
37
+
38
+ def _info(self):
39
+ return evaluate.MetricInfo(
40
+ description=_DESCRIPTION,
41
+ citation=_CITATION,
42
+ inputs_description=_KWARGS_DESCRIPTION,
43
+ features=datasets.Features(
44
+ {
45
+ "predictions": datasets.Sequence(datasets.Value("float32")),
46
+ "references": datasets.Sequence(datasets.Value("float32")),
47
+ }
48
+ ),
49
+ reference_urls=[],
50
+ )
51
+
52
+ def _compute(self, predictions: List[List], references: List[List]):
53
+
54
+ if isinstance(predictions, torch.Tensor):
55
+ predictions = torch.Tensor(predictions)
56
+ elif isinstance(predictions, list):
57
+ predictions = torch.Tensor(predictions)
58
+ else:
59
+ raise NotImplementedError()
60
+
61
+ if isinstance(references, torch.Tensor):
62
+ references = torch.Tensor(references)
63
+ elif isinstance(references, list):
64
+ references = torch.Tensor(references)
65
+ else:
66
+ raise NotImplementedError()
67
+
68
+ cosims = self.cossim(predictions, references)
69
+ val = torch.mean(cossim).item()
70
+
71
+ return {
72
+ "cos_similarity": float(val)
73
+ }
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ torch