theAIguy commited on
Commit
639df4a
1 Parent(s): c3cc6a6

add readme

Browse files
Files changed (2) hide show
  1. README.md +97 -1
  2. triplet_margin_loss.py +4 -4
README.md CHANGED
@@ -9,4 +9,100 @@ app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  pinned: false
10
  ---
11
 
12
+ # Metric Card for Triplet Margin Loss
13
+
14
+
15
+ ## Metric Description
16
+
17
+ Triplet margin loss is a loss function that measures a relative similarity between the samples.
18
+ A triplet is comprised of reference input 'anchor (a)', matching input 'positive examples (p)' and non-matching input 'negative examples (n)'.
19
+ The loss function for each triplet is given by:
20
+ L(a, p, n) = max{d(a,p) - d(a,n) + margin, 0}
21
+ where d(x, y) is the 2nd order (Euclidean) pairwise distance between x and y.
22
+
23
+ ## How to Use
24
+
25
+ At minimum, this metric requires anchor, positive and negative examples.
26
+
27
+ ```python
28
+ >>> triplet_margin_loss = evaluate.load("theAIguy/triplet_margin_loss")
29
+ >>> loss = triplet_margin_loss.compute(
30
+ anchor=[-0.4765, 1.7133, 1.3971, -1.0121, 0.0732],
31
+ positive=[0.9218, 0.6305, 0.3381, 0.1412, 0.2607],
32
+ negative=[0.1971, 0.7246, 0.6729, 0.0941, 0.1011])
33
+ >>> print(loss)
34
+ {'triplet_margin_loss': 1.59}
35
+ ```
36
+
37
+ You may also add a custom margin (default: 1.0).
38
+
39
+ ```python
40
+ >>> triplet_margin_loss = evaluate.load("theAIguy/triplet_margin_loss")
41
+ >>> loss = 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
+ margin=2.0)
46
+ >>> print(loss)
47
+ {'triplet_margin_loss': 2.59}
48
+ ```
49
+
50
+ ### Inputs
51
+ - **anchor** (`list` of `float`): Reference inputs.
52
+ - **positive** (`list` of `float`): Matching inputs.
53
+ - **negative** (`list` of `float`): Non-matching inputs.
54
+ - **margin** (`float`): Margin, default:`1.0`
55
+
56
+
57
+ ### Output Values
58
+ - **triple_margin_loss**(`float`): Total loss.
59
+
60
+ Output Example(s):
61
+ ```python
62
+ {'triplet_margin_loss': 2.59}
63
+ ```
64
+
65
+ This metric outputs a dictionary, containing the triplet margin loss.
66
+
67
+
68
+ ### Examples
69
+
70
+ Example 1-A simple example
71
+ ```python
72
+ >>> accuracy_metric = evaluate.load("triplet_margin_loss")
73
+ >>> loss = triplet_margin_loss.compute(
74
+ anchor=[-0.4765, 1.7133, 1.3971, -1.0121, 0.0732],
75
+ positive=[0.9218, 0.6305, 0.3381, 0.1412, 0.2607],
76
+ negative=[0.1971, 0.7246, 0.6729, 0.0941, 0.1011])
77
+ >>> print(loss)
78
+ {'triplet_margin_loss': 1.59}
79
+ ```
80
+
81
+ Example 2-The same as Example 1, except `margin` set to `2.0`.
82
+ ```python
83
+ >>> triplet_margin_loss = evaluate.load("triplet_margin_loss")
84
+ >>> loss = triplet_margin_loss.compute(
85
+ anchor=[-0.4765, 1.7133, 1.3971, -1.0121, 0.0732],
86
+ positive=[0.9218, 0.6305, 0.3381, 0.1412, 0.2607],
87
+ negative=[0.1971, 0.7246, 0.6729, 0.0941, 0.1011],
88
+ margin=2.0)
89
+ >>> print(loss)
90
+ {'triplet_margin_loss': 2.59}
91
+ ```
92
+
93
+
94
+
95
+ ## Limitations and Bias
96
+ When used to cluster data points, one needs to be careful to include feature rich data points else dissimilar data points might be clustered together. Negative hard sample mining is widely used along with this loss function to penalize such data points.
97
+
98
+
99
+ ## Citation(s)
100
+ ```bibtex
101
+ @article{schultz2003learning,
102
+ title={Learning a distance metric from relative comparisons},
103
+ author={Schultz, Matthew and Joachims, Thorsten},
104
+ journal={Advances in neural information processing systems},
105
+ volume={16},
106
+ year={2003}
107
+ }
108
+ ```
triplet_margin_loss.py CHANGED
@@ -39,16 +39,16 @@ Returns:
39
  Examples:
40
  Example 1-A simple example
41
  >>> triplet_margin_loss = evaluate.load("theAIguy/triplet_margin_loss")
42
- >>> results = triplet_margin_loss.compute(
43
  anchor=[-0.4765, 1.7133, 1.3971, -1.0121, 0.0732],
44
  positive=[0.9218, 0.6305, 0.3381, 0.1412, 0.2607],
45
  negative=[0.1971, 0.7246, 0.6729, 0.0941, 0.1011])
46
- >>> print(results)
47
  {'triplet_margin_loss': 1.59}
48
- Example 2-The same as Example 1, except with `margin` set to `2.0`.
49
  >>> triplet_margin_loss = evaluate.load("theAIguy/triplet_margin_loss")
50
  >>> results = triplet_margin_loss.compute(
51
- anchor=[-0.4765, 1.7133, 1.3971, -1.0121, 0.0732],
52
  positive=[0.9218, 0.6305, 0.3381, 0.1412, 0.2607],
53
  negative=[0.1971, 0.7246, 0.6729, 0.0941, 0.1011]),
54
  margin=2.0)
 
39
  Examples:
40
  Example 1-A simple example
41
  >>> triplet_margin_loss = evaluate.load("theAIguy/triplet_margin_loss")
42
+ >>> loss = triplet_margin_loss.compute(
43
  anchor=[-0.4765, 1.7133, 1.3971, -1.0121, 0.0732],
44
  positive=[0.9218, 0.6305, 0.3381, 0.1412, 0.2607],
45
  negative=[0.1971, 0.7246, 0.6729, 0.0941, 0.1011])
46
+ >>> print(loss)
47
  {'triplet_margin_loss': 1.59}
48
+ Example 2-The same as Example 1, except `margin` set to `2.0`.
49
  >>> triplet_margin_loss = evaluate.load("theAIguy/triplet_margin_loss")
50
  >>> results = triplet_margin_loss.compute(
51
+ anchor=[-0.4765, 1.7133, 1.3971, -1.0121, 0.0732],
52
  positive=[0.9218, 0.6305, 0.3381, 0.1412, 0.2607],
53
  negative=[0.1971, 0.7246, 0.6729, 0.0941, 0.1011]),
54
  margin=2.0)