KevinSpaghetti commited on
Commit
4564a8c
·
1 Parent(s): c56e83f

initial commit.

Browse files
Files changed (4) hide show
  1. README.md +68 -25
  2. accuracyk.py +71 -59
  3. app.py +0 -1
  4. tests.py +33 -17
README.md CHANGED
@@ -5,46 +5,89 @@ datasets:
5
  tags:
6
  - evaluate
7
  - metric
8
- description: "TODO: add a description here"
 
 
9
  sdk: gradio
10
  sdk_version: 3.0.2
11
  app_file: app.py
12
  pinned: false
13
  ---
14
 
15
- # Metric Card for accuracyk
16
-
17
- ***Module Card Instructions:*** *Fill out the following subsections. Feel free to take a look at existing metric cards if you'd like examples.*
18
 
19
  ## Metric Description
20
- *Give a brief overview of this metric, including what task(s) it is usually used for, if any.*
 
 
21
 
22
  ## How to Use
23
- *Give general statement of how to use the metric*
24
-
25
- *Provide simplest possible example for using the metric*
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  ### Inputs
28
- *List all input arguments in the format below*
29
- - **input_field** *(type): Definition of input, with explanation if necessary. State any default value(s).*
30
 
31
  ### Output Values
32
-
33
- *Explain what this metric outputs and provide an example of what the metric output looks like. Modules should return a dictionary with one or multiple key-value pairs, e.g. {"bleu" : 6.02}*
34
-
35
- *State the range of possible values that the metric's output can take, as well as what in that range is considered good. For example: "This metric can take on any value between 0 and 100, inclusive. Higher scores are better."*
36
-
37
- #### Values from Popular Papers
38
- *Give examples, preferrably with links to leaderboards or publications, to papers that have reported this metric, along with the values they have reported.*
39
 
40
  ### Examples
41
- *Give code examples of the metric being used. Try to include examples that clear up any potential ambiguity left from the metric description above. If possible, provide a range of examples that show both typical and atypical results, as well as examples where a variety of input parameters are passed.*
42
-
43
- ## Limitations and Bias
44
- *Note any known limitations or biases that the metric has, with links and references if possible.*
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- ## Citation
47
- *Cite the source where this metric was introduced.*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- ## Further References
50
- *Add any useful further references.*
 
 
 
 
5
  tags:
6
  - evaluate
7
  - metric
8
+ - accuracy
9
+
10
+ description: "computes the accuracy at k for a set of predictions as labels"
11
  sdk: gradio
12
  sdk_version: 3.0.2
13
  app_file: app.py
14
  pinned: false
15
  ---
16
 
17
+ # accuracyk
 
 
18
 
19
  ## Metric Description
20
+ Computes the accuracy at k for a set of predictions. The accuracy at k is the number of instances where the real label is in the set of the k most probable
21
+ classes.
22
+ The parameter k is inferred from the shape of the array passed. If you want the accuracy at 5 the shape needs to be (N, 5) where N is the number of examples.
23
 
24
  ## How to Use
25
+ ```
26
+ predictions = np.array([
27
+ [0, 7, 1, 3, 5],
28
+ [0, 2, 9, 8, 4],
29
+ [8, 4, 0, 1, 3],
30
+ ])
31
+ references = np.array([
32
+ 3,
33
+ 5,
34
+ 0
35
+ ])
36
+ results = accuracyk.compute(predictions=predictions, references=references)
37
+ # 2/3 of the labels are in the corresponding rows
38
+ # the shape of the array predictions is (3, 5) so accuracy at 5 has been computed
39
+ # { accuracy: 0.6 }
40
+ ```
41
 
42
  ### Inputs
43
+ - **predictions**: An array of shape (N, K) where N is the number of examples and K is the desired k (5 for accuracy at 5)
44
+ - **references**: An array of the true labels for the examples
45
 
46
  ### Output Values
47
+ The metric returns outputs between 0 and 1. With 0 being that no value is in its corresponding row and 1 being that every value occurs in its row (higher is better).
 
 
 
 
 
 
48
 
49
  ### Examples
50
+ ```python
51
+ >>> accuracyk = evaluate.load("KevinSpaghetti/accuracyk")
52
+ >>> # with numpy arrays
53
+ >>> predictions = np.array([
54
+ >>> [0, 7, 1, 3, 5],
55
+ >>> [0, 2, 9, 8, 4],
56
+ >>> [8, 4, 0, 1, 3],
57
+ >>> ])
58
+ >>> references = np.array([
59
+ >>> 3,
60
+ >>> 4,
61
+ >>> 0
62
+ >>> ])
63
+ >>> results = accuracyk.compute(predictions=predictions, references=references)
64
+ { accuracy: 1 } # every label is in its row
65
 
66
+ >>> # With lists
67
+ >>> predictions = [
68
+ >>> [0, 7, 1, 3, 5],
69
+ >>> [0, 2, 9, 8, 4],
70
+ >>> [8, 4, 0, 1, 3],
71
+ >>> ]
72
+ >>> references = [
73
+ >>> 3,
74
+ >>> 5,
75
+ >>> 0
76
+ >>> ]
77
+ >>> results = accuracyk.compute(predictions=predictions, references=references)
78
+ { accuracy: 0.6 }
79
+ >>> # 3 is in the first row,
80
+ >>> # 5 is not in the second row,
81
+ >>> # 0 is in the third row
82
+
83
+ >>> # with numpy for a batch of examples
84
+ >>> k=5
85
+ >>> # get the 5 highest probabilities
86
+ >>> top5_probs = np.argpartition(logits, -k, axis=-1)[:, -k:]
87
+ >>> results = accuracyk.compute(references=top5_probs, predictions=labels)
88
 
89
+ >>> # computing the accuracy at 1
90
+ >>> predictions = np.array([ 3, 8, 1 ])
91
+ >>> references = np.array([ 3, 4, 0 ])
92
+ >>> results = accuracyk.compute(predictions=np.expand_dims(predictions, axis=1), references=references)
93
+ ```
accuracyk.py CHANGED
@@ -1,68 +1,81 @@
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
 
20
- # TODO: Add BibTeX citation
21
- _CITATION = """\
22
- @InProceedings{huggingface:module,
23
- title = {A great new module},
24
- authors={huggingface, Inc.},
25
- year={2020}
26
- }
27
- """
28
-
29
- # TODO: Add description of the module here
30
  _DESCRIPTION = """\
31
- This new module is designed to solve this great ML task and is crafted with a lot of care.
 
 
 
 
32
  """
33
 
34
-
35
  # TODO: Add description of the arguments of the module here
36
  _KWARGS_DESCRIPTION = """
37
- Calculates how good are predictions given some references, using certain scores
38
  Args:
39
- predictions: list of predictions to score. Each predictions
40
- should be a string with tokens separated by spaces.
41
- references: list of reference for each prediction. Each
42
- reference should be a string with tokens separated by spaces.
43
  Returns:
44
- accuracy: description of the first score,
45
- another_score: description of the second score,
46
  Examples:
47
- Examples should be written in doctest format, and should illustrate how
48
- to use the function.
49
 
50
- >>> my_new_module = evaluate.load("my_new_module")
51
- >>> results = my_new_module.compute(references=[0, 1], predictions=[0, 1])
52
- >>> print(results)
53
- {'accuracy': 1.0}
54
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- # TODO: Define external resources urls if needed
57
- BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
 
 
 
58
 
 
59
 
60
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
61
  class accuracyk(evaluate.Metric):
62
- """TODO: Short description of my evaluation module."""
63
 
64
  def _info(self):
65
- # TODO: Specifies the evaluate.EvaluationModuleInfo object
66
  return evaluate.MetricInfo(
67
  # This is the description that will appear on the modules page.
68
  module_type="metric",
@@ -71,25 +84,24 @@ class accuracyk(evaluate.Metric):
71
  inputs_description=_KWARGS_DESCRIPTION,
72
  # This defines the format of each prediction and reference
73
  features=datasets.Features({
74
- 'predictions': datasets.Value('int64'),
75
  'references': datasets.Value('int64'),
76
  }),
77
- # Homepage of the module for documentation
78
- homepage="http://module.homepage",
79
- # Additional links to the codebase or references
80
- codebase_urls=["http://github.com/path/to/codebase/of/new_module"],
81
- reference_urls=["http://path.to.reference.url/new_module"]
82
  )
83
 
84
  def _download_and_prepare(self, dl_manager):
85
- """Optional: download external resources useful to compute the scores"""
86
- # TODO: Download external resources if needed
87
- pass
88
 
89
  def _compute(self, predictions, references):
90
- """Returns the scores"""
91
- # TODO: Compute the different scores of the module
92
- accuracy = sum(i == j for i, j in zip(predictions, references)) / len(predictions)
93
- return {
94
- "accuracy": accuracy,
95
- }
 
 
 
 
 
1
+ """Computes the accuracy at k for a set of labels"""
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  import evaluate
4
  import datasets
5
+ import typing
6
 
7
+ _CITATION = ""
8
 
 
 
 
 
 
 
 
 
 
 
9
  _DESCRIPTION = """\
10
+ Computes the accuracy at k for a set of predictions. The accuracy at k is the \
11
+ number of instances where the real label is in the set of the k most probable
12
+ classes.
13
+ The parameter k is inferred from the shape of the array passed. If you want the accuracy \
14
+ at 5 the shape needs to be (N, 5) where N is the number of examples.
15
  """
16
 
 
17
  # TODO: Add description of the arguments of the module here
18
  _KWARGS_DESCRIPTION = """
 
19
  Args:
20
+ predictions: An array of shape (N, K) where N is the number of examples
21
+ and K is the desired k (5 for accuracy at 5)
22
+ references: An array of the true labels for the examples
 
23
  Returns:
24
+ accuracy: the accuracy at k for the inputs
 
25
  Examples:
26
+
27
+ >>> accuracyk = evaluate.load("KevinSpaghetti/accuracyk")
28
 
29
+ >>> #with numpy arrays
30
+ >>> predictions = np.array([
31
+ >>> [0, 7, 1, 3, 5],
32
+ >>> [0, 2, 9, 8, 4],
33
+ >>> [8, 4, 0, 1, 3],
34
+ >>> ])
35
+ >>> references = np.array([
36
+ >>> 3,
37
+ >>> 4,
38
+ >>> 0
39
+ >>> ])
40
+ >>> results = accuracyk.compute(predictions=predictions, references=references)
41
+ >>> #\{ accuracy: 1 \} # every label is in its row
42
+
43
+ >>> #With lists
44
+ >>> predictions = [
45
+ >>> [0, 7, 1, 3, 5],
46
+ >>> [0, 2, 9, 8, 4],
47
+ >>> [8, 4, 0, 1, 3],
48
+ >>> ]
49
+ >>> references = [
50
+ >>> 3,
51
+ >>> 5,
52
+ >>> 0
53
+ >>> ]
54
+ >>> results = accuracyk.compute(predictions=predictions, references=references)
55
+ >>> #\{ accuracy: 0.6 \}
56
+ >>> # 3 is in the first row,
57
+ >>> # 5 is not in the second row,
58
+ >>> # 0 is in the third row
59
+
60
+ >>> #with numpy for a batch of examples
61
+ >>> k=5
62
+ >>> # get the 5 highest probabilities
63
+ >>> top5_probs = np.argpartition(logits, -k, axis=-1)[:, -k:]
64
+ >>> results = accuracyk.compute(references=top5_probs, predictions=labels)
65
 
66
+ >>> # computing the accuracy at 1
67
+ >>> predictions = np.array([ 3, 8, 1 ])
68
+ >>> references = np.array([ 3, 4, 0 ])
69
+ >>> results = accuracyk.compute(predictions=np.expand_dims(predictions, axis=1), references=references)
70
+ >>> print(results)
71
 
72
+ """
73
 
74
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
75
  class accuracyk(evaluate.Metric):
76
+ """Computes the accuracy at k for an array of shape (N, k) and correct labels"""
77
 
78
  def _info(self):
 
79
  return evaluate.MetricInfo(
80
  # This is the description that will appear on the modules page.
81
  module_type="metric",
 
84
  inputs_description=_KWARGS_DESCRIPTION,
85
  # This defines the format of each prediction and reference
86
  features=datasets.Features({
87
+ 'predictions': datasets.Sequence(datasets.Value("int64")),
88
  'references': datasets.Value('int64'),
89
  }),
90
+ codebase_urls=[],
91
+ reference_urls=[]
 
 
 
92
  )
93
 
94
  def _download_and_prepare(self, dl_manager):
95
+ ...
 
 
96
 
97
  def _compute(self, predictions, references):
98
+ """Returns the accuracy at k"""
99
+ if isinstance(predictions, list):
100
+ accuracyk = sum(
101
+ [reference in kpredictions for kpredictions, reference in zip(predictions, references)]
102
+ ) / len(references)
103
+ else:
104
+ accuracyk = (
105
+ references[:, None] == predictions[:, :]
106
+ ).any(axis=1).sum() / len(references)
107
+ return dict(accuracy=accuracyk)
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import evaluate
2
  from evaluate.utils import launch_gradio_widget
3
 
4
-
5
  module = evaluate.load("KevinSpaghetti/accuracyk")
6
  launch_gradio_widget(module)
 
1
  import evaluate
2
  from evaluate.utils import launch_gradio_widget
3
 
 
4
  module = evaluate.load("KevinSpaghetti/accuracyk")
5
  launch_gradio_widget(module)
tests.py CHANGED
@@ -1,17 +1,33 @@
1
- test_cases = [
2
- {
3
- "predictions": [0, 0],
4
- "references": [1, 1],
5
- "result": {"metric_score": 0}
6
- },
7
- {
8
- "predictions": [1, 1],
9
- "references": [1, 1],
10
- "result": {"metric_score": 1}
11
- },
12
- {
13
- "predictions": [1, 0],
14
- "references": [1, 1],
15
- "result": {"metric_score": 0.5}
16
- }
17
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import evaluate
2
+ import numpy as np
3
+
4
+ accuracyk = evaluate.load("./accuracyk.py", use_auth_token=True)
5
+
6
+ predictions = np.array([
7
+ [0, 7, 1, 3, 5],
8
+ [0, 2, 9, 8, 4],
9
+ [8, 4, 0, 1, 3],
10
+ ])
11
+
12
+ references = np.array([
13
+ 3,
14
+ 4,
15
+ 0
16
+ ])
17
+ results = accuracyk.compute(predictions=predictions, references=references)
18
+ print(results)
19
+
20
+ predictions = [
21
+ [0, 7, 1, 3, 5],
22
+ [0, 2, 9, 8, 4],
23
+ [8, 4, 0, 1, 3],
24
+ ]
25
+ references = [3, 5, 0]
26
+ results = accuracyk.compute(predictions=predictions, references=references)
27
+
28
+ predictions = np.array([ 3, 8, 1 ])
29
+ references = np.array([ 3, 4, 0 ])
30
+
31
+ results = accuracyk.compute(predictions=np.expand_dims(predictions, axis=1), references=references)
32
+ print(results)
33
+