You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dataset.py 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """
  16. Data operations, will be used in run_pretrain.py
  17. """
  18. import os
  19. import mindspore.common.dtype as mstype
  20. import mindspore.dataset.engine.datasets as de
  21. import mindspore.dataset.transforms.c_transforms as C
  22. from mindspore import log as logger
  23. from .config import bert_net_cfg
  24. def create_bert_dataset(device_num=1, rank=0, do_shuffle="true", data_dir=None, schema_dir=None):
  25. """create train dataset"""
  26. # apply repeat operations
  27. files = os.listdir(data_dir)
  28. data_files = []
  29. for file_name in files:
  30. if "tfrecord" in file_name:
  31. data_files.append(os.path.join(data_dir, file_name))
  32. ds = de.TFRecordDataset(data_files, schema_dir if schema_dir != "" else None,
  33. columns_list=["input_ids", "input_mask", "segment_ids", "next_sentence_labels",
  34. "masked_lm_positions", "masked_lm_ids", "masked_lm_weights"],
  35. shuffle=de.Shuffle.FILES if do_shuffle == "true" else False,
  36. num_shards=device_num, shard_id=rank, shard_equal_rows=True)
  37. ori_dataset_size = ds.get_dataset_size()
  38. print('origin dataset size: ', ori_dataset_size)
  39. type_cast_op = C.TypeCast(mstype.int32)
  40. ds = ds.map(operations=type_cast_op, input_columns="masked_lm_ids")
  41. ds = ds.map(operations=type_cast_op, input_columns="masked_lm_positions")
  42. ds = ds.map(operations=type_cast_op, input_columns="next_sentence_labels")
  43. ds = ds.map(operations=type_cast_op, input_columns="segment_ids")
  44. ds = ds.map(operations=type_cast_op, input_columns="input_mask")
  45. ds = ds.map(operations=type_cast_op, input_columns="input_ids")
  46. # apply batch operations
  47. ds = ds.batch(bert_net_cfg.batch_size, drop_remainder=True)
  48. logger.info("data size: {}".format(ds.get_dataset_size()))
  49. logger.info("repeat count: {}".format(ds.get_repeat_count()))
  50. return ds
  51. def create_ner_dataset(batch_size=1, repeat_count=1, assessment_method="accuracy",
  52. data_file_path=None, schema_file_path=None, do_shuffle=True):
  53. """create finetune or evaluation dataset"""
  54. type_cast_op = C.TypeCast(mstype.int32)
  55. ds = de.TFRecordDataset([data_file_path], schema_file_path if schema_file_path != "" else None,
  56. columns_list=["input_ids", "input_mask", "segment_ids", "label_ids"], shuffle=do_shuffle)
  57. if assessment_method == "Spearman_correlation":
  58. type_cast_op_float = C.TypeCast(mstype.float32)
  59. ds = ds.map(operations=type_cast_op_float, input_columns="label_ids")
  60. else:
  61. ds = ds.map(operations=type_cast_op, input_columns="label_ids")
  62. ds = ds.map(operations=type_cast_op, input_columns="segment_ids")
  63. ds = ds.map(operations=type_cast_op, input_columns="input_mask")
  64. ds = ds.map(operations=type_cast_op, input_columns="input_ids")
  65. ds = ds.repeat(repeat_count)
  66. # apply batch operations
  67. ds = ds.batch(batch_size, drop_remainder=True)
  68. return ds
  69. def create_classification_dataset(batch_size=1, repeat_count=1, assessment_method="accuracy",
  70. data_file_path=None, schema_file_path=None, do_shuffle=True):
  71. """create finetune or evaluation dataset"""
  72. type_cast_op = C.TypeCast(mstype.int32)
  73. ds = de.TFRecordDataset([data_file_path], schema_file_path if schema_file_path != "" else None,
  74. columns_list=["input_ids", "input_mask", "segment_ids", "label_ids"], shuffle=do_shuffle)
  75. if assessment_method == "Spearman_correlation":
  76. type_cast_op_float = C.TypeCast(mstype.float32)
  77. ds = ds.map(operations=type_cast_op_float, input_columns="label_ids")
  78. else:
  79. ds = ds.map(operations=type_cast_op, input_columns="label_ids")
  80. ds = ds.map(operations=type_cast_op, input_columns="segment_ids")
  81. ds = ds.map(operations=type_cast_op, input_columns="input_mask")
  82. ds = ds.map(operations=type_cast_op, input_columns="input_ids")
  83. ds = ds.repeat(repeat_count)
  84. # apply batch operations
  85. ds = ds.batch(batch_size, drop_remainder=True)
  86. return ds
  87. def create_squad_dataset(batch_size=1, repeat_count=1, data_file_path=None, schema_file_path=None,
  88. is_training=True, do_shuffle=True):
  89. """create finetune or evaluation dataset"""
  90. type_cast_op = C.TypeCast(mstype.int32)
  91. if is_training:
  92. ds = de.TFRecordDataset([data_file_path], schema_file_path if schema_file_path != "" else None,
  93. columns_list=["input_ids", "input_mask", "segment_ids", "start_positions",
  94. "end_positions", "unique_ids", "is_impossible"],
  95. shuffle=do_shuffle)
  96. ds = ds.map(operations=type_cast_op, input_columns="start_positions")
  97. ds = ds.map(operations=type_cast_op, input_columns="end_positions")
  98. else:
  99. ds = de.TFRecordDataset([data_file_path], schema_file_path if schema_file_path != "" else None,
  100. columns_list=["input_ids", "input_mask", "segment_ids", "unique_ids"])
  101. ds = ds.map(operations=type_cast_op, input_columns="segment_ids")
  102. ds = ds.map(operations=type_cast_op, input_columns="input_mask")
  103. ds = ds.map(operations=type_cast_op, input_columns="input_ids")
  104. ds = ds.repeat(repeat_count)
  105. # apply batch operations
  106. ds = ds.batch(batch_size, drop_remainder=True)
  107. return ds