activation_utils.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #ifndef TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_
  13. #define TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_
  14. #include <algorithm>
  15. #include <cmath>
  16. #include "tensorflow/lite/c/builtin_op_data.h"
  17. #include "tensorflow/lite/kernels/internal/cppmath.h"
  18. #include "tensorflow/lite/kernels/internal/max.h"
  19. #include "tensorflow/lite/kernels/internal/min.h"
  20. namespace tflite {
  21. namespace ops {
  22. namespace micro {
  23. // Returns the floating point value for a fused activation:
  24. inline float ActivationValFloat(TfLiteFusedActivation act, float a) {
  25. switch (act) {
  26. case kTfLiteActNone:
  27. return a;
  28. case kTfLiteActRelu:
  29. return TfLiteMax(0.0f, a);
  30. case kTfLiteActReluN1To1:
  31. return TfLiteMax(-1.0f, TfLiteMin(a, 1.0f));
  32. case kTfLiteActRelu6:
  33. return TfLiteMax(0.0f, TfLiteMin(a, 6.0f));
  34. case kTfLiteActTanh:
  35. return std::tanh(a);
  36. case kTfLiteActSignBit:
  37. return std::signbit(a);
  38. case kTfLiteActSigmoid:
  39. return 1.0f / (1.0f + std::exp(-a));
  40. }
  41. return 0.0f; // To indicate an unsupported activation (i.e. when a new fused
  42. // activation is added to the enum and not handled here).
  43. }
  44. } // namespace micro
  45. } // namespace ops
  46. } // namespace tflite
  47. #endif // TENSORFLOW_LITE_MICRO_KERNELS_ACTIVATION_UTILS_H_