1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#ifndef TENSORFLOW_CONTRIB_LITE_KERNELS_ACTIVATION_FUNCTOR_H_
16#define TENSORFLOW_CONTRIB_LITE_KERNELS_ACTIVATION_FUNCTOR_H_
17
18#include <algorithm>
19#include <cmath>
20#include <cstdlib>
21
22#include "tensorflow/contrib/lite/builtin_op_data.h"
23
24namespace tflite {
25
26// Dynamic (non-fused) activation functor. perhaps it is worth having
27// template instantiation?
28// TODO(aselle): Make this more efficient by pulling the switch to conv_eval
29// using template inlining.
30class ActivationFunctor {
31 public:
32  explicit ActivationFunctor(TfLiteFusedActivation act) : act_(act) {}
33
34  float operator()(float a) const {
35    switch (act_) {
36      case kTfLiteActNone:
37        return a;
38      case kTfLiteActRelu:
39        return a < 0.f ? 0.f : a;
40      case kTfLiteActRelu6:
41        return std::max(0.f, std::min(a, 6.f));
42      case kTfLiteActTanh:
43        return std::tanh(a);
44      case kTfLiteActSigmoid:
45        return 1.0f / (1.0f + std::exp(-a));
46      default:
47        // TODO(aselle): More informative fatal error!
48        exit(1);
49    }
50  }
51
52 private:
53  TfLiteFusedActivation act_;
54};
55
56}  // namespace tflite
57
58#endif  // TENSORFLOW_CONTRIB_LITE_KERNELS_ACTIVATION_FUNCTOR_H_
59