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.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /// Function to create a Distributed Sampler.
  21. std::shared_ptr<DistributedSamplerObj> DistributedSampler(int64_t num_shards, int64_t shard_id, bool shuffle,
  22. int64_t num_samples, uint32_t seed, int64_t offset,
  23. bool even_dist) {
  24. auto sampler =
  25. std::make_shared<DistributedSamplerObj>(num_shards, shard_id, shuffle, num_samples, seed, offset, even_dist);
  26. // Input validation
  27. if (sampler->ValidateParams().IsError()) {
  28. return nullptr;
  29. }
  30. return sampler;
  31. }
  32. /// Function to create a PK Sampler.
  33. std::shared_ptr<PKSamplerObj> PKSampler(int64_t num_val, bool shuffle, int64_t num_samples) {
  34. auto sampler = std::make_shared<PKSamplerObj>(num_val, shuffle, num_samples);
  35. // Input validation
  36. if (sampler->ValidateParams().IsError()) {
  37. return nullptr;
  38. }
  39. return sampler;
  40. }
  41. /// Function to create a Random Sampler.
  42. std::shared_ptr<RandomSamplerObj> RandomSampler(bool replacement, int64_t num_samples) {
  43. auto sampler = std::make_shared<RandomSamplerObj>(replacement, num_samples);
  44. // Input validation
  45. if (sampler->ValidateParams().IsError()) {
  46. return nullptr;
  47. }
  48. return sampler;
  49. }
  50. /// Function to create a Sequential Sampler.
  51. std::shared_ptr<SequentialSamplerObj> SequentialSampler(int64_t start_index, int64_t num_samples) {
  52. auto sampler = std::make_shared<SequentialSamplerObj>(start_index, num_samples);
  53. // Input validation
  54. if (sampler->ValidateParams().IsError()) {
  55. return nullptr;
  56. }
  57. return sampler;
  58. }
  59. /// Function to create a Subset Random Sampler.
  60. std::shared_ptr<SubsetSamplerObj> SubsetSampler(std::vector<int64_t> indices, int64_t num_samples) {
  61. auto sampler = std::make_shared<SubsetSamplerObj>(std::move(indices), num_samples);
  62. // Input validation
  63. if (sampler->ValidateParams().IsError()) {
  64. return nullptr;
  65. }
  66. return sampler;
  67. }
  68. /// Function to create a Subset Random Sampler.
  69. std::shared_ptr<SubsetRandomSamplerObj> SubsetRandomSampler(std::vector<int64_t> indices, int64_t num_samples) {
  70. auto sampler = std::make_shared<SubsetRandomSamplerObj>(std::move(indices), num_samples);
  71. // Input validation
  72. if (sampler->ValidateParams().IsError()) {
  73. return nullptr;
  74. }
  75. return sampler;
  76. }
  77. /// Function to create a Weighted Random Sampler.
  78. std::shared_ptr<WeightedRandomSamplerObj> WeightedRandomSampler(std::vector<double> weights, int64_t num_samples,
  79. bool replacement) {
  80. auto sampler = std::make_shared<WeightedRandomSamplerObj>(std::move(weights), num_samples, replacement);
  81. // Input validation
  82. if (sampler->ValidateParams().IsError()) {
  83. return nullptr;
  84. }
  85. return sampler;
  86. }
  87. } // namespace dataset
  88. } // namespace mindspore