Daniel Konegen / MNIST_example

Dependencies:   mbed-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers logistic.h Source File

logistic.h

00001 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
00002 
00003 Licensed under the Apache License, Version 2.0 (the "License");
00004 you may not use this file except in compliance with the License.
00005 You may obtain a copy of the License at
00006 
00007     http://www.apache.org/licenses/LICENSE-2.0
00008 
00009 Unless required by applicable law or agreed to in writing, software
00010 distributed under the License is distributed on an "AS IS" BASIS,
00011 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 See the License for the specific language governing permissions and
00013 limitations under the License.
00014 ==============================================================================*/
00015 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LOGISTIC_H_
00016 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LOGISTIC_H_
00017 
00018 #include "fixedpoint/fixedpoint.h"
00019 #include "tensorflow/lite/kernels/internal/common.h"
00020 #include "tensorflow/lite/kernels/internal/quantization_util.h"
00021 #include "tensorflow/lite/kernels/internal/round.h"
00022 #include "tensorflow/lite/kernels/internal/types.h"
00023 #include "tensorflow/lite/kernels/op_macros.h"
00024 
00025 namespace tflite {
00026 namespace reference_ops {
00027 
00028 inline void Logistic(const RuntimeShape& input_shape, const float* input_data,
00029                      const RuntimeShape& output_shape, float* output_data) {
00030   const int flat_size = MatchingFlatSize(input_shape, output_shape);
00031 
00032   for (int i = 0; i < flat_size; i++) {
00033     float val = input_data[i];
00034     float result = 1.f / (1.f + std::exp(-val));
00035     output_data[i] = result;
00036   }
00037 }
00038 
00039 // Convenience version that allows, for example, generated-code calls to be
00040 // uniform between data types.
00041 inline void Logistic(const LogisticParams&, const RuntimeShape& input_shape,
00042                      const float* input_data, const RuntimeShape& output_shape,
00043                      float* output_data) {
00044   // Drop params: not needed.
00045   Logistic(input_shape, input_data, output_shape, output_data);
00046 }
00047 
00048 inline void Logistic(const LogisticParams& params,
00049                      const RuntimeShape& input_shape, const int16* input_data,
00050                      const RuntimeShape& output_shape, int16* output_data) {
00051   const int flat_size = MatchingFlatSize(input_shape, output_shape);
00052 
00053   for (int i = 0; i < flat_size; i++) {
00054     // F0 uses 0 integer bits, range [-1, 1].
00055     // This is the return type of math functions such as tanh, logistic,
00056     // whose range is in [-1, 1].
00057     using F0 = gemmlowp::FixedPoint<std::int16_t, 0>;
00058     // F3 uses 3 integer bits, range [-8, 8], the input range expected here.
00059     using F3 = gemmlowp::FixedPoint<std::int16_t, 3>;
00060 
00061     const F3 input = F3::FromRaw(input_data[i]);
00062     F0 output = gemmlowp::logistic(input);
00063     output_data[i] = output.raw();
00064   }
00065 }
00066 
00067 }  // namespace reference_ops
00068 }  // namespace tflite
00069 
00070 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LOGISTIC_H_