Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

metrics.py 1.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. # coding=utf-8
  2. # Modified from transformers (Hugging face)
  3. #
  4. # Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team.
  5. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. try:
  19. from scipy.stats import pearsonr, spearmanr
  20. from sklearn.metrics import matthews_corrcoef, f1_score
  21. _has_sklearn = True
  22. except (AttributeError, ImportError):
  23. _has_sklearn = False
  24. def is_sklearn_available():
  25. return _has_sklearn
  26. if _has_sklearn:
  27. """
  28. def simple_accuracy(preds, labels):
  29. return (preds == labels).mean()
  30. """
  31. def acc_and_f1(preds, labels):
  32. acc = simple_accuracy(preds, labels)
  33. f1 = f1_score(y_true=labels, y_pred=preds)
  34. return {
  35. "acc": acc,
  36. "f1": f1,
  37. "acc_and_f1": (acc + f1) / 2,
  38. }
  39. """
  40. def pearson_and_spearman(preds, labels):
  41. pearson_corr = pearsonr(preds, labels)[0]
  42. spearman_corr = spearmanr(preds, labels)[0]
  43. return {
  44. "pearson": pearson_corr,
  45. "spearmanr": spearman_corr,
  46. "corr": (pearson_corr + spearman_corr) / 2,
  47. }
  48. """
  49. def glue_compute_metrics(task_name, preds, labels):
  50. assert len(preds) == len(labels)
  51. if task_name == "sst-2":
  52. return acc_and_f1(preds, labels)
  53. else:
  54. raise KeyError(task_name)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...