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

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