xnli.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # coding=utf-8
  2. # Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team.
  3. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """XNLI utils (dataset loading and evaluation)"""
  17. import os
  18. from ...utils import logging
  19. from .utils import DataProcessor, InputExample
  20. logger = logging.get_logger(__name__)
  21. class XnliProcessor(DataProcessor):
  22. """
  23. Processor for the XNLI dataset. Adapted from
  24. https://github.com/google-research/bert/blob/f39e881b169b9d53bea03d2d341b31707a6c052b/run_classifier.py#L207
  25. """
  26. def __init__(self, language, train_language=None):
  27. self.language = language
  28. self.train_language = train_language
  29. def get_train_examples(self, data_dir):
  30. """See base class."""
  31. lg = self.language if self.train_language is None else self.train_language
  32. lines = self._read_tsv(os.path.join(data_dir, f"XNLI-MT-1.0/multinli/multinli.train.{lg}.tsv"))
  33. examples = []
  34. for i, line in enumerate(lines):
  35. if i == 0:
  36. continue
  37. guid = f"train-{i}"
  38. text_a = line[0]
  39. text_b = line[1]
  40. label = "contradiction" if line[2] == "contradictory" else line[2]
  41. if not isinstance(text_a, str):
  42. raise TypeError(f"Training input {text_a} is not a string")
  43. if not isinstance(text_b, str):
  44. raise TypeError(f"Training input {text_b} is not a string")
  45. if not isinstance(label, str):
  46. raise TypeError(f"Training label {label} is not a string")
  47. examples.append(InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
  48. return examples
  49. def get_test_examples(self, data_dir):
  50. """See base class."""
  51. lines = self._read_tsv(os.path.join(data_dir, "XNLI-1.0/xnli.test.tsv"))
  52. examples = []
  53. for i, line in enumerate(lines):
  54. if i == 0:
  55. continue
  56. language = line[0]
  57. if language != self.language:
  58. continue
  59. guid = f"test-{i}"
  60. text_a = line[6]
  61. text_b = line[7]
  62. label = line[1]
  63. if not isinstance(text_a, str):
  64. raise TypeError(f"Training input {text_a} is not a string")
  65. if not isinstance(text_b, str):
  66. raise TypeError(f"Training input {text_b} is not a string")
  67. if not isinstance(label, str):
  68. raise TypeError(f"Training label {label} is not a string")
  69. examples.append(InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
  70. return examples
  71. def get_labels(self):
  72. """See base class."""
  73. return ["contradiction", "entailment", "neutral"]
  74. xnli_processors = {
  75. "xnli": XnliProcessor,
  76. }
  77. xnli_output_modes = {
  78. "xnli": "classification",
  79. }
  80. xnli_tasks_num_labels = {
  81. "xnli": 3,
  82. }