Spaces:
Runtime error
Runtime error
Ikala-allen
commited on
Commit
•
cb05f51
1
Parent(s):
370291f
Update relation_extraction.py
Browse files- relation_extraction.py +30 -49
relation_extraction.py
CHANGED
@@ -2,40 +2,32 @@ import evaluate
|
|
2 |
import datasets
|
3 |
import numpy as np
|
4 |
|
5 |
-
# Add BibTeX citation
|
6 |
_CITATION = """\
|
7 |
-
@
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
link = https://arxiv.org/abs/2009.10684
|
12 |
}
|
13 |
"""
|
14 |
|
15 |
-
# Add description of the module here
|
16 |
_DESCRIPTION = """\
|
17 |
-
This
|
18 |
"""
|
19 |
|
20 |
|
21 |
-
# Add description of the arguments of the module here
|
22 |
_KWARGS_DESCRIPTION = """
|
23 |
-
Calculates how good are predictions given some references, using
|
24 |
Args:
|
25 |
-
predictions
|
26 |
-
|
27 |
-
references
|
28 |
-
|
29 |
Returns:
|
30 |
-
|
31 |
-
|
32 |
-
- **tp** : true positive count
|
33 |
-
- **fp** : false positive count
|
34 |
-
- **fn** : false negative count
|
35 |
-
- **p** : precision
|
36 |
-
- **r** : recall
|
37 |
-
- **f1** : micro f1 score
|
38 |
-
- **ALL** (`dictionary`): score of all of the type (sell and belongs to)
|
39 |
- **tp** : true positive count
|
40 |
- **fp** : false positive count
|
41 |
- **fn** : false negative count
|
@@ -46,25 +38,15 @@ Returns:
|
|
46 |
- **Macro_p** : macro precision
|
47 |
- **Macro_r** : macro recall
|
48 |
Examples:
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
... ]
|
56 |
-
... ]
|
57 |
-
>>> predictions = [
|
58 |
-
... [
|
59 |
-
... {"head": "phipigments", "head_type": "product", "type": "sell", "tail": "國際認證之色乳", "tail_type": "product"},
|
60 |
-
... {"head": "tinadaviespigments", "head_type": "brand", "type": "sell", "tail": "國際認證之色乳", "tail_type": "product"},
|
61 |
-
... ]
|
62 |
-
... ]
|
63 |
-
>>> evaluation_scores = module.compute(predictions=predictions, references=references)
|
64 |
-
>>> print(evaluation_scores)
|
65 |
-
{'sell': {'tp': 1, 'fp': 1, 'fn': 1, 'p': 50.0, 'r': 50.0, 'f1': 50.0}, 'ALL': {'tp': 1, 'fp': 1, 'fn': 1, 'p': 50.0, 'r': 50.0, 'f1': 50.0, 'Macro_f1': 50.0, 'Macro_p': 50.0, 'Macro_r': 50.0}}
|
66 |
"""
|
67 |
|
|
|
68 |
def convert_format(data:list):
|
69 |
"""
|
70 |
Args:
|
@@ -75,12 +57,13 @@ def convert_format(data:list):
|
|
75 |
'head_type': ['product', 'brand'...],
|
76 |
'type': ['sell', 'sell'...],
|
77 |
'tail': ['國際認證之色乳', '國際認證之色乳'...],
|
78 |
-
'tail_type': ['product', 'product'...]},
|
|
|
79 |
{'head': ['SABONTAIWAN', 'SNTAIWAN'...],
|
80 |
'head_type': ['brand', 'brand'...],
|
81 |
'type': ['sell', 'sell'...],
|
82 |
'tail': ['大馬士革玫瑰有機光燦系列', '大馬士革玫瑰有機光燦系列'...],
|
83 |
-
'tail_type': ['product', 'product'...]}
|
84 |
...
|
85 |
]
|
86 |
"""
|
@@ -101,12 +84,7 @@ def convert_format(data:list):
|
|
101 |
|
102 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
103 |
class relation_extraction(evaluate.Metric):
|
104 |
-
"""
|
105 |
-
evaluation metric of relation extraction
|
106 |
-
inputs:
|
107 |
-
predictions : (`list` of `list`s of `dictionary`s) about relation and its type of prediction
|
108 |
-
references : (`list` of `list`s of `dictionary`s) about references for each relation and its type.
|
109 |
-
"""
|
110 |
|
111 |
def _info(self):
|
112 |
# TODO: Specifies the evaluate.EvaluationModuleInfo object
|
@@ -145,7 +123,7 @@ class relation_extraction(evaluate.Metric):
|
|
145 |
# TODO: Download external resources if needed
|
146 |
pass
|
147 |
|
148 |
-
def _compute(self, predictions, references, mode=
|
149 |
"""Returns the scores"""
|
150 |
# TODO: Compute the different scores of the module
|
151 |
|
@@ -229,4 +207,7 @@ class relation_extraction(evaluate.Metric):
|
|
229 |
scores["ALL"]["Macro_p"] = np.mean([scores[ent_type]["p"] for ent_type in relation_types])
|
230 |
scores["ALL"]["Macro_r"] = np.mean([scores[ent_type]["r"] for ent_type in relation_types])
|
231 |
|
|
|
|
|
|
|
232 |
return scores
|
|
|
2 |
import datasets
|
3 |
import numpy as np
|
4 |
|
5 |
+
# TODO: Add BibTeX citation
|
6 |
_CITATION = """\
|
7 |
+
@InProceedings{huggingface:module,
|
8 |
+
title = {A great new module},
|
9 |
+
authors={huggingface, Inc.},
|
10 |
+
year={2020}
|
|
|
11 |
}
|
12 |
"""
|
13 |
|
14 |
+
# TODO: Add description of the module here
|
15 |
_DESCRIPTION = """\
|
16 |
+
This new module is designed to solve this great ML task and is crafted with a lot of care.
|
17 |
"""
|
18 |
|
19 |
|
20 |
+
# TODO: Add description of the arguments of the module here
|
21 |
_KWARGS_DESCRIPTION = """
|
22 |
+
Calculates how good are predictions given some references, using certain scores
|
23 |
Args:
|
24 |
+
predictions (list of list of dictionary): relation and its type of prediction
|
25 |
+
|
26 |
+
references (list of list of dictionary): references for each relation and its type
|
27 |
+
|
28 |
Returns:
|
29 |
+
**output** (`dictionary` of `dictionary`s) with multiple key-value pairs
|
30 |
+
- **entity type** (`dictionary`): score of all of the type
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
- **tp** : true positive count
|
32 |
- **fp** : false positive count
|
33 |
- **fn** : false negative count
|
|
|
38 |
- **Macro_p** : macro precision
|
39 |
- **Macro_r** : macro recall
|
40 |
Examples:
|
41 |
+
Examples should be written in doctest format, and should illustrate how
|
42 |
+
to use the function.
|
43 |
+
my_new_module = evaluate.load("my_new_module")
|
44 |
+
results = my_new_module.compute(references=[0, 1], predictions=[0, 1])
|
45 |
+
print(results)
|
46 |
+
{'accuracy': 1.0}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
"""
|
48 |
|
49 |
+
|
50 |
def convert_format(data:list):
|
51 |
"""
|
52 |
Args:
|
|
|
57 |
'head_type': ['product', 'brand'...],
|
58 |
'type': ['sell', 'sell'...],
|
59 |
'tail': ['國際認證之色乳', '國際認證之色乳'...],
|
60 |
+
'tail_type': ['product', 'product'...]},
|
61 |
+
|
62 |
{'head': ['SABONTAIWAN', 'SNTAIWAN'...],
|
63 |
'head_type': ['brand', 'brand'...],
|
64 |
'type': ['sell', 'sell'...],
|
65 |
'tail': ['大馬士革玫瑰有機光燦系列', '大馬士革玫瑰有機光燦系列'...],
|
66 |
+
'tail_type': ['product', 'product'...]}
|
67 |
...
|
68 |
]
|
69 |
"""
|
|
|
84 |
|
85 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
86 |
class relation_extraction(evaluate.Metric):
|
87 |
+
"""TODO: Short description of my evaluation module."""
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
def _info(self):
|
90 |
# TODO: Specifies the evaluate.EvaluationModuleInfo object
|
|
|
123 |
# TODO: Download external resources if needed
|
124 |
pass
|
125 |
|
126 |
+
def _compute(self, predictions, references, mode, only_all=True, relation_types=[]):
|
127 |
"""Returns the scores"""
|
128 |
# TODO: Compute the different scores of the module
|
129 |
|
|
|
207 |
scores["ALL"]["Macro_p"] = np.mean([scores[ent_type]["p"] for ent_type in relation_types])
|
208 |
scores["ALL"]["Macro_r"] = np.mean([scores[ent_type]["r"] for ent_type in relation_types])
|
209 |
|
210 |
+
if only_all:
|
211 |
+
return scores["ALL"]
|
212 |
+
|
213 |
return scores
|