__init__.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Licensed under the Apache License, Version 2.0 (the "License");
  2. # you may not use this file except in compliance with the License.
  3. # You may obtain a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. import warnings
  13. from ...utils import is_sklearn_available, requires_backends
  14. if is_sklearn_available():
  15. from scipy.stats import pearsonr, spearmanr
  16. from sklearn.metrics import f1_score, matthews_corrcoef
  17. DEPRECATION_WARNING = (
  18. "This metric will be removed from the library soon, metrics should be handled with the 🤗 Evaluate "
  19. "library. You can have a look at this example script for pointers: "
  20. "https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py"
  21. )
  22. def simple_accuracy(preds, labels):
  23. warnings.warn(DEPRECATION_WARNING, FutureWarning)
  24. requires_backends(simple_accuracy, "sklearn")
  25. return (preds == labels).mean()
  26. def acc_and_f1(preds, labels):
  27. warnings.warn(DEPRECATION_WARNING, FutureWarning)
  28. requires_backends(acc_and_f1, "sklearn")
  29. acc = simple_accuracy(preds, labels)
  30. f1 = f1_score(y_true=labels, y_pred=preds)
  31. return {
  32. "acc": acc,
  33. "f1": f1,
  34. "acc_and_f1": (acc + f1) / 2,
  35. }
  36. def pearson_and_spearman(preds, labels):
  37. warnings.warn(DEPRECATION_WARNING, FutureWarning)
  38. requires_backends(pearson_and_spearman, "sklearn")
  39. pearson_corr = pearsonr(preds, labels)[0]
  40. spearman_corr = spearmanr(preds, labels)[0]
  41. return {
  42. "pearson": pearson_corr,
  43. "spearmanr": spearman_corr,
  44. "corr": (pearson_corr + spearman_corr) / 2,
  45. }
  46. def glue_compute_metrics(task_name, preds, labels):
  47. warnings.warn(DEPRECATION_WARNING, FutureWarning)
  48. requires_backends(glue_compute_metrics, "sklearn")
  49. assert len(preds) == len(labels), f"Predictions and labels have mismatched lengths {len(preds)} and {len(labels)}"
  50. if task_name == "cola":
  51. return {"mcc": matthews_corrcoef(labels, preds)}
  52. elif task_name == "sst-2":
  53. return {"acc": simple_accuracy(preds, labels)}
  54. elif task_name == "mrpc":
  55. return acc_and_f1(preds, labels)
  56. elif task_name == "sts-b":
  57. return pearson_and_spearman(preds, labels)
  58. elif task_name == "qqp":
  59. return acc_and_f1(preds, labels)
  60. elif task_name == "mnli":
  61. return {"mnli/acc": simple_accuracy(preds, labels)}
  62. elif task_name == "mnli-mm":
  63. return {"mnli-mm/acc": simple_accuracy(preds, labels)}
  64. elif task_name == "qnli":
  65. return {"acc": simple_accuracy(preds, labels)}
  66. elif task_name == "rte":
  67. return {"acc": simple_accuracy(preds, labels)}
  68. elif task_name == "wnli":
  69. return {"acc": simple_accuracy(preds, labels)}
  70. elif task_name == "hans":
  71. return {"acc": simple_accuracy(preds, labels)}
  72. else:
  73. raise KeyError(task_name)
  74. def xnli_compute_metrics(task_name, preds, labels):
  75. warnings.warn(DEPRECATION_WARNING, FutureWarning)
  76. requires_backends(xnli_compute_metrics, "sklearn")
  77. if len(preds) != len(labels):
  78. raise ValueError(f"Predictions and labels have mismatched lengths {len(preds)} and {len(labels)}")
  79. if task_name == "xnli":
  80. return {"acc": simple_accuracy(preds, labels)}
  81. else:
  82. raise KeyError(task_name)