TRF Language Model
hrf-model.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 #ifndef _HRF_MODEL_H_
18 #define _HRF_MODEL_H_
19 
20 #include "trf-model.h"
21 
22 namespace hrf
23 {
24  typedef trf::PValue PValue; // parameter values
25  typedef float HValue; // hidden variable valuse
26  typedef trf::Prob Prob;
27  typedef trf::LogP LogP;
28  typedef trf::Vocab Vocab;
30 
31 
32  class Model;
33  class AlgNode;
34  class AlgLayer;
35 
40  class Seq
41  {
42  public:
45  int m_nLen;
46  int m_hlayer;
47  int m_hnode;
48 
49  public:
50  Seq() : m_nLen(0),m_hnode(0),m_hlayer(0) {};
51  Seq(int len, int hlayer, int hnode) : m_nLen(0), m_hnode(0), m_hlayer(0) { Reset(len, hlayer, hnode); }
52  Seq(Seq &seq) { Copy(seq); }
53  void SetLen(int len) { m_nLen = len; }
54  int GetLen() const { return m_nLen; }
55  int GetHlayer() const { return m_hlayer; }
56  int GetHnode() const { return m_hnode; }
58  VocabID *wseq() { return x.GetWordSeq(); }
59  VocabID *cseq() { return x.GetClassSeq(); }
60  void Reset(int len, int hlayer, int hnode);
61  void Copy(Seq &seq);
63  Seq GetSubSeq(int nPos, int nOrder);
65  bool operator == (Seq &s);
66  void Print();
67  void Write(File &file);
68  };
69 
74  class AlgNode : public trf::Algfb
75  {
76  private:
77  Model *m_pModel;
78  Seq m_seq;
79  public:
80  AlgNode(Model *p);
81  virtual LogP ClusterSum(int *pSeq, int nLen, int nPos, int nOrder);
82  };
83 
84 #define HHMap(h1, h2) (int)((h1) * 2 + (h2))
85 
86 #define HRF_VALUE_SET(p, m) \
87  memcpy(m.GetBuf(), p, sizeof(PValue)*m.GetSize()); \
88  p += m.GetSize();
89 #define HRF_VALUE_GET(p, m) \
90  memcpy(p, m.GetBuf(), sizeof(PValue)*m.GetSize()); \
91  p += m.GetSize();
92 
93 
98  class Model : public trf::Model
99  {
100  public:
101  int m_hlayer;
102  int m_hnode;
107 
108  protected:
110 
111  public:
112  Model(Vocab *pv) :trf::Model(pv), m_hlayer(0), m_hnode(0), m_nodeCal(this),
113  m_nSampleHAccTimes(0), m_nSampleHTotalTimes(0),
114  m_nLenJumpAccTimes(0), m_nLenJumpTotalTime(0)
115  {}
116  Model(Vocab *pv, int hlayer, int hnode, int maxlen) : trf::Model(pv, maxlen),
117  m_hlayer(hlayer), m_hnode(hnode), m_nodeCal(this),
118  m_nSampleHAccTimes(0), m_nSampleHTotalTimes(0),
119  m_nLenJumpAccTimes(0), m_nLenJumpTotalTime(0)
120  {
121  Reset(pv, hlayer, hnode, maxlen);
122  }
124  void Reset(Vocab *pv, int hlayer, int hnode, int maxlen);
126  int GetHnode() const { return m_hnode; }
128  int GetHiddenOrder() const { return 2; }
130  int GetParamNum() const { return trf::Model::GetParamNum() + m_m3dVH.GetSize() + m_m3dCH.GetSize() + m_m3dHH.GetSize() + m_matBias.GetSize(); }
132  virtual void SetParam(PValue *pParam);
134  void GetParam(PValue *pParam);
136  LogP GetLogProb(Seq &seq, bool bNorm = true);
137 
139  void ReadT(const char *pfilename);
141  void WriteT(const char *pfilename);
142 
143  public:
144  /* Exact calculatation function */
146  LogP GetLogProb(VecShell<VocabID> &x, bool bNorm = true);
148  LogP ClusterSum(Seq &seq, int nPos, int nOrder);
150  LogP HiddenClusterSum(Seq &seq, int nPos, int nOrder);
152  LogP FeatClusterSum(trf::Seq &x, int nPos, int nOrder);
154  LogP LayerClusterSum(Seq &seq, int nlayer, int nPos, int nOrder);
156  double ExactNormalize(int nLen);
158  void ExactNormalize();
160  LogP GetMarginalLogProb(int nLen, int nPos, Seq &sub, bool bNorm = true);
162  void GetNodeExp(double *pExp, Prob *pLenProb = NULL);
164  void GetNodeExp(int nLen, double *pExp);
166  void GetNodeExp(int nLen, VecShell<double> featexp,
168  MatShell<double> Bexp);
170  void GetHiddenExp(VecShell<int> x, double *pExp);
172  void GetLayerExp(AlgLayer &fb, int nLayer,
174  LogP logz = 0);
175 
176  public:
181  void Sample(Seq &seq);
184  void LocalJump(Seq &seq);
186  void MarkovMove(Seq &seq);
188  LogP ProposeLength(int nOld, int &nNew, bool bSample);
190  LogP ProposeH0(VecShell<HValue> &hi, Seq &seq, int nPos, bool bSample);
192  LogP ProposeC0(VocabID &ci, Seq &seq, int nPos, bool bSample);
194  /*
195  * \param [out] logps return K values, denoting logP(h_{i,k} = 1), k=1,...,K
196  * \param [in] seq the sequence.
197  * \param [in] nPos position i.
198  * \param [in] bConsiderXandC =true mean considering the VH and CH matrix, otherwise only HH matrix.
199  * \details The function return the probability of each component and used to sample h_i or calculate Q.
200  */
201  void ProposeHProbs(VecShell<LogP> &logps, Seq &seq, int nPos, bool bConsiderXandC = false);
203  void ProposeCProbs(VecShell<LogP> &logps, Seq &seq, int nPos);
205  LogP GetReducedModelForH(Seq &seq, int nPos);
207  LogP GetReducedModelForC(Seq &seq, int nPos);
209  LogP GetReducedModelForW(Seq &seq, int nPos);
211  LogP GetConditionalProbForH(VecShell<HValue> &hi, VecShell<Prob> &probs);
213  LogP GetMarginalProbOfC(Seq &seq, int nPos);
215  void SampleC(Seq &seq, int nPos);
217  /* if bSample=ture, then sample w[nPos]. Otherwise only calculate the conditional probabilities of current w[nPos]. */
218  LogP SampleW(Seq &seq, int nPos, bool bSample = true);
220  void RandSeq(Seq &seq, int nLen = -1);
222  void RandHidden(Seq &seq);
224  /* different with SampleH, which using MH sample to sample h
225  If tagH != NULL, then just calcualte the transition probability.
226  */
227  virtual LogP SampleHAndCGivenX(Seq &seq, MatShell<HValue> *tagH = NULL);
228 
229  public:
231  int EncodeNode(VocabID xi, VocabID ci, VecShell<HValue> &hi);
233  void EncodeNode(VecShell<int> &vn, Seq &seq, int nPos = 0, int nDim = -1);
235  void DecodeNode(int n, VocabID &xi, VocabID &ci, VecShell<HValue> &hi);
237  void DecodeNode(VecShell<int> &vn, Seq &seq, int nPos = 0, int nDim = -1);
239  int GetEncodeNodeLimit() const;
241  int EncodeHidden(VecShell<HValue> hi);
243  void DecodeHidden(int n, VecShell<HValue> hi);
245  void DecodeHidden(VecShell<int> &vn, Mat<HValue> &h, int nPos = 0, int nDim = -1);
247  int GetEncodeHiddenLimit() const;
249  void DecodeLayer(VecShell<int> &vn, Mat<HValue> &h, int layer, int nPos = 0, int nDim = -1);
251  int GetEncodeLayerLimit() const;
252 
253  public:
255  template <typename T>
256  void BufMap(T *p, VecShell<T> &feat, Mat3dShell<T> &VH, Mat3dShell<T> &CH, Mat3dShell<T> &HH, MatShell<T> &Bias)
257  {
258  feat.Reset(p, trf::Model::GetParamNum());
259  p += feat.GetSize();
260 
261  VH.Reset(p, m_m3dVH.GetXDim(), m_m3dVH.GetYDim(), m_m3dVH.GetZDim());
262  p += VH.GetSize();
263  CH.Reset(p, m_m3dCH.GetXDim(), m_m3dCH.GetYDim(), m_m3dCH.GetZDim());
264  p += CH.GetSize();
265  HH.Reset(p, m_m3dHH.GetXDim(), m_m3dHH.GetYDim(), m_m3dHH.GetZDim());
266  p += HH.GetSize();
267  Bias.Reset(p, m_matBias.GetRow(), m_matBias.GetCol());
268  }
270  void FeatCount(Seq &seq, VecShell<double> featcount,
271  Mat3dShell<double> VHcount, Mat3dShell<double> CHcount, Mat3dShell<double> HHcount,
272  MatShell<double> Bcount,
273  double dadd = 1);
275  void HiddenFeatCount(Seq &seq,
276  Mat3dShell<double> VHcount, Mat3dShell<double> CHcount, Mat3dShell<double> HHcount,
277  MatShell<double> Bcount,
278  double dadd = 1);
280  void FeatCount(Seq &seq, VecShell<double> count, double dadd = 1);
281 
282  public:
283  PValue SumVHWeight(MatShell<PValue> m, VecShell<HValue> h);
284  PValue SumHHWeight(Mat3dShell<PValue> m, VecShell<HValue> h1, VecShell<HValue> h2);
285  PValue SumVHWeight(MatShell<PValue> m, VecShell<HValue> h, int layer);
286  PValue SumHHWeight(Mat3dShell<PValue> m, VecShell<HValue> h1, VecShell<HValue> h2, int layer);
287 
288  public:
290  void PerformSAMS(int nMinibatch, int tmax, int t0, int beta, double zgap = 10);
292  LogP GetLogProb_AIS(VecShell<VocabID> &x, int nChain = 10, int nIntermediate = 10000);
294  LogP GetLogProb_Gibbs(VecShell<VocabID> &x, int num = 100);
295  };
296 
297 
302  class AlgLayer : public trf::Algfb
303  {
304  public:
307  int m_nlayer;
308  public:
309  AlgLayer(Model *p, VecShell<VocabID> x, int nlayer);
310  virtual LogP ClusterSum(int *pSeq, int nLen, int nPos, int nOrder);
311  };
312 
313 }
314 
315 
316 #endif
int m_nSampleHAccTimes
sample H the acceptance times
Definition: hrf-model.h:177
int GetParamNum() const
Get the total parameter number.
Definition: hrf-model.h:130
trf::Vocab Vocab
Definition: hrf-model.h:28
int GetSize() const
Definition: wb-mat.h:126
void Write(File &file)
Definition: hrf-model.cpp:53
int GetHnode() const
get hidden node dimension
Definition: hrf-model.h:126
int GetParamNum() const
Get parameter number.
Definition: trf-model.h:106
double Prob
Definition: trf-def.h:28
trf::VocabID VocabID
Definition: hrf-model.h:29
int m_hlayer
Definition: hrf-model.h:46
int GetLen() const
Definition: hrf-model.h:54
m WriteT(cfg_pathModelWrite)
VocabID * cseq()
Definition: hrf-model.h:59
int m_hnode
the number of hidden nodes
Definition: hrf-model.h:102
void Copy(Seq &seq)
Definition: hrf-model.cpp:15
Seq GetSubSeq(int nPos, int nOrder)
Return the sub-sequence.
Definition: hrf-model.cpp:21
int VocabID
Definition: trf-vocab.h:23
VocabID * wseq()
Definition: hrf-model.h:58
trf::PValue PValue
Definition: hrf-model.h:24
void Reset(int len, int hlayer, int hnode)
Definition: hrf-model.cpp:5
bool operator==(Seq &s)
If the two sequence is equal.
Definition: hrf-model.cpp:34
VecShell< VocabID > GetWordSeq()
Definition: hrf-model.h:57
Mat3d< PValue > m_m3dHH
the weight between adjacent Hidden(H)
Definition: hrf-model.h:105
Model * m_pModel
Definition: hrf-model.h:305
double PValue
Definition: trf-def.h:26
int m_nLen
mutiple hidden matrix [position * (layer * hnode)]
Definition: hrf-model.h:45
hidden-random-field model
Definition: hrf-model.h:98
AlgNode m_nodeCal
the forward-backward calculation for node (x and h)
Definition: hrf-model.h:109
trf::Prob Prob
Definition: hrf-model.h:26
void BufMap(T *p, VecShell< T > &feat, Mat3dShell< T > &VH, Mat3dShell< T > &CH, Mat3dShell< T > &HH, MatShell< T > &Bias)
Map a paremeter vector to each kinds of parameters.
Definition: hrf-model.h:256
int m_nLenJumpTotalTime
Definition: hrf-model.h:180
double LogP
Definition: trf-def.h:27
int GetSize() const
Definition: wb-mat.h:173
void SetLen(int len)
Definition: hrf-model.h:53
Mat< HValue > h
Definition: hrf-model.h:44
void Reset(T *p, int size)
Definition: wb-mat.h:72
trf::Seq x
Definition: hrf-model.h:43
define a sequence including the word sequence and class sequence
Definition: trf-feature.h:41
int GetHnode() const
Definition: hrf-model.h:56
Model(Vocab *pv)
Definition: hrf-model.h:112
TRF model.
Definition: trf-model.h:51
int GetHlayer() const
Definition: hrf-model.h:55
int m_nlayer
the layer
Definition: hrf-model.h:307
file class.
Definition: wb-file.h:94
int GetSize() const
Definition: wb-mat.h:69
Mat3d< PValue > m_m3dCH
the weight between Class(C) and Hidden(H)
Definition: hrf-model.h:104
int GetHiddenOrder() const
Get HH mat order.
Definition: hrf-model.h:128
Seq(int len, int hlayer, int hnode)
Definition: hrf-model.h:51
VocabID * GetClassSeq()
get class sequence
Definition: trf-feature.h:82
int m_hlayer
the number of hidden layer
Definition: hrf-model.h:101
float HValue
Definition: hrf-model.h:25
void Print()
Definition: hrf-model.cpp:43
pFunc Reset & m
VocabID * GetWordSeq()
get word sequence
Definition: trf-feature.h:80
int m_nLenJumpAccTimes
lenght jump the acceptance times
Definition: hrf-model.h:179
int m_nSampleHTotalTimes
sample H the total times
Definition: hrf-model.h:178
Seq(Seq &seq)
Definition: hrf-model.h:52
int GetCol() const
Definition: wb-mat.h:129
trf::LogP LogP
Definition: hrf-model.h:27
int GetRow() const
Definition: wb-mat.h:128
Definition: trf-alg.cpp:20
int m_hnode
Definition: hrf-model.h:47
Mat< PValue > m_matBias
the bias for each value of Hidden(H)
Definition: hrf-model.h:106
void Reset(T *p, int xdim, int ydim, int zdim)
Definition: wb-mat.h:178
void Reset(T *pbuf, int row, int col)
Definition: wb-mat.h:130
Mat3d< PValue > m_m3dVH
the weight between Word(V) and Hidden(H)
Definition: hrf-model.h:103
Model(Vocab *pv, int hlayer, int hnode, int maxlen)
Definition: hrf-model.h:116