• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2023 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_DATA_RANDOM_APPLY_OP_
18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_DATA_RANDOM_APPLY_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/data/compose_op.h"
28 #include "minddata/dataset/kernels/tensor_op.h"
29 #include "minddata/dataset/util/random.h"
30 
31 namespace mindspore {
32 namespace dataset {
33 class RandomApplyOp : public RandomTensorOp {
34  public:
35   /// constructor
36   /// \param[in] ops the list of TensorOps to apply with prob likelihood
37   /// \param[in] prob probability whether the list of TensorOps will be applied
38   RandomApplyOp(const std::vector<std::shared_ptr<TensorOp>> &ops, double prob);
39 
40   /// default destructor
41   ~RandomApplyOp() override = default;
42 
43   /// return the number of inputs the first tensorOp in compose takes
44   /// \return number of input tensors
NumInput()45   uint32_t NumInput() override { return compose_->NumInput(); }
46 
47   /// return the number of outputs
48   /// \return number of output tensors
49   uint32_t NumOutput() override;
50 
51   /// return output shape if randomApply won't affect the output shape, otherwise return unknown shape
52   /// \param[in] inputs
53   /// \param[out] outputs
54   /// \return  Status code
55   Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override;
56 
57   /// return output type if randomApply won't affect the output type, otherwise return unknown type
58   /// \param[in] inputs
59   /// \param[out] outputs
60   /// \return Status code
61   Status OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) override;
62 
63   /// \param[in] input
64   /// \param[out] output
65   /// \return Status code
66   Status Compute(const TensorRow &input, TensorRow *output) override;
67 
Name()68   std::string Name() const override { return kRandomApplyOp; }
69 
70  private:
71   double prob_;
72   std::shared_ptr<TensorOp> compose_;
73   std::uniform_real_distribution<double> rand_double_;
74 };
75 }  // namespace dataset
76 }  // namespace mindspore
77 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_DATA_RANDOM_APPLY_OP_
78