ArmNN  NotReleased
Activation.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "Activation.hpp"
7 
8 #include <cmath>
9 
10 namespace armnn
11 {
12 float Activation(float in,
13  ActivationFunction function,
14  float a,
15  float b)
16 {
17  float output;
18 
19  // Compute the result of the activation function.
20  switch (function)
21  {
23  {
24  output = a * in + b;
25  break;
26  }
28  {
29  output = 1.f / (1.f + expf(-in));
30  break;
31  }
33  {
34  output = std::max(0.f, in);
35  break;
36  }
38  {
39  output = std::min(a, std::max(b, in));
40  break;
41  }
43  {
44  output = logf(1.0f + expf(in));
45  break;
46  }
48  {
49  output = in > 0.0f ? in : (in * a);
50  break;
51  }
53  {
54  output = in < 0 ? -in : in;
55  break;
56  }
58  {
59  output = sqrtf(in);
60  break;
61  }
63  {
64  output = in * in;
65  break;
66  }
68  {
69  output = a * tanhf(b * in);
70  break;
71  }
72  default:
73  {
74  throw InvalidArgumentException("Unsupported activation function");
75  }
76  }
77 
78  return output;
79 }
80 
81 
83  Encoder<float>& out,
84  const TensorInfo& tensorInfo,
85  ActivationFunction function,
86  float a,
87  float b)
88 {
89  unsigned int numElements = tensorInfo.GetNumElements();
90 
91  for (unsigned int i = 0; i < numElements; i++)
92  {
93  out.Set(Activation(in.Get(), function, a, b));
94  ++in;
95  ++out;
96  }
97  in -= numElements;
98  out -= numElements;
99 }
100 
101 } //namespace armnn
ActivationFunction
Definition: Types.hpp:54
unsigned int GetNumElements() const
Definition: Tensor.hpp:93
virtual IType Get() const =0
float Activation(float in, ActivationFunction function, float a, float b)
Definition: Activation.cpp:12
virtual void Set(IType right)=0