Drunper commited on
Commit
0628ea2
1 Parent(s): 16a7830
Files changed (1) hide show
  1. metrica_tesi.py +25 -22
metrica_tesi.py CHANGED
@@ -38,14 +38,17 @@ _KWARGS_DESCRIPTION = """
38
  Calculates how good are predictions given some references, using certain scores
39
  Args:
40
  predictions: list of predictions to score. Each prediction
41
- should be a string with tokens separated by spaces.
42
- Special tokens must be included.
43
  references: list of reference for each prediction. Each
44
- reference should be a string with tokens separated by spaces.
45
- Special tokens must be included.
46
- n: number of last tokens to be considered for the calculation.
 
47
  Returns:
48
- score: accuracy score calculated on the last n action tokens of every pair prediction-reference.
 
 
 
49
  Examples:
50
  Examples should be written in doctest format, and should illustrate how
51
  to use the function.
@@ -60,10 +63,6 @@ Examples:
60
  BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
61
 
62
 
63
- def get_last_n_tokens(string_of_tokens, n):
64
- return string_of_tokens.split(" ")[-n:]
65
-
66
-
67
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
68
  class MetricaTesi(evaluate.Metric):
69
  """TODO: Short description of my evaluation module."""
@@ -79,8 +78,9 @@ class MetricaTesi(evaluate.Metric):
79
  # This defines the format of each prediction and reference
80
  features=datasets.Features(
81
  {
82
- "predictions": datasets.Value("string", id="sequence"),
83
- "references": datasets.Value("string", id="sequence"),
 
84
  }
85
  ),
86
  # Homepage of the module for documentation
@@ -95,14 +95,17 @@ class MetricaTesi(evaluate.Metric):
95
  # TODO: Download external resources if needed
96
  pass
97
 
98
- def _compute(self, predictions, references, n=2):
99
  """Returns the scores"""
100
- # TODO: Compute the different scores of the module
101
- score = 0
102
- for pair in zip(map(get_last_n_tokens, predictions, repeat(n)), map(get_last_n_tokens, references, repeat(n))):
103
- if len(pair[0]) == 2:
104
- score += sum(pred_token == ref_token for pred_token, ref_token in zip(*pair)) / 2
105
- score /= len(predictions)
106
- return {
107
- "score": score,
108
- }
 
 
 
 
38
  Calculates how good are predictions given some references, using certain scores
39
  Args:
40
  predictions: list of predictions to score. Each prediction
41
+ should be an input id.
 
42
  references: list of reference for each prediction. Each
43
+ reference should be an input id.
44
+ actions_seen: number of actions token seen before generating the predicted action token.
45
+ max_actions_seen: the number of scores to calculate. For example, with max_actions_seen = 5,
46
+ it will calculate score for prediction with actions_seen = 0, 1, 2, 3, 4, 5.
47
  Returns:
48
+ score_k: accuracy score calculated on predictions with n = k. The number of scores
49
+ calculated in this way depends on the value of max_actions_seen. For example,
50
+ with max_actions_seen = 5, we will have score_0, score_1, ..., score_5.
51
+ support_k: the number of predictions that support the corresponding score_k.
52
  Examples:
53
  Examples should be written in doctest format, and should illustrate how
54
  to use the function.
 
63
  BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
64
 
65
 
 
 
 
 
66
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
67
  class MetricaTesi(evaluate.Metric):
68
  """TODO: Short description of my evaluation module."""
 
78
  # This defines the format of each prediction and reference
79
  features=datasets.Features(
80
  {
81
+ "predictions": datasets.Value("int32"),
82
+ "references": datasets.Value("int32"),
83
+ "n": datasets.Value("int32"),
84
  }
85
  ),
86
  # Homepage of the module for documentation
 
95
  # TODO: Download external resources if needed
96
  pass
97
 
98
+ def _compute(self, predictions, references, actions_seen, max_actions_seen=20):
99
  """Returns the scores"""
100
+ results = dict()
101
+ for i in range(max_actions_seen):
102
+ score = 0.0
103
+ support = sum(n == i for n in actions_seen)
104
+ if support != 0:
105
+ for prediction, reference, n in zip(predictions, references, actions_seen):
106
+ if prediction == reference:
107
+ score += 1
108
+ score /= support
109
+ results[f"support_{i}"] = support
110
+ results[f"score_{i}"] = score
111
+ return results