tesseract
5.0.0-alpha-619-ge9db
|
Go to the documentation of this file.
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.
43 inline double Tanh(
double x) {
44 if (x < 0.0)
return -
Tanh(-x);
46 unsigned index = static_cast<unsigned>(x);
51 return tanh_i0 + (tanh_i1 - tanh_i0) * (x - index);
55 if (x < 0.0)
return 1.0 -
Logistic(-x);
57 unsigned index = static_cast<unsigned>(x);
62 return l0 + (l1 - l0) * (x - index);
70 inline double operator()(
double y)
const {
return y * (1.0 - y); }
74 if (x <= 0.0)
return 0.0;
75 if (x >= 1.0)
return 1.0;
81 return 0.0 < y && y < 1.0 ? 1.0 : 0.0;
86 if (x <= 0.0)
return 0.0;
91 inline double operator()(
double y)
const {
return 0.0 < y ? 1.0 : 0.0; }
97 inline double operator()(
double y)
const {
return 1.0 - y * y; }
101 if (x <= -1.0)
return -1.0;
102 if (x >= 1.0)
return 1.0;
108 return -1.0 < y && y < 1.0 ? 1.0 : 0.0;
128 template <
class Func>
131 for (
int i = 0; i < n; ++i) {
132 inout[i] = f(inout[i]);
137 template <
class Func>
138 inline void FuncMultiply(
const double* u,
const double* v,
int n,
double* out) {
140 for (
int i = 0; i < n; ++i) {
141 out[i] = f(u[i]) * v[i];
145 template <
typename T>
149 const T kMaxSoftmaxActivation = 86.0f;
151 T max_output = inout[0];
152 for (
int i = 1; i < n; i++) {
154 if (output > max_output) max_output = output;
157 for (
int i = 0; i < n; i++) {
158 T prob = inout[i] - max_output;
159 prob = exp(
ClipToRange(prob, -kMaxSoftmaxActivation, static_cast<T>(0)));
163 if (prob_total > 0.0) {
164 for (
int i = 0; i < n; i++) inout[i] /= prob_total;
170 memcpy(
dest, src, n *
sizeof(
dest[0]));
175 for (
int i = 0; i < n; ++i)
dest[i] += src[i];
180 for (
int i = 0; i < n; ++i) inout[i] *= src[i];
186 for (
int i = 0; i < n; i++) {
187 out[i] += u[i] * v[i];
192 inline void SumVectors(
int n,
const double* v1,
const double* v2,
193 const double* v3,
const double* v4,
const double* v5,
195 for (
int i = 0; i < n; ++i) {
196 sum[i] = v1[i] + v2[i] + v3[i] + v4[i] + v5[i];
201 template <
typename T>
203 memset(vec, 0, n *
sizeof(*vec));
207 template <
typename T>
209 for (
int i = 0; i < n; ++i) vec[i] =
ClipToRange(vec[i], lower, upper);
215 if (nf <= 0 || n < nf)
return;
217 double best_score = vec[0];
218 for (
int i = 1; i < n; ++i) {
219 if (vec[i] > best_score) {
225 for (
int i = 0; i < nf; ++i, mask *= 2) {
226 vec[i] = (index & mask) ? 1.0 : 0.0;
232 #endif // TESSERACT_LSTM_FUNCTIONS_H_
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
double operator()(double x) const
double operator()(double y) const
const double LogisticTable[]
void CopyVector(int n, const double *src, double *dest)
double operator()(double x) const
double operator()(double x) const
void MultiplyVectorsInPlace(int n, const double *src, double *inout)
void FuncInplace(int n, double *inout)
double operator()(double y) const
double operator()(double x) const
void MultiplyAccumulate(int n, const double *u, const double *v, double *out)
double operator()(double) const
void SoftmaxInPlace(int n, T *inout)
void CodeInBinary(int n, int nf, double *vec)
constexpr double kScaleFactor
void FuncMultiply(const double *u, const double *v, int n, double *out)
double Logistic(double x)
double operator()(double y) const
double operator()(double x) const
void ZeroVector(int n, T *vec)
double operator()(double x) const
void ClipVector(int n, T lower, T upper, T *vec)
void AccumulateVector(int n, const double *src, double *dest)
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
double operator()(double y) const
double operator()(double y) const