xnli.py 3.4 KB

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