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.h 9.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * Copyright 2020 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. #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_SAMPLERS_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_SAMPLERS_H_
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #ifndef ENABLE_ANDROID
  22. #include "minddata/dataset/engine/datasetops/source/mindrecord_op.h"
  23. #endif
  24. namespace mindspore {
  25. namespace dataset {
  26. // Internal Sampler class forward declaration
  27. class Sampler;
  28. namespace api {
  29. class SamplerObj : public std::enable_shared_from_this<SamplerObj> {
  30. public:
  31. /// \brief Constructor
  32. SamplerObj();
  33. /// \brief Destructor
  34. ~SamplerObj() = default;
  35. /// \brief Pure virtual function for derived class to implement parameters validation
  36. /// \return bool true if all the parameters are valid
  37. virtual bool ValidateParams() = 0;
  38. /// \brief Pure virtual function to convert a SamplerObj class into a runtime sampler object
  39. /// \return Shared pointers to the newly created Sampler
  40. virtual std::shared_ptr<Sampler> Build() = 0;
  41. /// \brief Function for derived class to get the shard id of sampler
  42. /// \return The shard id of the derived sampler
  43. virtual int64_t ShardId() { return 0; }
  44. #ifndef ENABLE_ANDROID
  45. /// \brief Virtual function to convert a SamplerObj class into a runtime mindrecord sampler object,
  46. /// only override by SubsetRandomSampler, PkSampler, RandomSampler, SequentialSampler, DistributedSampler
  47. /// \return Shared pointers to the newly created Sampler
  48. virtual std::shared_ptr<mindrecord::ShardOperator> BuildForMindDataset() { return nullptr; }
  49. #endif
  50. };
  51. class DistributedSamplerObj;
  52. class PKSamplerObj;
  53. class RandomSamplerObj;
  54. class SequentialSamplerObj;
  55. class SubsetRandomSamplerObj;
  56. class WeightedRandomSamplerObj;
  57. /// Function to create a Distributed Sampler.
  58. /// \notes A Sampler that access a shard of the dataset.
  59. /// \param[in] num_shards - Number of shards to divide the dataset into.
  60. /// \param[in] shard_id - Shard ID of the current shard within num_shards.
  61. /// \param[in] shuffle - If true, the indices are shuffled.
  62. /// \param[in] num_samples - The number of samples to draw (default to all elements).
  63. /// \param[in] seed - The seed in use when shuffle is true.
  64. /// \param[in] offset - The starting position where access to elements in the dataset begins.
  65. /// \param[in] even_dist - If true, each shard would return the same number of rows (default to true).
  66. /// If false the total rows returned by all the shards would not have overlap.
  67. /// \return Shared pointer to the current Sampler.
  68. std::shared_ptr<DistributedSamplerObj> DistributedSampler(int64_t num_shards, int64_t shard_id, bool shuffle = true,
  69. int64_t num_samples = 0, uint32_t seed = 1,
  70. int64_t offset = -1, bool even_dist = true);
  71. /// Function to create a PK Sampler.
  72. /// \notes Samples K elements for each P class in the dataset.
  73. /// This will sample all classes.
  74. /// \param[in] num_val - Number of elements to sample for each class.
  75. /// \param[in] shuffle - If true, the class IDs are shuffled.
  76. /// \param[in] num_samples - The number of samples to draw (default to all elements).
  77. /// \return Shared pointer to the current Sampler.
  78. std::shared_ptr<PKSamplerObj> PKSampler(int64_t num_val, bool shuffle = false, int64_t num_samples = 0);
  79. /// Function to create a Random Sampler.
  80. /// \notes Samples the elements randomly.
  81. /// \param[in] replacement - If true, put the sample ID back for the next draw.
  82. /// \param[in] num_samples - The number of samples to draw (default to all elements).
  83. /// \return Shared pointer to the current Sampler.
  84. std::shared_ptr<RandomSamplerObj> RandomSampler(bool replacement = false, int64_t num_samples = 0);
  85. /// Function to create a Sequential Sampler.
  86. /// \notes Samples the dataset elements sequentially, same as not having a sampler.
  87. /// \param[in] start_index - Index to start sampling at (dafault to start at first id).
  88. /// \param[in] num_samples - The number of samples to draw (default to all elements).
  89. /// \return Shared pointer to the current Sampler.
  90. std::shared_ptr<SequentialSamplerObj> SequentialSampler(int64_t start_index = 0, int64_t num_samples = 0);
  91. /// Function to create a Subset Random Sampler.
  92. /// \notes Samples the elements randomly from a sequence of indices.
  93. /// \param[in] indices - A vector sequence of indices.
  94. /// \param[in] num_samples - The number of samples to draw (default to all elements).
  95. /// \return Shared pointer to the current Sampler.
  96. std::shared_ptr<SubsetRandomSamplerObj> SubsetRandomSampler(std::vector<int64_t> indices, int64_t num_samples = 0);
  97. /// Function to create a Weighted Random Sampler.
  98. /// \notes Samples the elements from [0, len(weights) - 1] randomly with the given
  99. /// weights (probabilities).
  100. /// \param[in] weights - A vector sequence of weights, not necessarily summing up to 1.
  101. /// \param[in] num_samples - The number of samples to draw (default to all elements).
  102. /// \param[in] replacement - If true, put the sample ID back for the next draw.
  103. /// \return Shared pointer to the current Sampler.
  104. std::shared_ptr<WeightedRandomSamplerObj> WeightedRandomSampler(std::vector<double> weights, int64_t num_samples = 0,
  105. bool replacement = true);
  106. /* ####################################### Derived Sampler classes ################################# */
  107. class DistributedSamplerObj : public SamplerObj {
  108. public:
  109. DistributedSamplerObj(int64_t num_shards, int64_t shard_id, bool shuffle, int64_t num_samples, uint32_t seed,
  110. int64_t offset, bool even_dist);
  111. ~DistributedSamplerObj() = default;
  112. std::shared_ptr<Sampler> Build() override;
  113. #ifndef ENABLE_ANDROID
  114. std::shared_ptr<mindrecord::ShardOperator> BuildForMindDataset() override;
  115. #endif
  116. bool ValidateParams() override;
  117. /// \brief Function to get the shard id of sampler
  118. /// \return The shard id of sampler
  119. int64_t ShardId() override { return shard_id_; }
  120. private:
  121. int64_t num_shards_;
  122. int64_t shard_id_;
  123. bool shuffle_;
  124. int64_t num_samples_;
  125. uint32_t seed_;
  126. int64_t offset_;
  127. bool even_dist_;
  128. };
  129. class PKSamplerObj : public SamplerObj {
  130. public:
  131. PKSamplerObj(int64_t num_val, bool shuffle, int64_t num_samples);
  132. ~PKSamplerObj() = default;
  133. std::shared_ptr<Sampler> Build() override;
  134. #ifndef ENABLE_ANDROID
  135. std::shared_ptr<mindrecord::ShardOperator> BuildForMindDataset() override;
  136. #endif
  137. bool ValidateParams() override;
  138. private:
  139. int64_t num_val_;
  140. bool shuffle_;
  141. int64_t num_samples_;
  142. };
  143. class RandomSamplerObj : public SamplerObj {
  144. public:
  145. RandomSamplerObj(bool replacement, int64_t num_samples);
  146. ~RandomSamplerObj() = default;
  147. std::shared_ptr<Sampler> Build() override;
  148. #ifndef ENABLE_ANDROID
  149. std::shared_ptr<mindrecord::ShardOperator> BuildForMindDataset() override;
  150. #endif
  151. bool ValidateParams() override;
  152. private:
  153. bool replacement_;
  154. int64_t num_samples_;
  155. };
  156. class SequentialSamplerObj : public SamplerObj {
  157. public:
  158. SequentialSamplerObj(int64_t start_index, int64_t num_samples);
  159. ~SequentialSamplerObj() = default;
  160. std::shared_ptr<Sampler> Build() override;
  161. #ifndef ENABLE_ANDROID
  162. std::shared_ptr<mindrecord::ShardOperator> BuildForMindDataset() override;
  163. #endif
  164. bool ValidateParams() override;
  165. private:
  166. int64_t start_index_;
  167. int64_t num_samples_;
  168. };
  169. class SubsetRandomSamplerObj : public SamplerObj {
  170. public:
  171. SubsetRandomSamplerObj(std::vector<int64_t> indices, int64_t num_samples);
  172. ~SubsetRandomSamplerObj() = default;
  173. std::shared_ptr<Sampler> Build() override;
  174. #ifndef ENABLE_ANDROID
  175. std::shared_ptr<mindrecord::ShardOperator> BuildForMindDataset() override;
  176. #endif
  177. bool ValidateParams() override;
  178. private:
  179. const std::vector<int64_t> indices_;
  180. int64_t num_samples_;
  181. };
  182. class WeightedRandomSamplerObj : public SamplerObj {
  183. public:
  184. explicit WeightedRandomSamplerObj(std::vector<double> weights, int64_t num_samples = 0, bool replacement = true);
  185. ~WeightedRandomSamplerObj() = default;
  186. std::shared_ptr<Sampler> Build() override;
  187. bool ValidateParams() override;
  188. private:
  189. const std::vector<double> weights_;
  190. int64_t num_samples_;
  191. bool replacement_;
  192. };
  193. } // namespace api
  194. } // namespace dataset
  195. } // namespace mindspore
  196. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_SAMPLERS_H_