TRF Language Model
trf-alg.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 "trf-def.h"
20 
21 
22 namespace trf
23 {
24 
25  class Msg;
26  class VecIter;
27  class Algfb;
28 
29 
34  class Algfb
35  {
36  public:
37  int m_nOrder;
38  int m_nLen;
42 
43  public:
44  Algfb() : m_nOrder(0), m_nLen(0), m_nValueLimit(0) {};
45  ~Algfb();
47  void Prepare(int nLen, int nOrder, int nValueLimit);
49  void ForwardBackward(int nLen, int nOrder, int nValueLimit);
51  LogP GetMarginalLogProb(int nPos, int *pSubSeq, int nSubLen, double logz = 0);
55  virtual LogP ClusterSum(int *pSeq, int nLen, int nPos, int nOrder) = 0;
56  };
57 
62  class Msg
63  {
64  private:
65  float *m_pbuf;
66  int m_dim;
67  int m_size;
68 
69  //Model *m_pmodel; ///< the pointer to the model
70  public:
71  Msg(int nMsgDim, int nSize);
72  ~Msg();
73  void Fill(float v);
74  void Copy(Msg &m);
75  float& Get(int *pIdx, int nDim);
76  int GetBufSize() const { return pow(m_size, m_dim); }
77  };
78 
83  class VecIter
84  {
85  public:
86  int *m_pBuf;
87  int m_nDim;
88  int m_nMin;
89  int m_nMax;
90  public:
91  VecIter(int *p, int nDim, int nMin, int nMax);
92  void Reset();
93  bool Next();
94  };
95 
96 }
int m_nMin
the min value of each dimension
Definition: trf-alg.h:88
int m_nDim
dimension
Definition: trf-alg.h:87
int m_nLen
the sequence length.
Definition: trf-alg.h:38
int GetBufSize() const
Definition: trf-alg.h:76
void Prepare(int nLen, int nOrder, int nValueLimit)
prepare
Definition: trf-alg.cpp:31
double LogP
Definition: trf-def.h:27
int m_nValueLimit
the max-value at each position
Definition: trf-alg.h:39
int m_nOrder
the order, i.e. the node number at each cluster {x_1,x_2,...,x_n}
Definition: trf-alg.h:37
int * m_pBuf
buffer
Definition: trf-alg.h:86
virtual LogP ClusterSum(int *pSeq, int nLen, int nPos, int nOrder)=0
This function need be derived. Calcualte the log probability of each cluster.
Array< Msg * > m_aAlpha
the forward message
Definition: trf-alg.h:40
int m_nMax
the max value of eahc dimension
Definition: trf-alg.h:89
LogP GetLogSummation()
Get the summation over the sequence, corresponding to the log normalization constants &#39;logZ&#39;...
Definition: trf-alg.cpp:155
void ForwardBackward(int nLen, int nOrder, int nValueLimit)
forward-backward calculation
Definition: trf-alg.cpp:54
pFunc Reset & m
LogP GetMarginalLogProb(int nPos, int *pSubSeq, int nSubLen, double logz=0)
Get the marginal probability. &#39;logz&#39; is the input of the log normalization constants.
Definition: trf-alg.cpp:99
aLL Fill(0)
Array< Msg * > m_aBeta
the backward message
Definition: trf-alg.h:41
Definition: trf-alg.cpp:20
Dynamic array.
Definition: wb-vector.h:205