• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "minddata/dataset/engine/datasetops/source/sampler/subset_random_sampler.h"
17 
18 #include <algorithm>
19 #include <random>
20 #include <string>
21 
22 #include "minddata/dataset/core/global_context.h"
23 #include "minddata/dataset/util/random.h"
24 
25 namespace mindspore {
26 namespace dataset {
27 // Constructor.
SubsetRandomSamplerRT(const std::vector<int64_t> & indices,int64_t num_samples,int64_t samples_per_tensor)28 SubsetRandomSamplerRT::SubsetRandomSamplerRT(const std::vector<int64_t> &indices, int64_t num_samples,
29                                              int64_t samples_per_tensor)
30     : SubsetSamplerRT(indices, num_samples, samples_per_tensor) {}
31 
32 // Initialized this Sampler.
InitSampler()33 Status SubsetRandomSamplerRT::InitSampler() {
34   if (is_initialized) {
35     return Status::OK();
36   }
37 
38   // Initialize random generator with seed from config manager
39   rand_gen_.seed(GetSeed());
40 
41   // num_samples_ could be smaller than the total number of input id's.
42   // We will shuffle the full set of id's, but only select the first num_samples_ of them later.
43   std::shuffle(indices_.begin(), indices_.end(), rand_gen_);
44 
45   return SubsetSamplerRT::InitSampler();
46 }
47 
48 // Reset the internal variable to the initial state.
ResetSampler()49 Status SubsetRandomSamplerRT::ResetSampler() {
50   // Randomized the indices again.
51   rand_gen_.seed(GetSeed());
52   std::shuffle(indices_.begin(), indices_.end(), rand_gen_);
53 
54   return SubsetSamplerRT::ResetSampler();
55 }
56 
SamplerPrint(std::ostream & out,bool show_all) const57 void SubsetRandomSamplerRT::SamplerPrint(std::ostream &out, bool show_all) const {
58   out << "\nSampler: SubsetRandomSampler";
59   if (show_all) {
60     // Call the super class for displaying any common detailed info
61     SamplerRT::SamplerPrint(out, show_all);
62     // Then add our own info if any
63   }
64 }
65 
to_json(nlohmann::json * out_json)66 Status SubsetRandomSamplerRT::to_json(nlohmann::json *out_json) {
67   nlohmann::json args;
68   RETURN_IF_NOT_OK(SubsetSamplerRT::to_json(&args));
69   args["sampler_name"] = "SubsetRandomSampler";
70   return Status::OK();
71 }
72 }  // namespace dataset
73 }  // namespace mindspore
74