msaidov commited on
Commit
6b2e235
1 Parent(s): bfb6087

Create bary_score.py

Browse files

Future work: improved references and comments here

Files changed (1) hide show
  1. bary_score.py +85 -0
bary_score.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
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
+ """TODO: Add a description here."""
15
+
16
+ import evaluate
17
+ import datasets
18
+
19
+ from score import BaryScoreMetric
20
+
21
+
22
+ # TODO: Add BibTeX citation
23
+ _CITATION = """\
24
+ @InProceedings{huggingface:module,
25
+ title = {A great new module},
26
+ authors={huggingface, Inc.},
27
+ year={2020}
28
+ }
29
+ """
30
+
31
+ # TODO: Add description of the module here
32
+ _DESCRIPTION = """\
33
+ This new module is designed to solve this great ML task and is crafted with a lot of care.
34
+ """
35
+
36
+
37
+ # TODO: Add description of the arguments of the module here
38
+ _KWARGS_DESCRIPTION = """
39
+ Calculates how good are predictions given some references, using certain scores
40
+ Args:
41
+ predictions: list of predictions to score. Each predictions
42
+ should be a string with tokens separated by spaces.
43
+ references: list of reference for each prediction. Each
44
+ reference should be a string with tokens separated by spaces.
45
+ Returns:
46
+ accuracy: description of the first score,
47
+ another_score: description of the second score,
48
+ Examples:
49
+ Examples should be written in doctest format, and should illustrate how
50
+ to use the function.
51
+ >>> my_new_module = evaluate.load("my_new_module")
52
+ >>> results = my_new_module.compute(references=[0, 1], predictions=[0, 1])
53
+ >>> print(results)
54
+ {'accuracy': 1.0}
55
+ """
56
+
57
+ @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
58
+ class BaryScore(evaluate.EvaluationModule):
59
+ """TODO: Short description of my evaluation module."""
60
+
61
+ def _info(self):
62
+ # TODO: Specifies the evaluate.EvaluationModuleInfo object
63
+ return evaluate.EvaluationModuleInfo(
64
+ # This is the description that will appear on the modules page.
65
+ module_type="metric",
66
+ description=_DESCRIPTION,
67
+ citation=_CITATION,
68
+ inputs_description=_KWARGS_DESCRIPTION,
69
+ # This defines the format of each prediction and reference
70
+ features=datasets.Features({
71
+ 'predictions': datasets.Value('string'),
72
+ 'references': datasets.Value('string'),
73
+ }),
74
+ # Homepage of the module for documentation
75
+ homepage="http://module.homepage",
76
+ # Additional links to the codebase or references
77
+ codebase_urls=["http://github.com/path/to/codebase/of/new_module"],
78
+ reference_urls=["http://path.to.reference.url/new_module"]
79
+ )
80
+
81
+ def _compute(self, predictions, references, model_name="bert-base-uncased", last_layers=5, use_idfs=True, sinkhorn_ref=0.01):
82
+ metric_call = BaryScoreMetric(model_name=model_name, last_layers=last_layers, use_idfs=use_idfs, sinkhorn_ref=sinkhorn_ref)
83
+ metric_call.prepare_idfs(references, predictions)
84
+ result = metric_call.evaluate_batch(references, predictions)
85
+ return result