theAIguy commited on
Commit
c870f2c
1 Parent(s): 94b6cbb

add triplet_margin_loss

Browse files
Files changed (3) hide show
  1. app.py +6 -0
  2. requirements.txt +3 -0
  3. triplet_margin_loss.py +106 -0
app.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ import evaluate
2
+ from evaluate.utils import launch_gradio_widget
3
+
4
+
5
+ module = evaluate.load("triplet_margin_loss")
6
+ launch_gradio_widget(module)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # TODO: fix github to release
2
+ git+https://github.com/huggingface/evaluate.git@505123230059f9605da8951880eddc9d1fbf4278
3
+ datasets~=2.0
triplet_margin_loss.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Triplet Margin Loss metric."""
15
+
16
+ import datasets
17
+ import evaluate
18
+
19
+
20
+ _DESCRIPTION = """
21
+ Triplet margin loss is a loss function that measures a relative similarity between the samples.
22
+ A triplet is comprised of reference input 'anchor(a)', matching input 'positive examples(p)' and non-matching input 'negative examples(n)'.
23
+ The loss function for each triplet is given by:
24
+ L(a, p, n) = max{d(a,p) - d(a,n) + margin, 0}
25
+ where d(x, y) is the 2nd order (Euclidean) pairwise distance between x and y
26
+ """
27
+
28
+
29
+ _KWARGS_DESCRIPTION = """
30
+ Args:
31
+ anchor (`list` of `float`): Reference inputs.
32
+ positive (`list` of `float`): Matching inputs.
33
+ negative (`list` of `float`): Non-matching inputs.
34
+ margin (`float`): Margin, default:`1.0`
35
+
36
+ Returns:
37
+ triplet_margin_loss (`float`): Total loss.
38
+ Examples:
39
+ Example 1-A simple example
40
+ >>> triplet_margin_loss = evaluate.load("triplet_margin_loss")
41
+ >>> results = triplet_margin_loss.compute(
42
+ anchor=[-0.4765, 1.7133, 1.3971, -1.0121, 0.0732],
43
+ positive=[0.9218, 0.6305, 0.3381, 0.1412, 0.2607],
44
+ negative=[0.1971, 0.7246, 0.6729, 0.0941, 0.1011])
45
+ >>> print(results)
46
+ {'triplet_margin_loss': 1.59}
47
+ Example 2-The same as Example 1, except with `margin` set to `2.0`.
48
+ >>> triplet_margin_loss = evaluate.load("triplet_margin_loss")
49
+ >>> results = triplet_margin_loss.compute(
50
+ anchor=[-0.4765, 1.7133, 1.3971, -1.0121, 0.0732],
51
+ positive=[0.9218, 0.6305, 0.3381, 0.1412, 0.2607],
52
+ negative=[0.1971, 0.7246, 0.6729, 0.0941, 0.1011]),
53
+ margin=2.0)
54
+ >>> print(results)
55
+ {'triplet_margin_loss': 2.59}
56
+ """
57
+
58
+
59
+ _CITATION = """
60
+ @article{scikit-learn,
61
+ title={Scikit-learn: Machine Learning in {P}ython},
62
+ author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
63
+ and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
64
+ and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
65
+ Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
66
+ journal={Journal of Machine Learning Research},
67
+ volume={12},
68
+ pages={2825--2830},
69
+ year={2011}
70
+ }
71
+ @article{schultz2003learning,
72
+ title={Learning a distance metric from relative comparisons},
73
+ author={Schultz, Matthew and Joachims, Thorsten},
74
+ journal={Advances in neural information processing systems},
75
+ volume={16},
76
+ year={2003}
77
+ }
78
+ """
79
+
80
+
81
+ @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
82
+ class TripletMarginLoss(evaluate.EvaluationModule):
83
+ def _info(self):
84
+ return evaluate.EvaluationModuleInfo(
85
+ description=_DESCRIPTION,
86
+ citation=_CITATION,
87
+ inputs_description=_KWARGS_DESCRIPTION,
88
+ features=datasets.Features(
89
+ {
90
+ "anchor": datasets.Sequence(datasets.Value("float", id="references")),
91
+ "positive": datasets.Sequence(datasets.Value("float"), id="sequence"),
92
+ "negative": datasets.Sequence(datasets.Value("float"), id="sequence"),
93
+ "margin": datasets.Value("float")
94
+ }
95
+ ),
96
+ reference_urls=["https://proceedings.neurips.cc/paper/2003/hash/d3b1fb02964aa64e257f9f26a31f72cf-Abstract.html"],
97
+ )
98
+
99
+ def _compute(self, anchor, positive, negative, margin=1.0):
100
+ d_a_p = sum((anchor - positive)**2)
101
+ d_a_n = sum((anchor - negative)**2)
102
+ return {
103
+ "accuracy": float(
104
+ max(d_a_p - d_a_n + margin, 0)
105
+ )
106
+ }