19 #ifndef TESSERACT_LSTM_FUNCTIONS_H_ 20 #define TESSERACT_LSTM_FUNCTIONS_H_ 29 #define DEBUG_DETAIL 0 31 #undef _OPENMP // Disable open mp to get the outputs in sync. 45 inline double Tanh(
double x) {
46 if (x < 0.0)
return -
Tanh(-x);
47 if (x >= (kTableSize - 1) / kScaleFactor)
return 1.0;
49 int index =
static_cast<int>(floor(x));
50 if (TanhTable[index] == 0.0 && index > 0) {
52 TanhTable[index] = tanh(index / kScaleFactor);
54 if (index == kTableSize - 1)
return TanhTable[kTableSize - 1];
55 if (TanhTable[index + 1] == 0.0) {
57 TanhTable[index + 1] = tanh((index + 1) / kScaleFactor);
59 double offset = x - index;
60 return TanhTable[index] * (1.0 - offset) + TanhTable[index + 1] * offset;
64 if (x < 0.0)
return 1.0 -
Logistic(-x);
65 if (x >= (kTableSize - 1) / kScaleFactor)
return 1.0;
67 int index =
static_cast<int>(floor(x));
68 if (LogisticTable[index] == 0.0) {
70 LogisticTable[index] = 1.0 / (1.0 + exp(-index / kScaleFactor));
72 if (index == kTableSize - 1)
return LogisticTable[kTableSize - 1];
73 if (LogisticTable[index + 1] == 0.0) {
75 LogisticTable[index + 1] = 1.0 / (1.0 + exp(-(index + 1) / kScaleFactor));
77 double offset = x - index;
78 return LogisticTable[index] * (1.0 - offset) +
79 LogisticTable[index + 1] * offset;
87 inline double operator()(
double y)
const {
return y * (1.0 - y); }
91 if (x <= 0.0)
return 0.0;
92 if (x >= 1.0)
return 1.0;
98 return 0.0 < y && y < 1.0 ? 1.0 : 0.0;
103 if (x <= 0.0)
return 0.0;
108 inline double operator()(
double y)
const {
return 0.0 < y ? 1.0 : 0.0; }
114 inline double operator()(
double y)
const {
return 1.0 - y * y; }
118 if (x <= -1.0)
return -1.0;
119 if (x >= 1.0)
return 1.0;
125 return -1.0 < y && y < 1.0 ? 1.0 : 0.0;
145 template <
class Func>
148 for (
int i = 0; i < n; ++i) {
149 inout[i] = f(inout[i]);
154 template <
class Func>
155 inline void FuncMultiply(
const double* u,
const double* v,
int n,
double* out) {
157 for (
int i = 0; i < n; ++i) {
158 out[i] = f(u[i]) * v[i];
162 template <
typename T>
166 const T kMaxSoftmaxActivation = 86.0f;
168 T max_output = inout[0];
169 for (
int i = 1; i < n; i++) {
171 if (output > max_output) max_output = output;
174 for (
int i = 0; i < n; i++) {
175 T prob = inout[i] - max_output;
176 prob = exp(ClipToRange(prob, -kMaxSoftmaxActivation, static_cast<T>(0)));
180 if (prob_total > 0.0) {
181 for (
int i = 0; i < n; i++) inout[i] /= prob_total;
186 inline void CopyVector(
int n,
const double* src,
double* dest) {
187 memcpy(dest, src, n *
sizeof(dest[0]));
192 for (
int i = 0; i < n; ++i) dest[i] += src[i];
197 for (
int i = 0; i < n; ++i) inout[i] *= src[i];
203 for (
int i = 0; i < n; i++) {
204 out[i] += u[i] * v[i];
209 inline void SumVectors(
int n,
const double* v1,
const double* v2,
210 const double* v3,
const double* v4,
const double* v5,
212 for (
int i = 0; i < n; ++i) {
213 sum[i] = v1[i] + v2[i] + v3[i] + v4[i] + v5[i];
218 template <
typename T>
220 memset(vec, 0, n *
sizeof(*vec));
224 template <
typename T>
226 for (
int i = 0; i < n; ++i) vec[i] = ClipToRange(vec[i], lower, upper);
232 if (nf <= 0 || n < nf)
return;
234 double best_score = vec[0];
235 for (
int i = 1; i < n; ++i) {
236 if (vec[i] > best_score) {
242 for (
int i = 0; i < nf; ++i, mask *= 2) {
243 vec[i] = (index & mask) ? 1.0 : 0.0;
249 #endif // TESSERACT_LSTM_FUNCTIONS_H_ double operator()(double x) const
Definition: functions.h:84
void MultiplyAccumulate(int n, const double *u, const double *v, double *out)
Definition: functions.h:201
Definition: functions.h:89
double TanhTable[kTableSize]
Definition: functions.cpp:23
void FuncMultiply(const double *u, const double *v, int n, double *out)
Definition: functions.h:155
double Logistic(double x)
Definition: functions.h:63
double operator()(double x) const
Definition: functions.h:102
double operator()(double y) const
Definition: functions.h:114
double operator()(double x) const
Definition: functions.h:90
double operator()(double y) const
Definition: functions.h:87
double operator()(double x) const
Definition: functions.h:141
void ZeroVector(int n, T *vec)
Definition: functions.h:219
double operator()(double x) const
Definition: functions.h:138
void CopyVector(int n, const double *src, double *dest)
Definition: functions.h:186
Definition: functions.h:137
Definition: functions.h:113
const int kTableSize
Definition: functions.h:37
void AccumulateVector(int n, const double *src, double *dest)
Definition: functions.h:191
Definition: baseapi.cpp:94
double operator()(double y) const
Definition: functions.h:108
Definition: functions.h:110
double operator()(double y) const
Definition: functions.h:97
Definition: functions.h:128
Definition: functions.h:107
const double kScaleFactor
Definition: functions.h:39
void FuncInplace(int n, double *inout)
Definition: functions.h:146
double operator()(double y) const
Definition: functions.h:124
Definition: functions.h:140
double operator()(double y) const
Definition: functions.h:132
void CodeInBinary(int n, int nf, double *vec)
Definition: functions.h:231
double operator()(double x) const
Definition: functions.h:117
void SoftmaxInPlace(int n, T *inout)
Definition: functions.h:163
Definition: functions.h:116
Definition: functions.h:123
double operator()(double x) const
Definition: functions.h:111
Definition: functions.h:86
void SumVectors(int n, const double *v1, const double *v2, const double *v3, const double *v4, const double *v5, double *sum)
Definition: functions.h:209
Definition: functions.h:101
Definition: functions.h:131
Definition: functions.h:96
double Tanh(double x)
Definition: functions.h:45
void ClipVector(int n, T lower, T upper, T *vec)
Definition: functions.h:225
double LogisticTable[kTableSize]
Definition: functions.cpp:24
double operator()(double x) const
Definition: functions.h:129
void MultiplyVectorsInPlace(int n, const double *src, double *inout)
Definition: functions.h:196
Definition: functions.h:83