TRF Language Model
trf-def.h
Go to the documentation of this file.
1 // You may obtain a copy of the License at
2 //
3 // http://www.apache.org/licenses/LICENSE-2.0
4 //
5 // Unless required by applicable law or agreed to in writing, software
6 // distributed under the License is distributed on an "AS IS" BASIS,
7 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8 // See the License for the specific language governing permissions and
9 // limitations under the License.
10 //
11 // Copyright 2014-2015 Tsinghua University
12 // Author: wb.th08@gmail.com (Bin Wang), ozj@tsinghua.edu.cn (Zhijian Ou)
13 //
14 // All h, cpp, cc, and script files (e.g. bat, sh, pl, py) should include the above
15 // license declaration. Different coding language may use different comment styles.
16 
17 
18 #pragma once
19 #include "wb-system.h"
20 #include <omp.h>
21 using namespace wb;
22 
23 namespace trf
24 {
25 
26  typedef double PValue;
27  typedef double LogP;
28  typedef double Prob;
29  const float INF = 1e20;
30  const float LogP_zero = -INF;
31 
32 
33  inline Prob LogP2Prob(LogP x) {
34  return (x <= LogP_zero / 2) ? 0 : exp((double)(x));
35  }
36  inline LogP Prob2LogP(Prob x) {
37  return ((x) <= 0) ? LogP_zero : log((double)(x));
38  }
39 
40  inline LogP Log_Sum(LogP x, LogP y) {
41  return (x > y) ? x + Prob2LogP(1 + LogP2Prob(y - x)) : y + Prob2LogP(1 + LogP2Prob(x - y));
42  }
44  inline LogP Log_Sub(LogP x, LogP y) {
45  return (x > y) ? x + Prob2LogP(1 - LogP2Prob(y - x)) : y + Prob2LogP( LogP2Prob(x - y) - 1);
46  }
48  inline LogP Log_Sum(LogP *p, int num) {
49  LogP sum = LogP_zero;
50  for (int i = 0; i < num; i++) {
51  sum = Log_Sum(sum, p[i]);
52  }
53  return sum;
54  }
55 
56  LogP LogLineNormalize(LogP* pdProbs, int nNum);
57 
58  int LogLineSampling(const LogP* pdProbs, int nNum);
59 
60  void LineNormalize(Prob* pdProbs, int nNum);
61 
62  int LineSampling(const Prob* pdProbs, int nNum);
63 
64  bool Acceptable(Prob prob);
65 
66  void RandomPos(int *a, int len, int n);
67 
71  int omp_rand(int thread_num = -1);
72 
74  double dRand(double dmin, double dmax);
75 
77  int omp_nrand(int nMin, int nMax);
78 
80  void EasySmooth(Prob *p, int num, Prob threshold = 1e-5);
81 
83  /* t -> current intermediate number (from 0 to T-1); T -> total intermadiate number. */
84  double GetAISFactor(int t, int T);
85 
87  double SigmFunc(double x);
88 }
const float LogP_zero
Definition: trf-def.h:30
double Prob
Definition: trf-def.h:28
LogP Log_Sub(LogP x, LogP y)
log[exp(x)-exp(y)]
Definition: trf-def.h:44
int LogLineSampling(const LogP *pdProbs, int nNum)
Definition: trf-def.cpp:62
void RandomPos(int *a, int len, int n)
Definition: trf-def.cpp:132
double SigmFunc(double x)
calculate the sigmoid function f(x) = 1/(1+exp(-x))
Definition: trf-def.cpp:177
const float INF
Definition: trf-def.h:29
LogP Prob2LogP(Prob x)
Definition: trf-def.h:36
void LineNormalize(Prob *pdProbs, int nNum)
Definition: trf-def.cpp:87
double PValue
Definition: trf-def.h:26
double LogP
Definition: trf-def.h:27
double dRand(double dmin, double dmax)
get a random float between dmin and dmax
Definition: trf-def.cpp:147
int LineSampling(const Prob *pdProbs, int nNum)
Definition: trf-def.cpp:103
bool Acceptable(Prob prob)
Definition: trf-def.cpp:127
int omp_rand(int thread_num)
Definition: trf-def.cpp:23
void EasySmooth(Prob *p, int num, Prob threshold)
smooth a distribution
Definition: trf-def.cpp:157
LogP LogLineNormalize(LogP *pdProbs, int nNum)
Definition: trf-def.cpp:53
double GetAISFactor(int t, int T)
Get the AIS intermediate factor beta_t.
Definition: trf-def.cpp:165
Prob LogP2Prob(LogP x)
Definition: trf-def.h:33
LogP Log_Sum(LogP *p, int num)
log summate all the values in array
Definition: trf-def.h:48
int omp_nrand(int nMin, int nMax)
get a random integer int [nMin, nMax-1]
Definition: trf-def.cpp:152
Definition: trf-alg.cpp:20
include all the wb-written modules
define all the code written by Bin Wang.
Definition: wb-file.cpp:21