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);
49 int index =
static_cast<int>(floor(x));
50 if (
TanhTable[index] == 0.0 && index > 0) {
59 double offset = x - index;
64 if (x < 0.0)
return 1.0 -
Logistic(-x);
67 int index =
static_cast<int>(floor(x));
77 double offset = x - index;
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
double operator()(double x) const
double operator()(double y) const
void FuncInplace(int n, double *inout)
void CodeInBinary(int n, int nf, double *vec)
void SoftmaxInPlace(int n, T *inout)
double operator()(double x) const
void CopyVector(int n, const double *src, double *dest)
double LogisticTable[kTableSize]
double operator()(double y) const
void ClipVector(int n, T lower, T upper, T *vec)
void MultiplyVectorsInPlace(int n, const double *src, double *inout)
double operator()(double y) const
void MultiplyAccumulate(int n, const double *u, const double *v, double *out)
double TanhTable[kTableSize]
double operator()(double y) const
const double kScaleFactor
void FuncMultiply(const double *u, const double *v, int n, double *out)
double operator()(double y) const
double operator()(double x) const
double operator()(double y) 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 x) const
void AccumulateVector(int n, const double *src, double *dest)
void ZeroVector(int n, T *vec)
double Logistic(double x)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
double operator()(double x) const
double operator()(double x) const
double operator()(double x) const