• 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 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_DATASETOPS_SOURCE_SAMPLER_RANDOM_SAMPLER_H_
17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_DATASETOPS_SOURCE_SAMPLER_RANDOM_SAMPLER_H_
18 
19 #include <limits>
20 #include <memory>
21 #include <vector>
22 
23 #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h"
24 
25 namespace mindspore {
26 namespace dataset {
27 class RandomSamplerRT : public SamplerRT {
28  public:
29   // Constructor
30   // @param bool replacement - put he id back / or not after a sample
31   // @param int64_t num_samples - number samples to draw
32   // @param reshuffle_each_epoch - T/F to reshuffle after epoch
33   // @param int64_t samples_per_tensor - Num of Sampler Ids to fetch via 1 GetNextSample call
34   RandomSamplerRT(bool replacement, int64_t num_samples, bool reshuffle_each_epoch,
35                   int64_t samples_per_tensor = std::numeric_limits<int64_t>::max());
36 
37   // Destructor.
38   ~RandomSamplerRT() = default;
39 
40   // Op calls this to get next Sample that contains all the sampleIds
41   // @param TensorRow to be returned to StorageOp
42   // @param int32_t workerId - not meant to be used
43   // @return Status The status code returned
44   Status GetNextSample(TensorRow *out) override;
45 
46   // meant to be called by base class or python
47   Status InitSampler() override;
48 
49   // for next epoch of sampleIds
50   // @return Status The status code returned
51   Status ResetSampler() override;
52 
53   void SamplerPrint(std::ostream &out, bool show_all) const override;
54 
55   /// \brief Get the arguments of node
56   /// \param[out] out_json JSON string of all attributes
57   /// \return Status of the function
58   Status to_json(nlohmann::json *out_json) override;
59 
60  private:
61   uint32_t seed_;
62   bool replacement_;
63   std::vector<int64_t> shuffled_ids_;  // only used for NO REPLACEMENT
64   int64_t next_id_;
65   std::mt19937 rnd_;
66   std::unique_ptr<std::uniform_int_distribution<int64_t>> dist;
67   bool reshuffle_each_epoch_;
68 };
69 }  // namespace dataset
70 }  // namespace mindspore
71 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_DATASETOPS_SOURCE_SAMPLER_RANDOM_SAMPLER_H_
72