• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "minddata/dataset/kernels/data/random_apply_op.h"
17 
18 #include "minddata/dataset/core/tensor.h"
19 #include "minddata/dataset/kernels/tensor_op.h"
20 #include "minddata/dataset/util/status.h"
21 
22 namespace mindspore {
23 namespace dataset {
24 
NumOutput()25 uint32_t RandomApplyOp::NumOutput() {
26   if (compose_->NumOutput() != NumInput()) {
27     MS_LOG(WARNING) << "NumOutput!=NumInput (randomApply would randomly affect number of outputs).";
28     return 0;
29   }
30   return compose_->NumOutput();
31 }
32 
OutputShape(const std::vector<TensorShape> & inputs,std::vector<TensorShape> & outputs)33 Status RandomApplyOp::OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) {
34   RETURN_IF_NOT_OK(compose_->OutputShape(inputs, outputs));
35   // randomApply either runs all ops or do nothing. If the two methods don't give the same result. return unknown shape.
36   if (inputs != outputs) {  // when RandomApply is not applied, input should be the same as output
37     outputs.clear();
38     outputs.resize(NumOutput(), TensorShape::CreateUnknownRankShape());
39   }
40   return Status::OK();
41 }
OutputType(const std::vector<DataType> & inputs,std::vector<DataType> & outputs)42 Status RandomApplyOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) {
43   RETURN_IF_NOT_OK(compose_->OutputType(inputs, outputs));
44   if (inputs != outputs) {  // when RandomApply is not applied, input should be the same as output
45     outputs.clear();
46     outputs.resize(NumOutput(), DataType(DataType::DE_UNKNOWN));
47   }
48   return Status::OK();
49 }
Compute(const TensorRow & input,TensorRow * output)50 Status RandomApplyOp::Compute(const TensorRow &input, TensorRow *output) {
51   if (rand_double_(gen_) <= prob_) {
52     RETURN_IF_NOT_OK(compose_->Compute(input, output));
53   } else {
54     IO_CHECK_VECTOR(input, output);
55     *output = input;  // copy over the tensors
56   }
57   return Status::OK();
58 }
RandomApplyOp(const std::vector<std::shared_ptr<TensorOp>> & ops,double prob)59 RandomApplyOp::RandomApplyOp(const std::vector<std::shared_ptr<TensorOp>> &ops, double prob)
60     : prob_(prob), gen_(GetSeed()), rand_double_(0, 1) {
61   compose_ = std::make_unique<ComposeOp>(ops);
62   is_deterministic_ = false;
63 }
64 
65 }  // namespace dataset
66 }  // namespace mindspore
67