Spaces:
Runtime error
Runtime error
File size: 10,350 Bytes
b751253 fa334e3 e25a90f b751253 6144c8c b751253 dbdeb1f b751253 ee7ea35 b751253 0214f0b b751253 94d2567 cb50ae9 b751253 6b7c88e b751253 fa334e3 b751253 1eb630b a27cb6e 609797f df896d1 a27cb6e b751253 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""TODO: Add a description here."""
import evaluate
import datasets
import pandas as pd
import numpy as np
import torch
# TODO: Add BibTeX citation
_CITATION = """\
@InProceedings{huggingface:module,
title = {A great new module},
authors={huggingface, Inc.},
year={2020}
}
"""
# TODO: Add description of the module here
_DESCRIPTION = """\
This new module is designed to solve this great ML task and is crafted with a lot of care.
"""
# TODO: Add description of the arguments of the module here
_KWARGS_DESCRIPTION = """
Calculates how good are predictions given some references, using certain scores
Args:
predictions: list of predictions to score. Each predictions
should be a string with tokens separated by spaces.
references: list of reference for each prediction. Each
reference should be a string with tokens separated by spaces.
Returns:
accuracy: description of the first score,
another_score: description of the second score,
Examples:
Examples should be written in doctest format, and should illustrate how
to use the function.
>>> my_new_module = evaluate.load("my_new_module")
>>> results = my_new_module.compute(references=[0, 1], predictions=[0, 1])
>>> print(results)
{'accuracy': 1.0}
"""
# TODO: Define external resources urls if needed
BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
class metric_tp_fp_Datasets(evaluate.Metric):
"""TODO: Short description of my metric."""
def _info(self):
# TODO: Specifies the evaluate.EvaluationModuleInfo object
return evaluate.MetricInfo(
# This is the description that will appear on the metrics page.
module_type="metric",
description=_DESCRIPTION,
citation=_CITATION,
inputs_description=_KWARGS_DESCRIPTION,
# This defines the format of each prediction and reference
features=datasets.Features({
'predictions': datasets.features.Sequence(datasets.Value('float32')),
'references': datasets.features.Sequence(datasets.Value('int32')),
}),
# Homepage of the metric for documentation
homepage="http://module.homepage",
# Additional links to the codebase or references
codebase_urls=["http://github.com/path/to/codebase/of/new_module"],
reference_urls=["http://path.to.reference.url/new_module"]
)
def _download_and_prepare(self, dl_manager):
"""Optional: download external resources useful to compute the scores"""
# TODO: Download external resources if needed
pass
#Prediction strategy function selector########################################
def predict(self, logits, prediction_strategy):
if prediction_strategy[0] == "argmax_max":
results = self.argmax_max(logits)
elif prediction_strategy[0] == "softmax_threshold":
results = self.softmax_threshold(logits, prediction_strategy[1])
elif prediction_strategy[0] == "softmax_topk":
results = self.softmax_topk(logits, prediction_strategy[1])
elif prediction_strategy[0] == "threshold":
results = self.threshold(logits, prediction_strategy[1])
elif prediction_strategy[0] == "topk":
results = self.topk(logits, prediction_strategy[1])
return results
#Prediction strategy functions______________________________________________
def argmax_max(self, logits):
predictions = []
argmax = torch.argmax(logits, dim=-1)
for prediction in argmax:
predicted_indexes = [prediction.item()]
predictions.append(predicted_indexes)
return predictions
def softmax_threshold(logits, threshold):
predictions = []
softmax = torch.softmax(logits, dim=-1)
for prediction in softmax:
predicted_indexes =[]
for index, value in enumerate(prediction):
if value >= threshold:
predicted_indexes.append(index)
predictions.append(predicted_indexes)
return predictions
def softmax_topk(self, logits, topk):
softmax = torch.softmax(logits, dim=-1)
predictions = softmax.topk(topk).indices.tolist()
return predictions
def threshold(self, logits, threshold):
predictions = []
for prediction in logits:
predicted_indexes =[]
for index, value in enumerate(prediction):
if value >= threshold:
predicted_indexes.append(index)
predictions.append(predicted_indexes)
return predictions
def topk(self, logits, topk):
predictions = logits.topk(topk).indices.tolist()
return predictions
#Builds a report with the metrics####################################################
def metrics_report(self, true_positives = "", false_positives = ""):
classes = true_positives.loc[true_positives["class"] != 'total']["class"].tolist()
samples = [0 for i in range(len(classes))]
results = pd.DataFrame({
"class": classes,
"N# of True samples": samples,
"N# of False samples": samples,
"True Positives": samples,
"False Positives": samples,
"r": samples,
"p": samples,
"f1": samples,
"acc": samples,
})
results.loc[len(results.index)] = ["total", 0, 0, 0, 0, 0, 0, 0, 0]
for label in results["class"].tolist():
if label in true_positives["class"].tolist():
label_true_samples = true_positives.loc[true_positives["class"] == label, "number of samples"].iloc[0]
label_true_positives = true_positives.loc[true_positives["class"] == label, "coincidence count"].iloc[0]
else:
label_true_samples = 0
label_true_positives = 0
if label in false_positives["class"].tolist():
label_false_samples = false_positives.loc[false_positives["class"] == label, "number of samples"].iloc[0]
label_false_positives = false_positives.loc[false_positives["class"] == label, "coincidence count"].iloc[0]
else:
label_false_samples = 0
label_false_positives = 0
r = label_true_positives/label_true_samples
p = label_true_positives/(label_true_positives+label_false_positives)
f1 = 2*r*p/(r+p)
if label_false_samples >> 0:
label_true_negatives = label_false_samples-label_false_positives
else:
label_true_negatives = 0
acc = (label_true_positives+(label_false_samples-label_false_positives))/(label_true_samples+label_false_samples)
results.loc[results["class"] == label, "N# of True samples"] = label_true_samples
results.loc[results["class"] == label, "N# of False samples"] = label_false_samples
results.loc[results["class"] == label, "True Positives"] = label_true_positives
results.loc[results["class"] == label, "False Positives"] = label_false_positives
if label != "total":
results.loc[results["class"] == label, "r"] = r
results.loc[results["class"] == label, "p"] = p
results.loc[results["class"] == label, "f1"] = f1
results.loc[results["class"] == label, "acc"] = acc
else:
results.loc[results["class"] == label, "r"] = ""
results.loc[results["class"] == label, "p"] = ""
results.loc[results["class"] == label, "f1"] = ""
results.loc[results["class"] == label, "acc"] = ""
results.loc[len(results.index)] = ["", "", "", "", "Micro avg.", r , p, f1, acc]
results = results.fillna(0.0)
final_values = results.loc[:len(results.index)-3]
results.loc[len(results.index)] = ["", "", "", "", "Macro avg.", final_values["r"].mean(), final_values["p"].mean(), final_values["f1"].mean(), final_values["acc"].mean()]
return results
#Computes the metric for each prediction strategy##############################################
def _compute(self, predictions, references, prediction_strategies = [["argmax_max"],], FPifWrong = False):
"""Returns the scores"""
# TODO: Compute the different scores of the metric
predictions = torch.from_numpy(np.array(predictions, dtype = 'float32'))
classes = [i for i in range(len(predictions[0]))]
#for value in references:
# if value[0] not in classes:
# classes.append(value[0])
results = {}
for prediction_strategy in prediction_strategies:
prediction_strategy_name = '-'.join(map(str, prediction_strategy))
print(prediction_strategy_name)
results[prediction_strategy_name] = {}
predicted_labels = self.predict(predictions, prediction_strategy)
samples = [0 for i in range(len(classes))]
TP_data = pd.DataFrame({
"class": classes,
"number of samples": samples,
"coincidence count": samples,
})
FP_data = pd.DataFrame({
"class": classes,
"number of samples": samples,
"coincidence count": samples,
})
for i, j in zip(predicted_labels, references):
#print(i)
if j[1] == 0:
TP_data.loc[TP_data["class"] == j[0], "number of samples"] += 1
if len(i) >> 0:
if j[0] in i:
TP_data.loc[TP_data["class"] == j[0], "coincidence count"] += 1
TP_data = TP_data.sort_values(by=["class"], ignore_index = True)
else:
if FPifWrong:
for k in i:
FP_data.loc[FP_data["class"] == k, "coincidence count"] += 1
FP_data = FP_data.sort_values(by=["class"], ignore_index = True)
if j[1] == 2:
FP_data.loc[FP_data["class"] == j[0], "number of samples"] += 1
if len(i) >> 0:
if j[0] in i:
FP_data.loc[FP_data["class"] == j[0], "coincidence count"] += 1
FP_data = FP_data.sort_values(by=["class"], ignore_index = True)
TP_data.loc[len(TP_data.index)] =["total", TP_data["number of samples"].sum(), TP_data["coincidence count"].sum()]
FP_data.loc[len(FP_data.index)] =["total", FP_data["number of samples"].sum(), FP_data["coincidence count"].sum()]
report_table = self.metrics_report(
true_positives = TP_data,
false_positives = FP_data
)
results[prediction_strategy_name] = report_table.rename_axis(prediction_strategy_name, axis='columns')
return results
|