TRF Language Model
hrf-code-exam.cpp
Go to the documentation of this file.
1 #include "hrf-code-exam.h"
2 
3 namespace hrf
4 {
6  {
7  Vec<PValue> vParams(pm->GetParamNum());
8  vParams.Fill(v);
9  pm->SetParam(vParams.GetBuf());
10  }
12  {
13  Vec<PValue> vParams(pm->GetParamNum());
14  for (int i = 0; i < vParams.GetSize(); i++)
15  vParams[i] = 1.0 * rand() / RAND_MAX - 0.5;
16  pm->SetParam(vParams.GetBuf());
17  }
19  {
20  lout << "[ModelExam] Test Normalization (nlen=" << nLen << ")" << endl;
21  Vec<int> nodeSeq(nLen);
22  Seq seq(nLen, pm->m_hlayer, pm->m_hnode);
23 
24  LogP logz1 = pm->ExactNormalize(nLen);
25  LogP logz2 = trf::LogP_zero;
26  trf::VecIter nodeIter(nodeSeq.GetBuf(), nLen, 0, pm->GetEncodeNodeLimit() - 1);
27  while (nodeIter.Next()) {
28  pm->DecodeNode(nodeSeq, seq);
29  logz2 = trf::Log_Sum(logz2, pm->GetLogProb(seq, false));
30  }
31  lout_variable(logz1);
32  lout_variable(logz2);
33  }
35  {
36  lout << "[ModelExam] Test Expectation (nlen=" << nLen << ")" << endl;
37  Vec<int> nodeSeq(nLen);
38  Seq seq(nLen, pm->m_hlayer, pm->m_hnode);
39 
40  pm->ExactNormalize(nLen);
41 
42  Vec<double> exp1(pm->GetParamNum());
43  Vec<double> exp2(pm->GetParamNum());
44  exp1.Fill(0);
45  exp2.Fill(0);
46 
47  pm->GetNodeExp(nLen, exp1.GetBuf());
48  trf::VecIter nodeIter(nodeSeq.GetBuf(), nLen, 0, pm->GetEncodeNodeLimit() - 1);
49  while (nodeIter.Next()) {
50  pm->DecodeNode(nodeSeq, seq);
51  LogP lp = pm->GetLogProb(seq, true);
52  pm->FeatCount(seq, exp2, trf::LogP2Prob(lp));
53  }
54 
55  for (int i = 0; i < pm->GetParamNum(); i++) {
56  if (fabs(exp1[i] - exp2[i]) > 1e-4) {
57  lout << "[Warning] i="<<i<<" "<< exp1[i] << " " << exp2[i] << " " << exp1[i] - exp2[i] << endl;
58  }
59  }
60  }
61 
62  void ModelExam::TestHiddenExp(int nLen)
63  {
64  lout << "[ModelExam] Test Hidden Exp (nlen=" << nLen << ")" << endl;
65 
66  Vec<int> wseq(nLen);
67  wseq.Fill(0);
68 
69  pm->ExactNormalize(nLen);
70 
71  Vec<double> exp1(pm->GetParamNum());
72  Vec<double> exp2(pm->GetParamNum());
73  exp1.Fill(0);
74  exp2.Fill(0);
75 
76  LogP mglogp1, mglogp2, mplogpAIS;
77 
78  pm->GetHiddenExp(wseq, exp1.GetBuf());
79  mglogp1 = pm->GetLogProb(wseq, true);
80  mplogpAIS = pm->GetLogProb_AIS(wseq);
81 
82  Seq seq(nLen, pm->m_hlayer, pm->m_hnode);
83  seq.x.Set(wseq.GetBuf(), nLen, pm->GetVocab());
84  Vec<int> hnodeSeq(nLen);
85  trf::VecIter iter(hnodeSeq.GetBuf(), nLen, 0, pm->GetEncodeHiddenLimit() - 1);
86  LogP logpSum = trf::LogP_zero;
87  while (iter.Next()) {
88  pm->DecodeHidden(hnodeSeq, seq.h);
89  LogP lp = pm->GetLogProb(seq, true);
90  pm->FeatCount(seq, exp2, trf::LogP2Prob(lp));
91  logpSum = trf::Log_Sum(logpSum, lp);
92  }
93  exp2 /= trf::LogP2Prob(logpSum);
94  mglogp2 = logpSum;
95 
96 
97  lout_variable(mglogp1);
98  lout_variable(mglogp2);
99  lout_variable(mplogpAIS);
100 
101  for (int i = 0; i < pm->GetParamNum(); i++) {
102  if (fabs(exp1[i] - exp2[i]) > 1e-4) {
103  lout << "[Warning] i=" << i << " " << exp1[i] << " " << exp2[i] << " " << exp1[i] - exp2[i] << endl;
104  }
105  }
106  }
107 
108  void ModelExam::TestSample(int nLen /* = -1 */)
109  {
110  lout << "[ModelExam] Test Sample (nlen=" << nLen << ")" << endl;
111 
112  Vec<double> exp1(pm->GetParamNum());
113  Vec<double> exp2(pm->GetParamNum());
114  Vec<double> exp3(pm->GetParamNum());
115  Vec<Prob> len1(pm->GetMaxLen() + 1);
116  Vec<Prob> len2(pm->GetMaxLen() + 1);
117 
118  len1.Fill(1.0 / pm->GetMaxLen());
119  pm->SetPi(len1.GetBuf());
120 
121  if (nLen > 0) {
122  pm->ExactNormalize(nLen);
123  pm->GetNodeExp(nLen, exp1.GetBuf());
124  }
125  else {
126  pm->ExactNormalize();
127  pm->GetNodeExp(exp1.GetBuf());
128  }
129 
130  exp2.Fill(0);
131  exp3.Fill(0);
132  len2.Fill(0);
133  File fdbg("test_sample_exp.dbg", "wt");
134  fdbg.PrintArray("%f ", exp1.GetBuf(), exp1.GetSize());
135  int nSampleNum = 1000;
136  int nSampleTime = 100;
137  Seq seq;
138  pm->RandSeq(seq, nLen);
139  for (int t = 1; t <= nSampleTime; t++) {
140  for (int i = 0; i < nSampleNum; i++) {
141  if (nLen > 0)
142  pm->MarkovMove(seq);
143  else
144  pm->Sample(seq);
145 
146  pm->FeatCount(seq, exp2);
147  pm->GetHiddenExp(VecShell<VocabID>(seq.wseq(), seq.GetLen()), exp3.GetBuf());
148  len2[seq.GetLen()]++;
149  }
150 
151  exp2 /= t * nSampleNum;
152  exp3 /= t* nSampleNum;
153  fdbg.PrintArray("%f ", exp2.GetBuf(), exp2.GetSize());
154  fdbg.PrintArray("%f ", exp3.GetBuf(), exp3.GetSize());
155  double diff2 = VecDiff(exp1.GetBuf(), exp2.GetBuf(), exp2.GetSize());
156  double diff3 = VecDiff(exp1.GetBuf(), exp3.GetBuf(), exp3.GetSize());
157  lout << "t=" << t << " diff2-1=" << diff2 << " diff3-1=" << diff3 << endl;
158  exp2 *= t * nSampleNum;
159  exp3 *= t * nSampleNum;
160  }
161 
162  exp2 /= nSampleTime * nSampleNum;
163  len2 /= nSampleTime * nSampleNum;
164  for (int i = 1; i <= pm->GetMaxLen(); i++) {
165  lout << i << "\t" << len1[i] << "\t" << len2[i] << endl;
166  }
167  }
168 
170  {
171  ModelExam m(pfunc->GetModel());
172  m.SetValueRand();
173  m.pm->ExactNormalize();
174 
175  Vec<double> grad1(pfunc->GetParamNum());
176  Vec<double> grad2(pfunc->GetParamNum());
177 
178  }
179 }
int GetParamNum() const
Get the total parameter number.
Definition: hrf-model.h:130
void SetValueAll(PValue v)
set parameter values
const float LogP_zero
Definition: trf-def.h:30
int GetEncodeNodeLimit() const
The encoded integer size.
Definition: hrf-model.cpp:1139
int GetLen() const
Definition: hrf-model.h:54
int m_hnode
the number of hidden nodes
Definition: hrf-model.h:102
VocabID * wseq()
Definition: hrf-model.h:58
void TestSample(int nLen=-1)
test sample
trf::PValue PValue
Definition: hrf-model.h:24
void SetPi(Prob *pPi)
Set the pi.
Definition: trf-model.cpp:70
LogP Log_Sum(LogP x, LogP y)
Definition: trf-def.h:40
void GetNodeExp(double *pExp, Prob *pLenProb=NULL)
[exact] sum_l { n_l/n * E_{p_l}[f] }: Exactly calculate the expectation over x and h ...
Definition: hrf-model.cpp:470
virtual void SetParam(PValue *pParam)
Set the parameters.
Definition: hrf-model.cpp:78
void TestHiddenExp(int nLen)
test hidden expectation
double ExactNormalize(int nLen)
[exact] Exact Normalization, return the logz of given length
Definition: hrf-model.cpp:403
void SetValueRand()
set parameter values randomly
void FeatCount(Seq &seq, VecShell< double > featcount, Mat3dShell< double > VHcount, Mat3dShell< double > CHcount, Mat3dShell< double > HHcount, MatShell< double > Bcount, double dadd=1)
Count the feature number in current sequence, and add to the result.
Definition: hrf-model.cpp:1186
int GetEncodeHiddenLimit() const
The encoded integer size.
Definition: hrf-model.cpp:1166
Mat< HValue > h
Definition: hrf-model.h:44
#define lout_variable(x)
Definition: wb-log.h:179
trf::Seq x
Definition: hrf-model.h:43
void TestGradient()
test the sa gradient
void Fill(T v)
Definition: wb-mat.h:279
file class.
Definition: wb-file.h:94
void Set(Array< int > &aInt, Vocab *pv)
transform the word sequence (form file) to Seq
Definition: trf-feature.cpp:22
T * GetBuf() const
Definition: wb-mat.h:68
void DecodeNode(int n, VocabID &xi, VocabID &ci, VecShell< HValue > &hi)
decode a integer to the x_i and h_i
Definition: hrf-model.cpp:1124
Vocab * GetVocab() const
Get Vocab.
Definition: trf-model.h:102
double VecDiff(T *pVec1, T *pVec2, int len)
[Vec-function] |v1-v2|
Definition: wb-vector.h:591
void DecodeHidden(int n, VecShell< HValue > hi)
decode a integer to a hidden vector
Definition: hrf-model.cpp:1152
int m_hlayer
the number of hidden layer
Definition: hrf-model.h:101
void MarkovMove(Seq &seq)
[sample] Markov Move - perform the gibbs sampling
Definition: hrf-model.cpp:777
pFunc Reset & m
Log lout
the defination is in wb-log.cpp
Definition: wb-log.cpp:22
void PrintArray(const char *pformat, TYPE *pbuf, int num)
print a array into file
Definition: wb-file.h:148
Prob LogP2Prob(LogP x)
Definition: trf-def.h:33
trf::LogP LogP
Definition: hrf-model.h:27
LogP GetLogProb_AIS(VecShell< VocabID > &x, int nChain=10, int nIntermediate=10000)
perform AIS to esitmate the mariginal probabilities
Definition: hrf-model.cpp:1342
void RandSeq(Seq &seq, int nLen=-1)
Random init sequence, if nLen==-1, random the length also.
Definition: hrf-model.cpp:1083
int GetMaxLen() const
Get max-len.
Definition: trf-model.h:100
void GetHiddenExp(VecShell< int > x, double *pExp)
[exact] E_{p_l(h|x)}[f]: don&#39;t clean the pExp and directly add the new exp to pExp.
Definition: hrf-model.cpp:622
LogP GetLogProb(Seq &seq, bool bNorm=true)
calculate the probability
Definition: hrf-model.cpp:112
void Sample(Seq &seq)
[sample] Perform one train-dimensional mixture sampling
Definition: hrf-model.cpp:725
void TestNormalization(int nLen)
test the exact normalization
void TestExpectation(int nLen)
test feat expectation