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 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. """Data operations, will be used in train.py."""
  16. import mindspore.common.dtype as mstype
  17. import mindspore.dataset.engine.datasets as de
  18. import mindspore.dataset.transforms.c_transforms as deC
  19. from mindspore import log as logger
  20. from .config import transformer_net_cfg
  21. def create_transformer_dataset(epoch_count=1, rank_size=1, rank_id=0, do_shuffle="true", enable_data_sink="true",
  22. dataset_path=None):
  23. """create dataset"""
  24. repeat_count = epoch_count
  25. ds = de.MindDataset(dataset_path,
  26. columns_list=["source_eos_ids", "source_eos_mask",
  27. "target_sos_ids", "target_sos_mask",
  28. "target_eos_ids", "target_eos_mask"],
  29. shuffle=(do_shuffle == "true"), num_shards=rank_size, shard_id=rank_id)
  30. type_cast_op = deC.TypeCast(mstype.int32)
  31. ds = ds.map(input_columns="source_eos_ids", operations=type_cast_op)
  32. ds = ds.map(input_columns="source_eos_mask", operations=type_cast_op)
  33. ds = ds.map(input_columns="target_sos_ids", operations=type_cast_op)
  34. ds = ds.map(input_columns="target_sos_mask", operations=type_cast_op)
  35. ds = ds.map(input_columns="target_eos_ids", operations=type_cast_op)
  36. ds = ds.map(input_columns="target_eos_mask", operations=type_cast_op)
  37. # apply batch operations
  38. ds = ds.batch(transformer_net_cfg.batch_size, drop_remainder=True)
  39. ds = ds.repeat(repeat_count)
  40. ds.channel_name = 'transformer'
  41. logger.info("data size: {}".format(ds.get_dataset_size()))
  42. logger.info("repeatcount: {}".format(ds.get_repeat_count()))
  43. return ds, repeat_count