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.

weighted_random_sampler_test.cc 8.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * Copyright 2019 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 "common/common.h"
  17. #include "gtest/gtest.h"
  18. #include "minddata/dataset/include/dataset/constants.h"
  19. #include "minddata/dataset/core/tensor.h"
  20. #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h"
  21. #include "minddata/dataset/engine/datasetops/source/sampler/weighted_random_sampler.h"
  22. #include "utils/log_adapter.h"
  23. #include <vector>
  24. #include <unordered_set>
  25. using namespace mindspore::dataset;
  26. using mindspore::LogStream;
  27. using mindspore::ExceptionType::NoExceptionType;
  28. using mindspore::MsLogLevel::INFO;
  29. class MindDataTestWeightedRandomSampler : public UT::Common {
  30. public:
  31. class DummyRandomAccessOp : public RandomAccessOp {
  32. public:
  33. DummyRandomAccessOp(uint64_t num_rows) {
  34. // row count is in base class as protected member
  35. // GetNumRowsInDataset does not need an override, the default from base class is fine.
  36. num_rows_ = num_rows;
  37. }
  38. };
  39. };
  40. TEST_F(MindDataTestWeightedRandomSampler, TestOneshotReplacement) {
  41. // num samples to draw.
  42. uint64_t num_samples = 100;
  43. uint64_t total_samples = 1000;
  44. std::vector<double> weights(total_samples, std::rand() % 100);
  45. std::vector<uint64_t> freq(total_samples, 0);
  46. // create sampler with replacement = true
  47. WeightedRandomSamplerRT m_sampler(num_samples, weights, true);
  48. DummyRandomAccessOp dummyRandomAccessOp(total_samples);
  49. m_sampler.HandshakeRandomAccessOp(&dummyRandomAccessOp);
  50. TensorRow row;
  51. std::vector<uint64_t> out;
  52. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  53. for (const auto &t : row) {
  54. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  55. out.push_back(*it);
  56. freq[*it]++;
  57. }
  58. }
  59. ASSERT_EQ(num_samples, out.size());
  60. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  61. ASSERT_EQ(row.eoe(), true);
  62. }
  63. TEST_F(MindDataTestWeightedRandomSampler, TestOneshotNoReplacement) {
  64. // num samples to draw.
  65. uint64_t num_samples = 100;
  66. uint64_t total_samples = 1000;
  67. std::vector<double> weights(total_samples, std::rand() % 100);
  68. std::vector<uint64_t> freq(total_samples, 0);
  69. // create sampler with replacement = replacement
  70. WeightedRandomSamplerRT m_sampler(num_samples, weights, false);
  71. DummyRandomAccessOp dummyRandomAccessOp(total_samples);
  72. m_sampler.HandshakeRandomAccessOp(&dummyRandomAccessOp);
  73. TensorRow row;
  74. std::vector<uint64_t> out;
  75. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  76. for (const auto &t : row) {
  77. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  78. out.push_back(*it);
  79. freq[*it]++;
  80. }
  81. }
  82. ASSERT_EQ(num_samples, out.size());
  83. // Without replacement, each sample only drawn once.
  84. for (int i = 0; i < total_samples; i++) {
  85. if (freq[i]) {
  86. ASSERT_EQ(freq[i], 1);
  87. }
  88. }
  89. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  90. ASSERT_EQ(row.eoe(), true);
  91. }
  92. TEST_F(MindDataTestWeightedRandomSampler, TestGetNextSampleReplacement) {
  93. // num samples to draw.
  94. uint64_t num_samples = 100;
  95. uint64_t total_samples = 1000;
  96. uint64_t samples_per_tensor = 10;
  97. std::vector<double> weights(total_samples, std::rand() % 100);
  98. // create sampler with replacement = replacement
  99. WeightedRandomSamplerRT m_sampler(num_samples, weights, true, samples_per_tensor);
  100. DummyRandomAccessOp dummyRandomAccessOp(total_samples);
  101. m_sampler.HandshakeRandomAccessOp(&dummyRandomAccessOp);
  102. TensorRow row;
  103. std::vector<uint64_t> out;
  104. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  105. int epoch = 0;
  106. while (!row.eoe()) {
  107. epoch++;
  108. for (const auto &t : row) {
  109. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  110. out.push_back(*it);
  111. }
  112. }
  113. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  114. }
  115. ASSERT_EQ(epoch, (num_samples + samples_per_tensor - 1) / samples_per_tensor);
  116. ASSERT_EQ(num_samples, out.size());
  117. }
  118. TEST_F(MindDataTestWeightedRandomSampler, TestGetNextSampleNoReplacement) {
  119. // num samples to draw.
  120. uint64_t num_samples = 100;
  121. uint64_t total_samples = 100;
  122. uint64_t samples_per_tensor = 10;
  123. std::vector<double> weights(total_samples, std::rand() % 100);
  124. weights[1] = 0;
  125. weights[2] = 0;
  126. std::vector<uint64_t> freq(total_samples, 0);
  127. // create sampler with replacement = replacement
  128. WeightedRandomSamplerRT m_sampler(num_samples, weights, false, samples_per_tensor);
  129. DummyRandomAccessOp dummyRandomAccessOp(total_samples);
  130. m_sampler.HandshakeRandomAccessOp(&dummyRandomAccessOp);
  131. TensorRow row;
  132. std::vector<uint64_t> out;
  133. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  134. int epoch = 0;
  135. while (!row.eoe()) {
  136. epoch++;
  137. for (const auto &t : row) {
  138. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  139. out.push_back(*it);
  140. freq[*it]++;
  141. }
  142. }
  143. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  144. }
  145. // Without replacement, each sample only drawn once.
  146. for (int i = 0; i < total_samples; i++) {
  147. if (freq[i]) {
  148. ASSERT_EQ(freq[i], 1);
  149. }
  150. }
  151. ASSERT_EQ(epoch, (num_samples + samples_per_tensor - 1) / samples_per_tensor);
  152. ASSERT_EQ(num_samples, out.size());
  153. }
  154. TEST_F(MindDataTestWeightedRandomSampler, TestResetReplacement) {
  155. // num samples to draw.
  156. uint64_t num_samples = 1000000;
  157. uint64_t total_samples = 1000000;
  158. std::vector<double> weights(total_samples, std::rand() % 100);
  159. std::vector<uint64_t> freq(total_samples, 0);
  160. // create sampler with replacement = true
  161. WeightedRandomSamplerRT m_sampler(num_samples, weights, true);
  162. DummyRandomAccessOp dummyRandomAccessOp(total_samples);
  163. m_sampler.HandshakeRandomAccessOp(&dummyRandomAccessOp);
  164. TensorRow row;
  165. std::vector<uint64_t> out;
  166. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  167. for (const auto &t : row) {
  168. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  169. out.push_back(*it);
  170. freq[*it]++;
  171. }
  172. }
  173. ASSERT_EQ(num_samples, out.size());
  174. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  175. ASSERT_EQ(row.eoe(), true);
  176. m_sampler.ResetSampler();
  177. out.clear();
  178. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  179. for (const auto &t : row) {
  180. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  181. out.push_back(*it);
  182. freq[*it]++;
  183. }
  184. }
  185. ASSERT_EQ(num_samples, out.size());
  186. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  187. ASSERT_EQ(row.eoe(), true);
  188. }
  189. TEST_F(MindDataTestWeightedRandomSampler, TestResetNoReplacement) {
  190. // num samples to draw.
  191. uint64_t num_samples = 1000000;
  192. uint64_t total_samples = 1000000;
  193. std::vector<double> weights(total_samples, std::rand() % 100);
  194. std::vector<uint64_t> freq(total_samples, 0);
  195. // create sampler with replacement = true
  196. WeightedRandomSamplerRT m_sampler(num_samples, weights, false);
  197. DummyRandomAccessOp dummyRandomAccessOp(total_samples);
  198. m_sampler.HandshakeRandomAccessOp(&dummyRandomAccessOp);
  199. TensorRow row;
  200. std::vector<uint64_t> out;
  201. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  202. for (const auto &t : row) {
  203. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  204. out.push_back(*it);
  205. freq[*it]++;
  206. }
  207. }
  208. ASSERT_EQ(num_samples, out.size());
  209. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  210. ASSERT_EQ(row.eoe(), true);
  211. m_sampler.ResetSampler();
  212. out.clear();
  213. freq.clear();
  214. freq.resize(total_samples, 0);
  215. MS_LOG(INFO) << "Resetting sampler";
  216. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  217. for (const auto &t : row) {
  218. for (auto it = t->begin<uint64_t>(); it != t->end<uint64_t>(); it++) {
  219. out.push_back(*it);
  220. freq[*it]++;
  221. }
  222. }
  223. ASSERT_EQ(num_samples, out.size());
  224. // Without replacement, each sample only drawn once.
  225. for (int i = 0; i < total_samples; i++) {
  226. if (freq[i]) {
  227. ASSERT_EQ(freq[i], 1);
  228. }
  229. }
  230. ASSERT_EQ(m_sampler.GetNextSample(&row), Status::OK());
  231. ASSERT_EQ(row.eoe(), true);
  232. }