18 #ifndef TESSERACT_LSTM_FUNCTIONS_H_ 19 #define TESSERACT_LSTM_FUNCTIONS_H_ 26 #define DEBUG_DETAIL 0 28 #undef _OPENMP // Disable open mp to get the outputs in sync. 38 #if __cplusplus < 201402 || defined(__clang__) // C++11 43 #else // C++14 or newer 45 typedef double (*LUT_FUNCTION)(
int i);
47 constexpr
double LUTFuncTanh(
int i) {
48 return std::tanh(i / kScaleFactor);
51 constexpr
double LUTFuncLog(
int i) {
52 return 1 / (1 + std::exp(-i / kScaleFactor));
55 template<
int n, LUT_FUNCTION f>
57 constexpr LUTTempl() : table_() {
58 for (
auto i = 0; i < n; ++i) {
62 const double& operator[](
size_t i)
const {
68 extern const LUTTempl<kTableSize, LUTFuncTanh>
TanhTable;
74 inline double Tanh(
double x) {
75 if (x < 0.0)
return -
Tanh(-x);
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];
82 return tanh_i0 + (tanh_i1 - tanh_i0) * (x - index);
86 if (x < 0.0)
return 1.0 -
Logistic(-x);
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];
93 return l0 + (l1 - l0) * (x - index);
101 inline double operator()(
double y)
const {
return y * (1.0 - y); }
105 if (x <= 0.0)
return 0.0;
106 if (x >= 1.0)
return 1.0;
112 return 0.0 < y && y < 1.0 ? 1.0 : 0.0;
117 if (x <= 0.0)
return 0.0;
122 inline double operator()(
double y)
const {
return 0.0 < y ? 1.0 : 0.0; }
128 inline double operator()(
double y)
const {
return 1.0 - y * y; }
132 if (x <= -1.0)
return -1.0;
133 if (x >= 1.0)
return 1.0;
139 return -1.0 < y && y < 1.0 ? 1.0 : 0.0;
159 template <
class Func>
162 for (
int i = 0; i < n; ++i) {
163 inout[i] = f(inout[i]);
168 template <
class Func>
169 inline void FuncMultiply(
const double* u,
const double* v,
int n,
double* out) {
171 for (
int i = 0; i < n; ++i) {
172 out[i] = f(u[i]) * v[i];
176 template <
typename T>
180 const T kMaxSoftmaxActivation = 86.0f;
182 T max_output = inout[0];
183 for (
int i = 1; i < n; i++) {
185 if (output > max_output) max_output = output;
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)));
194 if (prob_total > 0.0) {
195 for (
int i = 0; i < n; i++) inout[i] /= prob_total;
201 memcpy(dest, src, n *
sizeof(dest[0]));
206 for (
int i = 0; i < n; ++i) dest[i] += src[i];
211 for (
int i = 0; i < n; ++i) inout[i] *= src[i];
217 for (
int i = 0; i < n; i++) {
218 out[i] += u[i] * v[i];
223 inline void SumVectors(
int n,
const double* v1,
const double* v2,
224 const double* v3,
const double* v4,
const double* v5,
226 for (
int i = 0; i < n; ++i) {
227 sum[i] = v1[i] + v2[i] + v3[i] + v4[i] + v5[i];
232 template <
typename T>
234 memset(vec, 0, n *
sizeof(*vec));
238 template <
typename T>
240 for (
int i = 0; i < n; ++i) vec[i] =
ClipToRange(vec[i], lower, upper);
246 if (nf <= 0 || n < nf)
return;
248 double best_score = vec[0];
249 for (
int i = 1; i < n; ++i) {
250 if (vec[i] > best_score) {
256 for (
int i = 0; i < nf; ++i, mask *= 2) {
257 vec[i] = (index & mask) ? 1.0 : 0.0;
263 #endif // TESSERACT_LSTM_FUNCTIONS_H_ void FuncInplace(int n, double *inout)
double LogisticTable[kTableSize]
double operator()(double x) const
double operator()(double x) const
double operator()(double x) const
constexpr double kScaleFactor
void CopyVector(int n, const double *src, double *dest)
double operator()(double x) const
void SoftmaxInPlace(int n, T *inout)
double operator()(double) const
double operator()(double y) const
double operator()(double y) const
void MultiplyAccumulate(int n, const double *u, const double *v, double *out)
void ZeroVector(int n, T *vec)
double operator()(double x) const
double operator()(double y) const
double TanhTable[kTableSize]
double operator()(double x) const
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
void AccumulateVector(int n, const double *src, double *dest)
void FuncMultiply(const double *u, const double *v, int n, double *out)
double Logistic(double x)
double operator()(double y) const
double operator()(double y) const
void ClipVector(int n, T lower, T upper, T *vec)
void CodeInBinary(int n, int nf, double *vec)
double operator()(double x) const
void SumVectors(int n, const double *v1, const double *v2, const double *v3, const double *v4, const double *v5, double *sum)
double operator()(double y) const
void MultiplyVectorsInPlace(int n, const double *src, double *inout)