tesseract  4.1.0
functions.h
Go to the documentation of this file.
1 // File: functions.h
3 // Description: Collection of function-objects used by the network layers.
4 // Author: Ray Smith
5 //
6 // (C) Copyright 2014, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 
18 #ifndef TESSERACT_LSTM_FUNCTIONS_H_
19 #define TESSERACT_LSTM_FUNCTIONS_H_
20 
21 #include "helpers.h"
22 
23 // Setting this to 1 or more causes massive dumps of debug data: weights,
24 // updates, internal calculations etc, and reduces the number of test iterations
25 // to a small number, so outputs can be diffed.
26 #define DEBUG_DETAIL 0
27 #if DEBUG_DETAIL > 0
28 #undef _OPENMP // Disable open mp to get the outputs in sync.
29 #endif
30 
31 namespace tesseract {
32 
33 // Size of static tables.
34 constexpr int kTableSize = 4096;
35 // Scale factor for float arg to int index.
36 constexpr double kScaleFactor = 256.0;
37 
38 #if __cplusplus < 201402 || defined(__clang__) // C++11
39 
40 extern double TanhTable[];
41 extern double LogisticTable[];
42 
43 #else // C++14 or newer
44 
45 typedef double (*LUT_FUNCTION)(int i);
46 
47 constexpr double LUTFuncTanh(int i) {
48  return std::tanh(i / kScaleFactor);
49 }
50 
51 constexpr double LUTFuncLog(int i) {
52  return 1 / (1 + std::exp(-i / kScaleFactor));
53 }
54 
55 template<int n, LUT_FUNCTION f>
56 struct LUTTempl {
57  constexpr LUTTempl() : table_() {
58  for (auto i = 0; i < n; ++i) {
59  table_[i] = f(i);
60  }
61  }
62  const double& operator[](size_t i) const {
63  return table_[i];
64  }
65  double table_[n];
66 };
67 
68 extern const LUTTempl<kTableSize, LUTFuncTanh> TanhTable;
69 extern const LUTTempl<kTableSize, LUTFuncLog> LogisticTable;
70 
71 #endif
72 
73 // Non-linearity (sigmoid) functions with cache tables and clipping.
74 inline double Tanh(double x) {
75  if (x < 0.0) return -Tanh(-x);
76  x *= kScaleFactor;
77  int index = static_cast<int>(x);
78  if (index >= (kTableSize - 1)) return 1.0;
79  double tanh_i0 = TanhTable[index];
80  double tanh_i1 = TanhTable[index + 1];
81  // Linear interpolation.
82  return tanh_i0 + (tanh_i1 - tanh_i0) * (x - index);
83 }
84 
85 inline double Logistic(double x) {
86  if (x < 0.0) return 1.0 - Logistic(-x);
87  x *= kScaleFactor;
88  int index = static_cast<int>(x);
89  if (index >= (kTableSize - 1)) return 1.0;
90  double l0 = LogisticTable[index];
91  double l1 = LogisticTable[index + 1];
92  // Linear interpolation.
93  return l0 + (l1 - l0) * (x - index);
94 }
95 
96 // Non-linearity (sigmoid) functions and their derivatives.
97 struct FFunc {
98  inline double operator()(double x) const { return Logistic(x); }
99 };
100 struct FPrime {
101  inline double operator()(double y) const { return y * (1.0 - y); }
102 };
103 struct ClipFFunc {
104  inline double operator()(double x) const {
105  if (x <= 0.0) return 0.0;
106  if (x >= 1.0) return 1.0;
107  return x;
108  }
109 };
110 struct ClipFPrime {
111  inline double operator()(double y) const {
112  return 0.0 < y && y < 1.0 ? 1.0 : 0.0;
113  }
114 };
115 struct Relu {
116  inline double operator()(double x) const {
117  if (x <= 0.0) return 0.0;
118  return x;
119  }
120 };
121 struct ReluPrime {
122  inline double operator()(double y) const { return 0.0 < y ? 1.0 : 0.0; }
123 };
124 struct GFunc {
125  inline double operator()(double x) const { return Tanh(x); }
126 };
127 struct GPrime {
128  inline double operator()(double y) const { return 1.0 - y * y; }
129 };
130 struct ClipGFunc {
131  inline double operator()(double x) const {
132  if (x <= -1.0) return -1.0;
133  if (x >= 1.0) return 1.0;
134  return x;
135  }
136 };
137 struct ClipGPrime {
138  inline double operator()(double y) const {
139  return -1.0 < y && y < 1.0 ? 1.0 : 0.0;
140  }
141 };
142 struct HFunc {
143  inline double operator()(double x) const { return Tanh(x); }
144 };
145 struct HPrime {
146  inline double operator()(double y) const {
147  double u = Tanh(y);
148  return 1.0 - u * u;
149  }
150 };
151 struct UnityFunc {
152  inline double operator()(double /*x*/) const { return 1.0; }
153 };
154 struct IdentityFunc {
155  inline double operator()(double x) const { return x; }
156 };
157 
158 // Applies Func in-place to inout, of size n.
159 template <class Func>
160 inline void FuncInplace(int n, double* inout) {
161  Func f;
162  for (int i = 0; i < n; ++i) {
163  inout[i] = f(inout[i]);
164  }
165 }
166 // Applies Func to u and multiplies the result by v component-wise,
167 // putting the product in out, all of size n.
168 template <class Func>
169 inline void FuncMultiply(const double* u, const double* v, int n, double* out) {
170  Func f;
171  for (int i = 0; i < n; ++i) {
172  out[i] = f(u[i]) * v[i];
173  }
174 }
175 // Applies the Softmax function in-place to inout, of size n.
176 template <typename T>
177 inline void SoftmaxInPlace(int n, T* inout) {
178  if (n <= 0) return;
179  // A limit on the negative range input to exp to guarantee non-zero output.
180  const T kMaxSoftmaxActivation = 86.0f;
181 
182  T max_output = inout[0];
183  for (int i = 1; i < n; i++) {
184  T output = inout[i];
185  if (output > max_output) max_output = output;
186  }
187  T prob_total = 0.0;
188  for (int i = 0; i < n; i++) {
189  T prob = inout[i] - max_output;
190  prob = exp(ClipToRange(prob, -kMaxSoftmaxActivation, static_cast<T>(0)));
191  prob_total += prob;
192  inout[i] = prob;
193  }
194  if (prob_total > 0.0) {
195  for (int i = 0; i < n; i++) inout[i] /= prob_total;
196  }
197 }
198 
199 // Copies n values of the given src vector to dest.
200 inline void CopyVector(int n, const double* src, double* dest) {
201  memcpy(dest, src, n * sizeof(dest[0]));
202 }
203 
204 // Adds n values of the given src vector to dest.
205 inline void AccumulateVector(int n, const double* src, double* dest) {
206  for (int i = 0; i < n; ++i) dest[i] += src[i];
207 }
208 
209 // Multiplies n values of inout in-place element-wise by the given src vector.
210 inline void MultiplyVectorsInPlace(int n, const double* src, double* inout) {
211  for (int i = 0; i < n; ++i) inout[i] *= src[i];
212 }
213 
214 // Multiplies n values of u by v, element-wise, accumulating to out.
215 inline void MultiplyAccumulate(int n, const double* u, const double* v,
216  double* out) {
217  for (int i = 0; i < n; i++) {
218  out[i] += u[i] * v[i];
219  }
220 }
221 
222 // Sums the given 5 n-vectors putting the result into sum.
223 inline void SumVectors(int n, const double* v1, const double* v2,
224  const double* v3, const double* v4, const double* v5,
225  double* sum) {
226  for (int i = 0; i < n; ++i) {
227  sum[i] = v1[i] + v2[i] + v3[i] + v4[i] + v5[i];
228  }
229 }
230 
231 // Sets the given n-vector vec to 0.
232 template <typename T>
233 inline void ZeroVector(int n, T* vec) {
234  memset(vec, 0, n * sizeof(*vec));
235 }
236 
237 // Clips the given vector vec, of size n to [lower, upper].
238 template <typename T>
239 inline void ClipVector(int n, T lower, T upper, T* vec) {
240  for (int i = 0; i < n; ++i) vec[i] = ClipToRange(vec[i], lower, upper);
241 }
242 
243 // Converts the given n-vector to a binary encoding of the maximum value,
244 // encoded as vector of nf binary values.
245 inline void CodeInBinary(int n, int nf, double* vec) {
246  if (nf <= 0 || n < nf) return;
247  int index = 0;
248  double best_score = vec[0];
249  for (int i = 1; i < n; ++i) {
250  if (vec[i] > best_score) {
251  best_score = vec[i];
252  index = i;
253  }
254  }
255  int mask = 1;
256  for (int i = 0; i < nf; ++i, mask *= 2) {
257  vec[i] = (index & mask) ? 1.0 : 0.0;
258  }
259 }
260 
261 } // namespace tesseract.
262 
263 #endif // TESSERACT_LSTM_FUNCTIONS_H_
void FuncInplace(int n, double *inout)
Definition: functions.h:160
double LogisticTable[kTableSize]
Definition: functions.cpp:26
double operator()(double x) const
Definition: functions.h:131
double operator()(double x) const
Definition: functions.h:143
constexpr int kTableSize
Definition: functions.h:34
double operator()(double x) const
Definition: functions.h:98
constexpr double kScaleFactor
Definition: functions.h:36
void CopyVector(int n, const double *src, double *dest)
Definition: functions.h:200
double operator()(double x) const
Definition: functions.h:125
void SoftmaxInPlace(int n, T *inout)
Definition: functions.h:177
double operator()(double) const
Definition: functions.h:152
double operator()(double y) const
Definition: functions.h:101
double operator()(double y) const
Definition: functions.h:111
void MultiplyAccumulate(int n, const double *u, const double *v, double *out)
Definition: functions.h:215
void ZeroVector(int n, T *vec)
Definition: functions.h:233
double operator()(double x) const
Definition: functions.h:155
double operator()(double y) const
Definition: functions.h:138
double Tanh(double x)
Definition: functions.h:74
double TanhTable[kTableSize]
Definition: functions.cpp:25
double operator()(double x) const
Definition: functions.h:104
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:108
void AccumulateVector(int n, const double *src, double *dest)
Definition: functions.h:205
void FuncMultiply(const double *u, const double *v, int n, double *out)
Definition: functions.h:169
double Logistic(double x)
Definition: functions.h:85
double operator()(double y) const
Definition: functions.h:146
double operator()(double y) const
Definition: functions.h:128
void ClipVector(int n, T lower, T upper, T *vec)
Definition: functions.h:239
void CodeInBinary(int n, int nf, double *vec)
Definition: functions.h:245
double operator()(double x) const
Definition: functions.h:116
void SumVectors(int n, const double *v1, const double *v2, const double *v3, const double *v4, const double *v5, double *sum)
Definition: functions.h:223
double operator()(double y) const
Definition: functions.h:122
void MultiplyVectorsInPlace(int n, const double *src, double *inout)
Definition: functions.h:210