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.

samplers.cc 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Copyright 2020-2021 Huawei Technologies Co., Ltd
  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. */
  16. #include "minddata/dataset/include/dataset/samplers.h"
  17. #include "minddata/dataset/engine/ir/datasetops/source/samplers/distributed_sampler_ir.h"
  18. #include "minddata/dataset/engine/ir/datasetops/source/samplers/pk_sampler_ir.h"
  19. #include "minddata/dataset/engine/ir/datasetops/source/samplers/prebuilt_sampler_ir.h"
  20. #include "minddata/dataset/engine/ir/datasetops/source/samplers/random_sampler_ir.h"
  21. #include "minddata/dataset/engine/ir/datasetops/source/samplers/samplers_ir.h"
  22. #include "minddata/dataset/engine/ir/datasetops/source/samplers/sequential_sampler_ir.h"
  23. #include "minddata/dataset/engine/ir/datasetops/source/samplers/subset_random_sampler_ir.h"
  24. #include "minddata/dataset/engine/ir/datasetops/source/samplers/subset_sampler_ir.h"
  25. #include "minddata/dataset/engine/ir/datasetops/source/samplers/weighted_random_sampler_ir.h"
  26. namespace mindspore {
  27. namespace dataset {
  28. // DistributedSampler
  29. DistributedSampler::DistributedSampler(int64_t num_shards, int64_t shard_id, bool shuffle, int64_t num_samples,
  30. uint32_t seed, int64_t offset, bool even_dist)
  31. : num_shards_(num_shards),
  32. shard_id_(shard_id),
  33. shuffle_(shuffle),
  34. num_samples_(num_samples),
  35. seed_(seed),
  36. offset_(offset),
  37. even_dist_(even_dist) {}
  38. std::shared_ptr<SamplerObj> DistributedSampler::Parse() const {
  39. return std::make_shared<DistributedSamplerObj>(num_shards_, shard_id_, shuffle_, num_samples_, seed_, offset_,
  40. even_dist_);
  41. }
  42. // PKSampler
  43. PKSampler::PKSampler(int64_t num_val, bool shuffle, int64_t num_samples)
  44. : num_val_(num_val), shuffle_(shuffle), num_samples_(num_samples) {}
  45. std::shared_ptr<SamplerObj> PKSampler::Parse() const {
  46. return std::make_shared<PKSamplerObj>(num_val_, shuffle_, num_samples_);
  47. }
  48. // RandomSampler
  49. RandomSampler::RandomSampler(bool replacement, int64_t num_samples)
  50. : replacement_(replacement), num_samples_(num_samples) {}
  51. std::shared_ptr<SamplerObj> RandomSampler::Parse() const {
  52. return std::make_shared<RandomSamplerObj>(replacement_, num_samples_);
  53. }
  54. // SequentialSampler
  55. SequentialSampler::SequentialSampler(int64_t start_index, int64_t num_samples)
  56. : start_index_(start_index), num_samples_(num_samples) {}
  57. std::shared_ptr<SamplerObj> SequentialSampler::Parse() const {
  58. return std::make_shared<SequentialSamplerObj>(start_index_, num_samples_);
  59. }
  60. // SubsetSampler
  61. SubsetSampler::SubsetSampler(std::vector<int64_t> indices, int64_t num_samples)
  62. : indices_(indices), num_samples_(num_samples) {}
  63. std::shared_ptr<SamplerObj> SubsetSampler::Parse() const {
  64. return std::make_shared<SubsetSamplerObj>(indices_, num_samples_);
  65. }
  66. // SubsetRandomSampler
  67. SubsetRandomSampler::SubsetRandomSampler(std::vector<int64_t> indices, int64_t num_samples)
  68. : SubsetSampler(indices, num_samples) {}
  69. std::shared_ptr<SamplerObj> SubsetRandomSampler::Parse() const {
  70. return std::make_shared<SubsetRandomSamplerObj>(indices_, num_samples_);
  71. }
  72. // WeightedRandomSampler
  73. WeightedRandomSampler::WeightedRandomSampler(std::vector<double> weights, int64_t num_samples, bool replacement)
  74. : weights_(weights), num_samples_(num_samples), replacement_(replacement) {}
  75. std::shared_ptr<SamplerObj> WeightedRandomSampler::Parse() const {
  76. return std::make_shared<WeightedRandomSamplerObj>(weights_, num_samples_, replacement_);
  77. }
  78. } // namespace dataset
  79. } // namespace mindspore