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 17 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_RANDOM_CHOICE_OP_ 18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_RANDOM_CHOICE_OP_ 19 20 #include <memory> 21 #include <random> 22 #include <string> 23 #include <utility> 24 #include <vector> 25 26 #include "minddata/dataset/core/tensor.h" 27 #include "minddata/dataset/kernels/tensor_op.h" 28 #include "minddata/dataset/kernels/data/compose_op.h" 29 #include "minddata/dataset/util/random.h" 30 31 namespace mindspore { 32 namespace dataset { 33 class RandomChoiceOp : public TensorOp { 34 public: 35 /// constructor 36 /// \param[in] ops list of TensorOps to randomly choose 1 from 37 explicit RandomChoiceOp(const std::vector<std::shared_ptr<TensorOp>> &ops); 38 39 /// default destructor 40 ~RandomChoiceOp() = default; 41 42 /// return the number of inputs. All op in ops_ should have the same number of inputs 43 /// \return number of input tensors 44 uint32_t NumInput() override; 45 46 /// return the number of outputs. All op in ops_ should have the same number of outputs 47 /// \return number of input tensors 48 uint32_t NumOutput() override; 49 50 /// return output shape if all ops in ops_ return the same shape, otherwise return unknown shape 51 /// \param[in] inputs 52 /// \param[out] outputs 53 /// \return Status code 54 Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override; 55 56 /// return output type if all ops in ops_ return the same type, otherwise return unknown type 57 /// \param[in] inputs 58 /// \param[out] outputs 59 /// \return Status code 60 Status OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) override; 61 62 /// \param[in] input 63 /// \param[out] output 64 /// \return Status code 65 Status Compute(const TensorRow &input, TensorRow *output) override; 66 Name()67 std::string Name() const override { return kRandomChoiceOp; } 68 69 private: 70 std::vector<std::shared_ptr<TensorOp>> ops_; 71 std::mt19937 gen_; // mersenne_twister_engine 72 std::uniform_int_distribution<size_t> rand_int_; 73 }; 74 } // namespace dataset 75 } // namespace mindspore 76 77 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_RANDOM_CHOICE_OP_ 78