Spaces:
Sleeping
Sleeping
added numpy requirements
Browse files- requirements.txt +2 -1
- triplet_margin_loss.py +8 -7
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
# TODO: fix github to release
|
2 |
git+https://github.com/huggingface/evaluate.git@505123230059f9605da8951880eddc9d1fbf4278
|
3 |
-
datasets~=2.0
|
|
|
|
1 |
# TODO: fix github to release
|
2 |
git+https://github.com/huggingface/evaluate.git@505123230059f9605da8951880eddc9d1fbf4278
|
3 |
+
datasets~=2.0
|
4 |
+
numpy
|
triplet_margin_loss.py
CHANGED
@@ -15,14 +15,15 @@
|
|
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 |
|
@@ -97,8 +98,8 @@ class TripletMarginLoss(evaluate.EvaluationModule):
|
|
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)
|
|
|
15 |
|
16 |
import datasets
|
17 |
import evaluate
|
18 |
+
import numpy as np
|
19 |
|
20 |
|
21 |
_DESCRIPTION = """
|
22 |
+
Triplet margin loss is a loss function that measures a relative similarity between the samples.\n
|
23 |
+
A triplet is comprised of reference input 'anchor (a)', matching input 'positive examples (p)' and non-matching input 'negative examples (n)'.
|
24 |
+
The loss function for each triplet is given by:\n
|
25 |
+
L(a, p, n) = max{d(a,p) - d(a,n) + margin, 0}\n
|
26 |
+
where d(x, y) is the 2nd order (Euclidean) pairwise distance between x and y.
|
27 |
"""
|
28 |
|
29 |
|
|
|
98 |
)
|
99 |
|
100 |
def _compute(self, anchor, positive, negative, margin=1.0):
|
101 |
+
d_a_p = np.sqrt(sum((anchor - positive)**2))
|
102 |
+
d_a_n = np.sqrt(sum((anchor - negative)**2))
|
103 |
return {
|
104 |
"accuracy": float(
|
105 |
max(d_a_p - d_a_n + margin, 0)
|