You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

transforms.cc 3.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/include/transforms.h"
  17. // Kernel data headers (in alphabetical order)
  18. #include "minddata/dataset/kernels/data/one_hot_op.h"
  19. #include "minddata/dataset/kernels/data/type_cast_op.h"
  20. namespace mindspore {
  21. namespace dataset {
  22. TensorOperation::TensorOperation() {}
  23. // Transform operations for data.
  24. namespace transforms {
  25. // FUNCTIONS TO CREATE DATA TRANSFORM OPERATIONS
  26. // (In alphabetical order)
  27. // Function to create OneHotOperation.
  28. std::shared_ptr<OneHotOperation> OneHot(int32_t num_classes) {
  29. auto op = std::make_shared<OneHotOperation>(num_classes);
  30. // Input validation
  31. if (!op->ValidateParams()) {
  32. return nullptr;
  33. }
  34. return op;
  35. }
  36. // Function to create TypeCastOperation.
  37. std::shared_ptr<TypeCastOperation> TypeCast(std::string data_type) {
  38. auto op = std::make_shared<TypeCastOperation>(data_type);
  39. // Input validation
  40. if (!op->ValidateParams()) {
  41. return nullptr;
  42. }
  43. return op;
  44. }
  45. /* ####################################### Validator Functions ############################################ */
  46. /* ####################################### Derived TensorOperation classes ################################# */
  47. // (In alphabetical order)
  48. // OneHotOperation
  49. OneHotOperation::OneHotOperation(int32_t num_classes) : num_classes_(num_classes) {}
  50. Status OneHotOperation::ValidateParams() {
  51. if (num_classes_ <= 0) {
  52. std::string err_msg =
  53. "OneHot: Number of classes must be greater than 0. num_classes: " + std::to_string(num_classes_);
  54. MS_LOG(ERROR) << err_msg;
  55. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  56. }
  57. return Status::OK();
  58. }
  59. std::shared_ptr<TensorOp> OneHotOperation::Build() { return std::make_shared<OneHotOp>(num_classes_); }
  60. // TypeCastOperation
  61. TypeCastOperation::TypeCastOperation(std::string data_type) : data_type_(data_type) {}
  62. Status TypeCastOperation::ValidateParams() {
  63. std::vector<std::string> predefine_type = {"bool", "int8", "uint8", "int16", "uint16", "int32", "uint32",
  64. "int64", "uint64", "float16", "float32", "float64", "string"};
  65. auto itr = std::find(predefine_type.begin(), predefine_type.end(), data_type_);
  66. if (itr == predefine_type.end()) {
  67. std::string err_msg = "TypeCast: Invalid data type: " + data_type_;
  68. MS_LOG(ERROR) << "TypeCast: Only supports data type bool, int8, uint8, int16, uint16, int32, uint32, "
  69. << "int64, uint64, float16, float32, float64, string, but got " << data_type_;
  70. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  71. }
  72. return Status::OK();
  73. }
  74. std::shared_ptr<TensorOp> TypeCastOperation::Build() { return std::make_shared<TypeCastOp>(data_type_); }
  75. } // namespace transforms
  76. } // namespace dataset
  77. } // namespace mindspore