12 h.
Reset(len, hlayer * hnode);
23 if (nPos + nOrder >
m_nLen) {
24 lout_error(
"[Seq] GetSubSeq: nPos+nOrder > nLen!!");
27 for (
int i = nPos; i < nPos + nOrder; i++) {
30 sub.
h[i - nPos] =
h[i];
39 if (
x.
x == s.
x.
x &&
h == s.
h)
45 for (
int i = 0; i <
h.
GetCol(); i++) {
46 for (
int j = 0; j <
m_nLen; j++) {
47 lout <<
h[j][i] <<
"\t";
55 ofstream ofile(file.
fp);
56 for (
int i = 0; i <
h.
GetCol(); i++) {
57 for (
int j = 0; j <
m_nLen; j++) {
58 ofile <<
h[j][i] <<
"\t";
73 m_m3dHH.Reset(
m_hlayer * m_hnode, m_hnode, 4);
74 m_matBias.Reset(
m_hlayer*m_hnode, 2);
82 pParam += m_pFeat->GetNum();
99 pParam += m_pFeat->GetNum();
117 double dfactor = 1.0;
120 for (
int i = 0; i < seq.
GetLen(); i++) {
121 logSum += dfactor * SumVHWeight(m_m3dVH[seq.
wseq()[i]], seq.
h[i]);
125 if (m_m3dCH.GetSize() > 0) {
126 for (
int i = 0; i < seq.
GetLen(); i++) {
127 logSum += dfactor * SumVHWeight(m_m3dCH[seq.
cseq()[i]], seq.
h[i]);
132 for (
int i = 0; i < seq.
GetLen() - 1; i++) {
133 logSum += dfactor * SumHHWeight(m_m3dHH, seq.
h[i], seq.
h[i + 1]);
137 for (
int i = 0; i < seq.
GetLen(); i++) {
138 logSum += dfactor * SumVHWeight(m_matBias, seq.
h[i]);
143 int nLen = min(m_maxlen, seq.
GetLen());
151 File fout(pfilename,
"rt");
153 lout <<
"[Model]: Read(txt) from " << pfilename << endl;
156 fout.
Scanf(
"m_vocabsize=%d\n", &nVocabSize);
157 fout.
Scanf(
"m_maxlen=%d\n", &m_maxlen);
162 if (m_pVocab->GetSize() != nVocabSize) {
163 lout_error(
"[Model] ReadT: the input nVocabSize(" << nVocabSize <<
") != m_pVocab->GetSize(" << m_pVocab->GetSize() <<
")");
167 fout.
Scanf(
"m_pi=[ ");
168 for (
int i = 1; i <= m_maxlen; i++) {
169 fout.
Scanf(
"%lf ", &dValue);
173 fout.
Scanf(
"m_logz=[ ");
174 for (
int i = 1; i <= m_maxlen; i++) {
175 fout.
Scanf(
"%lf ", &dValue);
179 fout.
Scanf(
"m_zeta=[ ");
180 for (
int i = 1; i <= m_maxlen; i++) {
181 fout.
Scanf(
"%lf ", &dValue);
187 fout.
Scanf(
"featnum=%d\n", &nValue);
188 m_value.Reset(nValue);
192 m_pFeat->ReadT(fout, m_value.GetBuf());
200 while (pLine = fout.
GetLine()) {
203 String strLabel = strtok(pLine,
": \t");
204 pLine = strtok(NULL,
": \t");
205 if (strLabel ==
"m_matVH")
208 sscanf(pLine,
"(num=%d*%d)", &nRow, &nCol);
209 m_m3dVH.Reset(nRow, nCol, 2);
212 else if (strLabel ==
"m_matCH")
215 sscanf(pLine,
"(num=%d*%d)", &nRow, &nCol);
216 m_m3dCH.Reset(nRow, nCol, 2);
219 else if (strLabel ==
"m_matHH")
221 sscanf(pLine,
"(num=%d*%d)", &nRow, &nCol);
222 m_m3dHH.Reset(nRow, nCol, 4);
225 else if (strLabel ==
"m_matBias")
227 sscanf(pLine,
"(num=%d)", &nRow);
228 m_matBias.Reset(nRow, 2);
229 m_matBias.Read(fout);
235 File fout(pfilename,
"wt");
236 lout <<
"[Model] Write(txt) to " << pfilename << endl;
238 fout.
Print(
"m_vocabsize=%d\n", m_pVocab->GetSize());
239 fout.
Print(
"m_maxlen=%d\n", m_maxlen);
242 fout.
Print(
"m_pi=[ ");
243 for (
int i = 1; i <= m_maxlen; i++) {
244 fout.
Print(
"%f ", m_pi[i]);
247 fout.
Print(
"m_logz=[ ");
248 for (
int i = 1; i <= m_maxlen; i++) {
249 fout.
Print(
"%f ", m_logz[i]);
252 fout.
Print(
"m_zeta=[ ");
253 for (
int i = 1; i <= m_maxlen; i++) {
254 fout.
Print(
"%f ", m_zeta[i]);
258 fout.
Print(
"featnum=%d\n", m_pFeat->GetNum());
259 m_pFeat->WriteT(fout, m_value.GetBuf());
262 fout.
Print(
"m_matVH: (num=%d*%d)\n", m_m3dVH.GetXDim(), m_m3dVH.GetYDim());
266 fout.
Print(
"m_matCH: (num=%d*%d)\n", m_m3dCH.GetXDim(), m_m3dCH.GetYDim());
270 fout.
Print(
"m_matHH: (num=%d*%d)\n", m_m3dHH.GetXDim(), m_m3dHH.GetYDim());
274 fout.
Print(
"m_matBias: (num=%d)\n", m_matBias.GetRow());
275 m_matBias.Write(fout);
280 for (
int layer = 0; layer <
m_hlayer; layer++) {
289 logProb += FeatClusterSum(trfseq, 0, x.
GetSize());
292 int nLen = min(m_maxlen, x.
GetSize());
300 return FeatClusterSum(seq.
x, nPos, nOrder) + HiddenClusterSum(seq, nPos, nOrder);
311 double dfactor = 1.0;
314 LogSum += dfactor * SumVHWeight(m_m3dVH[seq.
wseq()[nPos]], seq.
h[nPos]);
316 if (nPos == seq.
GetLen() - nOrder) {
317 for (
int i = nPos + 1; i < seq.
GetLen(); i++) {
318 LogSum += dfactor * SumVHWeight(m_m3dVH[seq.
wseq()[i]], seq.
h[i]);
323 if (m_m3dCH.GetSize() > 0) {
324 LogSum += dfactor * SumVHWeight(m_m3dCH[seq.
cseq()[nPos]], seq.
h[nPos]);
326 if (nPos == seq.
GetLen() - nOrder) {
327 for (
int i = nPos + 1; i < seq.
GetLen(); i++) {
328 LogSum += dfactor * SumVHWeight(m_m3dCH[seq.
cseq()[i]], seq.
h[i]);
335 LogSum += dfactor * SumHHWeight(m_m3dHH, seq.
h[nPos], seq.
h[nPos + 1]);
337 if (nPos == seq.
GetLen() - nOrder) {
338 for (
int i = nPos + 1; i < seq.
GetLen() - 1; i++)
339 LogSum += dfactor * SumHHWeight(m_m3dHH, seq.
h[i], seq.
h[i + 1]);
344 LogSum += dfactor * SumVHWeight(m_matBias, seq.
h[nPos]);
346 if (nPos == seq.
GetLen() - nOrder) {
347 for (
int i = nPos + 1; i < seq.
GetLen(); i++) {
348 LogSum += dfactor * SumVHWeight(m_matBias, seq.
h[i]);
359 double dfactor = 1.0;
362 LogSum += dfactor * SumVHWeight(m_m3dVH[seq.
wseq()[nPos]], seq.
h[nPos], nlayer);
364 if (nPos == seq.
GetLen() - nOrder) {
365 for (
int i = nPos + 1; i < seq.
GetLen(); i++) {
366 LogSum += dfactor * SumVHWeight(m_m3dVH[seq.
wseq()[i]], seq.
h[i], nlayer);
371 if (m_m3dCH.GetSize() > 0) {
372 LogSum += dfactor * SumVHWeight(m_m3dCH[seq.
cseq()[nPos]], seq.
h[nPos], nlayer);
374 if (nPos == seq.
GetLen() - nOrder) {
375 for (
int i = nPos + 1; i < seq.
GetLen(); i++) {
376 LogSum += dfactor * SumVHWeight(m_m3dCH[seq.
cseq()[i]], seq.
h[i], nlayer);
383 LogSum += dfactor * SumHHWeight(m_m3dHH, seq.
h[nPos], seq.
h[nPos + 1], nlayer);
385 if (nPos == seq.
GetLen() - nOrder) {
386 for (
int i = nPos + 1; i < seq.
GetLen() - 1; i++)
387 LogSum += dfactor * SumHHWeight(m_m3dHH, seq.
h[i], seq.
h[i + 1], nlayer);
392 LogSum += dfactor * SumVHWeight(m_matBias, seq.
h[nPos], nlayer);
394 if (nPos == seq.
GetLen() - nOrder) {
395 for (
int i = nPos + 1; i < seq.
GetLen(); i++) {
396 LogSum += dfactor * SumVHWeight(m_matBias, seq.
h[i], nlayer);
405 int nMaxOrder = max(GetMaxOrder(), GetHiddenOrder());
406 int nIterDim = min(nMaxOrder, nLen);
414 for (
VocabID x = m_pVocab->IterBeg();
x <= m_pVocab->IterEnd();
x++) {
416 VocabID cid = m_pVocab->GetClass(
x);
417 xseq.
Set(&
x, 1, m_pVocab);
418 double d1 = FeatClusterSum(xseq, 0, 1);
423 d2 +=
trf::Log_Sum(m_matBias[k][0] + m_m3dVH[
x][k][0] + m_m3dCH[cid][k][0],
424 m_matBias[k][1] + m_m3dVH[
x][k][1] + m_m3dCH[cid][k][1]);
427 d2 +=
trf::Log_Sum(m_matBias[k][0] + m_m3dVH[
x][k][0], m_matBias[k][1] + m_m3dVH[
x][k][1]);
432 m_logz[nLen] = dLogSum;
435 int nEncoderLimit = GetEncodeNodeLimit();
437 m_nodeCal.ForwardBackward(nLen, nMaxOrder, nEncoderLimit);
439 m_logz[nLen] = m_nodeCal.GetLogSummation();
447 for (
int len = 1; len <= m_maxlen; len++) {
449 m_zeta[len] = m_logz[len] - m_logz[1];
457 if (nPos + sub.
GetLen() > nLen) {
458 lout_error(
"[Model] GetMarginalLogProb: nPos(" << nPos <<
")+nOrder(" << sub.
GetLen() <<
") > seq.len(" << nLen <<
")!!");
463 EncodeNode(nsub, sub);
465 LogP dSum = m_nodeCal.GetMarginalLogProb(nPos, nsub.GetBuf(), nsub.GetSize());
467 return (bNorm) ? dSum - m_logz[nLen] : dSum;
472 if (pLenProb == NULL)
473 pLenProb = m_pi.GetBuf();
477 double *p = expTemp.
GetBuf();
481 BufMap(p, featexp, VHexp, CHexp, HHexp, Bexp);
484 for (
int len = 1; len <= m_maxlen; len++) {
486 int nMaxOrder = max(GetMaxOrder(), GetHiddenOrder());
487 m_nodeCal.ForwardBackward(len, nMaxOrder, GetEncodeNodeLimit());
489 GetNodeExp(len, featexp, VHexp, CHexp, HHexp, Bexp);
494 for (
int i = 0; i < exp.
GetSize(); i++) {
495 exp[i] += pLenProb[len] * expTemp[i];
504 BufMap(pExp, featexp, VHexp, CHexp, HHexp, Bexp);
505 GetNodeExp(nLen, featexp, VHexp, CHexp, HHexp, Bexp);
519 double dfactor = 1.0;
522 int nClusterNum = nLen - m_nodeCal.m_nOrder + 1;
523 int nClusterDim = m_nodeCal.m_nOrder;
524 if (nClusterNum < 1) {
533 for (
int pos = 0; pos < nClusterNum; pos++) {
536 while (iter.Next()) {
537 DecodeNode(nseq, seq, pos, nClusterDim);
544 for (
int n = 1; n <= nClusterDim; n++) {
545 m_pFeat->
Find(afeat, seq.
x, pos, n);
547 for (
int i = 0; i < afeat.
GetNum(); i++) {
548 featexp[afeat[i]] += prob;
553 VHexp[
x][k][(int)(seq.
h[pos][k])] += dfactor * prob;
555 if (m_pVocab->GetClassNum() > 0) {
558 CHexp[c][k][(int)(seq.
h[pos][k])] += dfactor * prob;
561 if (nClusterDim > 1) {
562 for (
int l = 0; l <
m_hlayer; l++) {
563 for (
int a = 0; a <
m_hnode; a++) {
564 for (
int b = 0; b <
m_hnode; b++) {
565 HHexp[l*m_hnode + a][b][
HHMap(seq.
h[pos][l*m_hnode + a], seq.
h[pos + 1][l*m_hnode + b])] += dfactor * prob;
571 Bexp[k][(int)(seq.
h[pos][k])] += dfactor * prob;
578 if (pos == nClusterNum - 1) {
580 for (
int ii = 1; ii < nClusterDim; ii++) {
581 for (
int n = 1; n <= nClusterDim - ii; n++) {
582 m_pFeat->Find(afeat, seq.
x, pos + ii, n);
585 for (
int i = 0; i < afeat.
GetNum(); i++) {
586 featexp[afeat[i]] += prob;
589 for (
int ii = 1; ii < nClusterDim; ii++) {
592 VHexp[
x][k][seq.
h[pos + ii][k]] += dfactor * prob;
595 if (m_pVocab->GetClassNum() > 0) {
596 for (
int ii = 1; ii < nClusterDim; ii++) {
599 CHexp[c][k][seq.
h[pos + ii][k]] += dfactor * prob;
603 for (
int ii = 1; ii < nClusterDim - 1; ii++) {
604 for (
int l = 0; l <
m_hlayer; l++) {
605 for (
int a = 0; a <
m_hnode; a++) {
606 for (
int b = 0; b <
m_hnode; b++) {
607 HHexp[l*m_hnode + a][b][
HHMap(seq.
h[pos + ii][l*m_hnode + a], seq.
h[pos + ii + 1][l*m_hnode + b])] += dfactor * prob;
612 for (
int ii = 1; ii < nClusterDim; ii++) {
614 Bexp[k][seq.
h[pos + ii][k]] += dfactor * prob;
628 BufMap(pExp, featexp, VHexp, CHexp, HHexp, Bexp);
632 int nMaxOrder = GetHiddenOrder();
634 for (
int layer = 0; layer <
m_hlayer; layer++) {
641 GetLayerExp(fb, layer, VHexp, CHexp, HHexp, Bexp, logz);
657 int nClusterNum = nLen - fb.
m_nOrder + 1;
659 if (nClusterNum < 1) {
664 double dfactor = 1.0;
667 for (
int pos = 0; pos < nClusterNum; pos++) {
670 while (iter.Next()) {
671 DecodeLayer(hseq, h, nLayer, pos, nClusterDim);
676 for (
int k = nLayer*m_hnode; k < nLayer*m_hnode+
m_hnode; k++) {
677 VHexp[
x][k][h[pos][k]] += dfactor * prob;
679 if (m_pVocab->GetClassNum() > 0) {
681 for (
int k = nLayer*m_hnode; k < nLayer*m_hnode +
m_hnode; k++) {
682 CHexp[c][k][h[pos][k]] += dfactor * prob;
685 if (nClusterDim > 1) {
686 for (
int a = 0; a <
m_hnode; a++) {
687 for (
int b = 0; b <
m_hnode; b++) {
688 HHexp[nLayer*m_hnode + a][b][
HHMap(h[pos][nLayer*m_hnode + a], h[pos + 1][nLayer*m_hnode + b])] += dfactor * prob;
692 for (
int k = nLayer*m_hnode; k < nLayer*m_hnode +
m_hnode; k++) {
693 Bexp[k][h[pos][k]] += dfactor * prob;
697 if (pos == nClusterNum - 1) {
698 for (
int ii = 1; ii < nClusterDim; ii++) {
700 for (
int k = nLayer*m_hnode; k < nLayer*m_hnode +
m_hnode; k++) {
701 VHexp[
x][k][h[pos + ii][k]] += dfactor * prob;
703 if (m_pVocab->GetClassNum() > 0) {
705 for (
int k = nLayer*m_hnode; k < nLayer*m_hnode +
m_hnode; k++) {
706 CHexp[c][k][h[pos + ii][k]] += dfactor * prob;
709 for (
int k = nLayer*m_hnode; k < nLayer*m_hnode +
m_hnode; k++) {
710 Bexp[k][h[pos + ii][k]] += dfactor * prob;
713 for (
int ii = 1; ii < nClusterDim - 1; ii++) {
714 for (
int a = 0; a <
m_hnode; a++) {
715 for (
int b = 0; b <
m_hnode; b++) {
716 HHexp[nLayer*m_hnode + a][b][
HHMap(h[pos + ii][nLayer*m_hnode + a], h[pos + ii + 1][nLayer*m_hnode + b])] += dfactor * prob;
732 int nOldLen = seq.
GetLen();
734 LogP j1 = ProposeLength(nOldLen, nNewLen,
true);
735 LogP j2 = ProposeLength(nNewLen, nOldLen,
false);
737 if (nNewLen == nOldLen)
741 if (nNewLen == nOldLen + 1) {
742 LogP logpold = GetLogProb(seq);
744 LogP Q = ProposeH0(seq.
h[nNewLen - 1], seq, nNewLen - 1,
true);
745 LogP R = ProposeC0(seq.
cseq()[nNewLen - 1], seq, nNewLen - 1,
true);
746 LogP G = SampleW(seq, nNewLen - 1);
747 LogP logpnew = GetLogProb(seq);
749 logpAcc = (j2 - j1) + logpnew - (logpold + Q + R + G);
751 else if (nNewLen == nOldLen - 1) {
752 LogP logpold = GetLogProb(seq);
753 LogP Q = ProposeH0(seq.
h[nOldLen - 1], seq, nOldLen - 1,
false);
754 LogP R = ProposeC0(seq.
cseq()[nOldLen - 1], seq, nOldLen - 1,
false);
755 LogP G = SampleW(seq, nOldLen - 1,
false);
758 LogP logpnew = GetLogProb(seq);
760 logpAcc = (j2 - j1) + logpnew + Q + R + G - logpold;
762 else if (nNewLen != nOldLen){
763 lout_error(
"[Model] Sample: nNewLen(" << nNewLen <<
") and nOldLen(" << nOldLen <<
")");
769 m_nLenJumpAccTimes++;
774 m_nLenJumpTotalTime++;
780 SampleHAndCGivenX(seq);
781 for (
int nPos = 0; nPos < seq.
GetLen(); nPos++) {
803 if (nPos + 1 > seq.
GetLen()) {
808 ProposeHProbs(logps, seq, nPos);
813 for (
int i = 0; i < logps.
GetSize(); i++) {
819 LogP resLogp = GetConditionalProbForH(hi, logps);
827 if (m_pVocab->GetClassNum() == 0) {
832 Vec<LogP> vlogps(m_pVocab->GetClassNum());
833 ProposeCProbs(vlogps, seq, nPos);
848 double dfactor = 1.0;
851 if (nPos - 1 >= 0 && nPos - 1 <= seq.
GetLen() - 1) {
852 for (
int l = 0; l <
m_hlayer; l++) {
853 for (
int i = 0; i <
m_hnode; i++) {
854 HValue curh = seq.
h[nPos - 1][l*m_hnode + i];
855 for (
int j = 0; j <
m_hnode; j++) {
856 matLogp.
Get(l*m_hnode + j, 0) += dfactor * m_m3dHH.Get(l*m_hnode + i, j,
HHMap(curh, 0));
857 matLogp.
Get(l*m_hnode + j, 1) += dfactor * m_m3dHH.Get(l*m_hnode + i, j,
HHMap(curh, 1));
862 if (nPos + 1 <= seq.
GetLen() - 1) {
863 for (
int l = 0; l <
m_hlayer; l++) {
864 for (
int i = 0; i <
m_hnode; i++) {
865 HValue curh = seq.
h[nPos + 1][l*m_hnode + i];
866 for (
int j = 0; j <
m_hnode; j++) {
867 matLogp.
Get(l*m_hnode + j, 0) += dfactor * m_m3dHH.Get(l*m_hnode + j, i,
HHMap(0, curh));
868 matLogp.
Get(l*m_hnode + j, 1) += dfactor * m_m3dHH.Get(l*m_hnode + j, i,
HHMap(1, curh));
876 matLogp[i][0] += dfactor * m_matBias[i][0];
877 matLogp[i][1] += dfactor * m_matBias[i][1];
880 if (bConsiderXandC) {
883 matLogp[i][0] += dfactor * m_m3dVH[seq.
wseq()[nPos]][i][0];
884 matLogp[i][1] += dfactor * m_m3dVH[seq.
wseq()[nPos]][i][1];
886 if (m_m3dCH.GetSize() > 0) {
889 matLogp[i][0] += dfactor * m_m3dCH[seq.
cseq()[nPos]][i][0];
890 matLogp[i][1] += dfactor * m_m3dCH[seq.
cseq()[nPos]][i][1];
900 logps[i] = matLogp[i][1] -
trf::Log_Sum(matLogp[i][1], matLogp[i][0]);
906 for (
int cid = 0; cid < m_pVocab->GetClassNum(); cid++) {
907 seq.
cseq()[nPos] = cid;
908 logps[cid] = GetReducedModelForC(seq, nPos);
910 seq.
cseq()[nPos] = savecid;
918 double dfactor = 1.0;
920 for (
int i = max(0, nPos - 1); i <= min(seq.
GetLen() - 2, nPos); i++) {
921 logSum += dfactor * SumHHWeight(m_m3dHH, seq.
h[i], seq.
h[i + 1]);
924 logSum += dfactor * SumVHWeight(m_matBias, seq.
h[nPos]);
934 double dfactor = 1.0;
935 if (m_m3dCH.GetSize() > 0) {
936 logSum += dfactor * SumVHWeight(m_m3dCH[seq.
cseq()[nPos]], seq.
h[nPos]);
947 double dfactor = 1.0;
948 logSum += dfactor * SumVHWeight(m_m3dVH[seq.
wseq()[nPos]], seq.
h[nPos]);
955 for (
int i = 0; i < hi.
GetSize(); i++) {
956 resLogp += (hi[i] == 0) ?
trf::Log_Sub(0, logps[i]) : logps[i];
968 for (
int i = 0; i < pXs->
GetNum(); i++) {
969 seq.
wseq()[nPos] = pXs->
Get(i);
972 resLogp =
trf::Log_Sum(resLogp, GetReducedModelForW(seq, nPos) + GetReducedModelForC(seq, nPos));
975 seq.
wseq()[nPos] = saveX;
981 if (m_pVocab->GetClassNum() == 0) {
987 Vec<LogP> vlogps_c(m_pVocab->GetClassNum());
988 ProposeCProbs(vlogps_c, seq, nPos);
991 LogP logpRi = vlogps_c[ci];
992 LogP logpR0 = vlogps_c[C0];
996 seq.
cseq()[nPos] = ci;
997 LogP Logp_ci = GetMarginalProbOfC(seq, nPos);
998 seq.
cseq()[nPos] = C0;
999 LogP Logp_C0 = GetMarginalProbOfC(seq, nPos);
1001 LogP acclogp = logpRi + Logp_C0 - (logpR0 + Logp_ci);
1003 m_nSampleHTotalTimes++;
1005 m_nSampleHAccTimes++;
1006 seq.
cseq()[nPos] = C0;
1009 seq.
cseq()[nPos] = ci;
1019 if (nPos >= seq.
GetLen()) {
1020 lout_error(
"[Model] SampleH: the nPos(" << nPos <<
") > the length of sequence(" << seq.
GetLen() <<
")");
1027 for (
int i = 0; i < pXs->GetNum(); i++) {
1028 seq.
wseq()[nPos] = pXs->Get(i);
1032 aLogps[i] = GetReducedModelForW(seq, nPos);
1040 seq.
wseq()[nPos] = pXs->Get(idx);
1043 idx = pXs->Find(nSaveX);
1044 seq.
wseq()[nPos] = nSaveX;
1046 lout_error(
"Can't find the VocabID(" << nSaveX <<
") in the array.\n" 1047 <<
"This may beacuse word(" << nSaveX <<
") doesnot belongs to class(" << seq.
cseq()[nPos] <<
")");
1055 LogP totallogProb = 0;
1061 for (
int nPos = 0; nPos < seq.
GetLen(); nPos++) {
1063 ProposeHProbs(vlogps_h, seq, nPos,
true);
1067 hsample.
Copy((*tagH)[nPos]);
1070 for (
int i = 0; i < hsample.
GetSize(); i++) {
1075 seq.
h[nPos] = hsample;
1077 LogP logprob = GetConditionalProbForH(hsample, vlogps_h);
1078 totallogProb += logprob;
1080 return totallogProb;
1093 for (
int i = 0; i < seq.
h.
GetRow(); i++) {
1094 for (
int k = 0; k < seq.
h.
GetCol(); k++) {
1095 seq.
h[i][k] = rand() % 2;
1104 for (
int i = 0; i < seq.
h.
GetRow(); i++) {
1105 for (
int k = 0; k < seq.
h.
GetCol(); k++) {
1106 seq.
h[i][k] = rand() % 2;
1113 int hnum = EncodeHidden(hi);
1115 return hnum * m_pVocab->GetSize() + xi;
1119 nDim = (nDim == -1) ? seq.
GetLen() - nPos : nDim;
1120 for (
int i = nPos; i < nPos + nDim; i++) {
1121 vn[i] = EncodeNode(seq.
wseq()[i], seq.
cseq()[i], seq.
h[i]);
1126 int hnum = n / m_pVocab->GetSize();
1128 xi = n % m_pVocab->GetSize();
1129 ci = m_pVocab->GetClass(xi);
1130 DecodeHidden(hnum, hi);
1134 nDim = (nDim == -1) ? vn.
GetSize() - nPos : nDim;
1135 for (
int i = nPos; i < nPos + nDim; i++) {
1136 DecodeNode(vn[i], seq.
wseq()[i], seq.
cseq()[i], seq.
h[i]);
1141 return GetEncodeHiddenLimit() * m_pVocab->GetSize();
1146 for (
int i = 0; i < hi.
GetSize(); i++) {
1147 hnum += (int)hi[i] * (1 << i);
1154 for (
int i = 0; i < hi.
GetSize(); i++) {
1161 nDim = (nDim == -1) ? vn.
GetSize() - nPos : nDim;
1162 for (
int i = nPos; i < nPos + nDim; i++) {
1163 DecodeHidden(vn[i], h[i]);
1176 nDim = (nDim == -1) ? vn.
GetSize() - nPos : nDim;
1177 for (
int i = nPos; i < nPos + nDim; i++) {
1178 DecodeHidden(vn[i], h[i].GetSub(layer*
m_hnode, m_hnode));
1192 HiddenFeatCount(seq, VHcount, CHcount, HHcount, Bcount, dadd);
1199 double dfactor = 1.0;
1201 for (
int i = 0; i < seq.
GetLen(); i++) {
1203 VHcount[seq.
wseq()[i]][k][seq.
h[i][k]] += dfactor * dadd;
1208 if (m_pVocab->GetClassNum() > 0) {
1209 for (
int i = 0; i < seq.
GetLen(); i++) {
1211 CHcount[seq.
cseq()[i]][k][seq.
h[i][k]] += dfactor * dadd;
1217 for (
int i = 0; i < seq.
GetLen() - 1; i++) {
1218 for (
int l = 0; l <
m_hlayer; l++) {
1219 for (
int a = 0; a <
m_hnode; a++) {
1220 for (
int b = 0; b <
m_hnode; b++) {
1221 HHcount.
Get(l * m_hnode + a, b,
HHMap(seq.
h.
Get(i, l*m_hnode + a), seq.
h.
Get(i + 1, l*m_hnode + b))) += dfactor * dadd;
1228 for (
int i = 0; i < seq.
GetLen(); i++) {
1230 Bcount[k][seq.
h[i][k]] += dfactor * dadd;
1239 BufMap(count.
GetBuf(), featcount, VHcount, CHcount, HHcount, Bcount);
1240 FeatCount(seq, featcount, VHcount, CHcount, HHcount, Bcount, dadd);
1246 for (
int i = 0; i < h.
GetSize(); i++) {
1247 dsum += m[i][(int)h[i]];
1255 for (
int k = 0; k <
m_hlayer; k++) {
1256 for (
int i = 0; i <
m_hnode; i++) {
1257 for (
int j = 0; j <
m_hnode; j++)
1259 dsum += m.
Get(k*m_hnode + i, j,
HHMap(h1[k*m_hnode + i], h2[k*m_hnode + j]));
1269 dsum += m[i][(int)h[i]];
1278 for (
int i = 0; i <
m_hnode; i++) {
1279 for (
int j = 0; j <
m_hnode; j++)
1281 dsum += m.
Get(k*m_hnode + i, j,
HHMap(h1[k*m_hnode + i], h2[k*m_hnode + j]));
1290 int nThread = omp_get_max_threads();
1291 Mat<double> m_matSampleLen(nThread, GetMaxLen() + 1);
1296 for (
int i = 0; i < nThread; i++) {
1305 for (
int t = 1; t <= tmax; t++) {
1306 m_matSampleLen.
Fill(0);
1307 m_vecSampleLen.
Fill(0);
1309 #pragma omp parallel for 1310 for (
int m = 0;
m < nMinibatch;
m++) {
1311 int tid = omp_get_thread_num();
1312 this->Sample(*aSeqs[tid]);
1313 int nLen = min(GetMaxLen(), aSeqs[tid]->
GetLen());
1314 m_matSampleLen[tid][nLen]++;
1318 for (
int i = 0; i < nThread; i++) {
1319 m_vecSampleLen += m_matSampleLen[i];
1321 m_vecSampleLen /= nMinibatch;
1326 gamma = 1.0 / pow(t, beta);
1329 gamma = 1.0 / (pow(t0, beta) + t - t0);
1333 for (
int i = 1; i <= GetMaxLen(); i++) {
1334 zeta[i] += min(zgap, gamma * m_vecSampleLen[i] / m_pi[i]);
1336 this->SetZeta(zeta.
GetBuf());
1345 int nParamsNumOfIntermediateModel = GetParamNum() - m_pFeat->GetNum();
1347 Vec<PValue> vParamsPn(nParamsNumOfIntermediateModel);
1348 Vec<PValue> vParamsP0(nParamsNumOfIntermediateModel);
1349 Vec<PValue> vParamsCur(nParamsNumOfIntermediateModel);
1361 vParamsPn.
Copy(vParamsP0);
1362 p = vParamsPn.
GetBuf() + m_m3dVH.GetSize() + m_m3dCH.GetSize();
1363 memset(p, 0,
sizeof(
PValue)*m_m3dHH.GetSize());
1367 for (
int nPos = 0; nPos < nLen; nPos++) {
1369 VocabID cid = m_pVocab->GetClass(xid);
1374 d2 +=
trf::Log_Sum(m_m3dVH[xid][k][0] + m_m3dCH[cid][k][0] + m_matBias[k][0], m_m3dVH[xid][k][1] + m_m3dCH[cid][k][1] + m_matBias[k][1]);
1377 d2 +=
trf::Log_Sum(m_m3dVH[xid][k][0] + m_matBias[k][0], m_m3dVH[xid][k][1] + m_matBias[k][1]);
1386 for (
int i = 0; i < aInterModel.
GetNum(); i++) {
1392 aInterModel[i]->SetPi(m_pi.GetBuf());
1393 aInterModel[i]->m_zeta.
Copy(m_zeta);
1394 aInterModel[i]->m_logz.
Copy(m_logz);
1395 lout_assert(aInterModel[i]->GetParamNum() == nParamsNumOfIntermediateModel);
1398 aLogWeight.
SetNum(nChain);
1403 for (
int k = 0; k < nChain; k++) {
1405 Model *pInterModel = aInterModel[tid];
1410 for (
int nPos = 0; nPos < seq.
GetLen(); nPos++) {
1412 VocabID cid = m_pVocab->GetClass(xid);
1416 curP[0] = m_m3dVH[xid][k][0] + m_m3dCH[cid][k][0] + m_matBias[k][0];
1417 curP[1] = m_m3dVH[xid][k][1] + m_m3dCH[cid][k][1] + m_matBias[k][1];
1420 curP[0] = m_m3dVH[xid][k][0] + m_matBias[k][0];
1421 curP[1] = m_m3dVH[xid][k][1] + m_matBias[k][1];
1434 for (
int t = nIntermediate - 1; t >= 0; t--) {
1438 for (
int i = 0; i < vParamsCur.
GetSize(); i++)
1439 vParamsCur[i] = vParamsP0[i] * (1 - beta) + vParamsPn[i] * beta;
1451 aLogWeight[k] = log_w;
1455 for (
int i = 0; i < aInterModel.
GetNum(); i++) {
1465 LogP logpx = FeatClusterSum(seq.
x, 0, nLen);
1467 return logprob + logpx;
1478 for (
int i = 0; i < num; i++) {
1479 SampleHAndCGivenX(seq);
1480 LogP curlogp = GetLogProb(seq,
true);
1481 if (curlogp > maxLogp) {
1497 m_seq.Reset(nLen, m_pModel->m_hlayer, m_pModel->m_hnode);
1498 m_pModel->DecodeNode(
VecShell<int>(pSeq, nLen), m_seq, nPos, nOrder);
1499 return m_pModel->ClusterSum(m_seq, nPos, nOrder);
1511 m_pModel->DecodeLayer(
VecShell<int>(pSeq, nLen), m_seq.h, m_nlayer, nPos, nOrder);
1512 return m_pModel->LayerClusterSum(m_seq, m_nlayer, nPos, nOrder);
virtual LogP SampleHAndCGivenX(Seq &seq, MatShell< HValue > *tagH=NULL)
[sample] sample h given x using gibbs sampling.
AlgLayer(Model *p, VecShell< VocabID > x, int nlayer)
void Copy(Seq &seq)
copy the sequence
T & Get(unsigned int i, unsigned int j)
void Copy(const Array< T > &array)
Copy the array to current array.
#define SAFE_DELETE(p)
memory release
LogP Log_Sub(LogP x, LogP y)
log[exp(x)-exp(y)]
int GetEncodeNodeLimit() const
The encoded integer size.
T & Get(int i)
get the value at position i
void LocalJump(Seq &seq)
[sample] Local Jump - sample a new length
int LogLineSampling(const LogP *pdProbs, int nNum)
int m_hnode
the number of hidden nodes
Seq GetSubSeq(int nPos, int nOrder)
Return the sub-sequence.
void Random(Vocab *pv)
Random.
void Reset(Vocab *pv, int maxlen)
reset, the maxlen is the length excluding the beg/end symbols.
virtual LogP ClusterSum(int *pSeq, int nLen, int nPos, int nOrder)
This function need be derived. Calcualte the log probability of each cluster.
void Reset(int len, int hlayer, int hnode)
bool operator==(Seq &s)
If the two sequence is equal.
int m_nLen
the sequence length.
void ExactNormalize()
[exact] Exact Normalization all the length
LogP GetMarginalLogProb(int nLen, int nPos, Seq &sub, bool bNorm=true)
[exact] Exactly calculate the marginal probability at position 'nPos' and with order 'nOrder' ...
LogP GetLogProb_Gibbs(VecShell< VocabID > &x, int num=100)
sample the best hidden and calculate the joint probability.
LogP Log_Sum(LogP x, LogP y)
LogP HiddenClusterSum(Seq &seq, int nPos, int nOrder)
[exact] Calculate the logp in each cluster. Only consinder the VH,CH,HH values, used in class AlgHidd...
LogP GetLogProb(Seq &seq, bool bNorm=true)
calculate the probability
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 ...
int m_nLen
mutiple hidden matrix [position * (layer * hnode)]
T & Get(int x, int y, int z)
virtual void SetParam(PValue *pParam)
Set the parameters.
hidden-random-field model
void GetLayerExp(AlgLayer &fb, int nLayer, Mat3dShell< double > &VHexp, Mat3dShell< double > &CHexp, Mat3dShell< double > &HHexp, MatShell< double > &Bexp, LogP logz=0)
[exact] called in GetHiddenExp.
virtual void SetParam(PValue *pValue)
Set the parameters.
LogP GetReducedModelForC(Seq &seq, int nPos)
[sample] A unnormalized reduced model to sample class c_i.
virtual int Scanf(const char *p_pMessage,...)
scanf
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.
int GetEncodeHiddenLimit() const
The encoded integer size.
void Reset(int p_len)
reset only change the len variable, does not change the buffer size.
T * GetBuffer(int i=0) const
get the buffer pointer
LogP GetMarginalProbOfC(Seq &seq, int nPos)
[sample] Fixed h, given c_i, summate the probabilities of x_i, i.e. P(c_i)
void SampleC(Seq &seq, int nPos)
[sample] Sample the c_i at position nPos given h_i without x_i.
virtual void Print(const char *p_pMessage,...)
print
int m_nOrder
the order, i.e. the node number at each cluster {x_1,x_2,...,x_n}
define a sequence including the word sequence and class sequence
LogP ProposeC0(VocabID &ci, Seq &seq, int nPos, bool bSample)
[sample] Propose the c_{i} at position i. Then return the propose probability R(c_i|h_i,c_{other})
void FeatCount(Seq &seq, double *pCount, double dadd=1.0)
Count the feature number in a sequence.
LogP ClusterSum(Seq &seq, int nPos, int nOrder)
[exact] Calculate the logP in each cluster. Only used for forward-backword algorithms ( class AlgNode...
int m_nTotalNum
total feature number
void ReadT(const char *pfilename)
Read Model.
int LineSampling(const Prob *pdProbs, int nNum)
LogP ProposeH0(VecShell< HValue > &hi, Seq &seq, int nPos, bool bSample)
[sample] Propose the h_{i} at position i. Then return the propose probability Q(h_i|h_{other}) ...
LogP GetReducedModelForC(Seq &seq, int nPos)
[sample] A unnormalized reduced model to sample class c_i, consindering CH matrix(U) and class-ngram ...
int Find(T t)
Find a value and return the position.
LogP SampleW(Seq &seq, int nPos, bool bSample=true)
[sample] Sample the w_i at position nPos
void GetParam(PValue *pValue)
Get the paremetre vector.
int GetEncodeLayerLimit() const
The encoded integer size of one layer.
void Set(Array< int > &aInt, Vocab *pv)
transform the word sequence (form file) to Seq
LogP ProposeLength(int nOld, int &nNew, bool bSample)
[sample] Propose the length, using the variable m_matLenJump
#define HRF_VALUE_SET(p, m)
void SetNum(int n)
Set Array number, to melloc enough memory.
virtual char * GetLine(bool bPrecent=false)
Read a line into the buffer.
bool Acceptable(Prob prob)
LogP GetLogSummation()
Get the summation over the sequence, corresponding to the log normalization constants 'logZ'...
void ProposeCProbs(VecShell< LogP > &logps, Seq &seq, int nPos)
[sample] Return the distribution of c_i at position nPos
void DecodeNode(int n, VocabID &xi, VocabID &ci, VecShell< HValue > &hi)
decode a integer to the x_i and h_i
void Clean()
Clean the array. Just set the top of array to -1 and donot release the memory.
void HiddenFeatCount(Seq &seq, Mat3dShell< double > VHcount, Mat3dShell< double > CHcount, Mat3dShell< double > HHcount, MatShell< double > Bcount, double dadd=1)
Count the hidden features.
int EncodeNode(VocabID xi, VocabID ci, VecShell< HValue > &hi)
encode the x_i and h_i at position i to a integer
Vocab * GetVocab() const
Get Vocab.
LogP ClusterSum(Seq &seq, int nPos, int nOrder)
Read Binary.
void PerformSAMS(int nMinibatch, int tmax, int t0, int beta, double zgap=10)
perform the SAMS to estimate the normalization constants zeta
LogP LayerClusterSum(Seq &seq, int nlayer, int nPos, int nOrder)
[exact] Calculate the logp in each cluster. Only consinder the VH,CH,HH values on such layer ...
int GetNum() const
Get Array number.
void Reset(int row=0, int col=0)
void DecodeHidden(int n, VecShell< HValue > hi)
decode a integer to a hidden vector
VocabID * GetClassSeq()
get class sequence
int m_hlayer
the number of hidden layer
LogP LogLineNormalize(LogP *pdProbs, int nNum)
void ForwardBackward(int nLen, int nOrder, int nValueLimit)
forward-backward calculation
void MarkovMove(Seq &seq)
[sample] Markov Move - perform the gibbs sampling
double GetAISFactor(int t, int T)
Get the AIS intermediate factor beta_t.
VocabID * GetWordSeq()
get word sequence
Log lout
the defination is in wb-log.cpp
LogP FeatClusterSum(trf::Seq &x, int nPos, int nOrder)
[exact] Calculate the logp in each cluster. Only consinder the feature values
void Fill(T m)
set all the values to m
void Reset(Vocab *pv, int hlayer, int hnode, int maxlen)
reset, the maxlen is the length excluding the beg/end symbols.
LogP GetMarginalLogProb(int nPos, int *pSubSeq, int nSubLen, double logz=0)
Get the marginal probability. 'logz' is the input of the log normalization constants.
void ProposeHProbs(VecShell< LogP > &logps, Seq &seq, int nPos, bool bConsiderXandC=false)
[sample] A reduced model only consinder HHmat(W) and VHmat(M) and CHmat(U).
#define HRF_VALUE_GET(p, m)
void RandHidden(Seq &seq)
Random init the hidden variables.
void GetParam(PValue *pParam)
Get the paremetre vector.
void Copy(MatShell< T > &m)
int EncodeHidden(VecShell< HValue > hi)
encode the hidden vector h_i to a integer
void DecodeLayer(VecShell< int > &vn, Mat< HValue > &h, int layer, int nPos=0, int nDim=-1)
decoder several integer to a sequence
LogP GetReducedModelForW(Seq &seq, int nPos)
[sample] A unnormalized reduced model to sample word w_i, consindering VH matrix(M) and word-ngram (l...
include all the feature table
PValue SumHHWeight(Mat3dShell< PValue > m, VecShell< HValue > h1, VecShell< HValue > h2)
virtual LogP ClusterSum(int *pSeq, int nLen, int nPos, int nOrder)
This function need be derived. Calcualte the log probability of each cluster.
LogP GetLogProb_AIS(VecShell< VocabID > &x, int nChain=10, int nIntermediate=10000)
perform AIS to esitmate the mariginal probabilities
void RandSeq(Seq &seq, int nLen=-1)
Random init sequence, if nLen==-1, random the length also.
PValue SumVHWeight(MatShell< PValue > m, VecShell< HValue > h)
LogP GetConditionalProbForH(VecShell< HValue > &hi, VecShell< Prob > &probs)
[sample] using the logprobs returned by ProposeHProb to calculate the logprob of hi.
void Copy(VecShell< T > v)
LogP GetReducedModelForH(Seq &seq, int nPos)
[sample] A unnormalized reduced model. It only consindering the HH matrix (W)
void GetHiddenExp(VecShell< int > x, double *pExp)
[exact] E_{p_l(h|x)}[f]: don't clean the pExp and directly add the new exp to pExp.
LogP GetLogProb(Seq &seq, bool bNorm=true)
calculate the probability
void Sample(Seq &seq)
[sample] Perform one train-dimensional mixture sampling
LogP GetReducedModelForW(Seq &seq, int nPos)
[sample] A unnormalized reduced model to sample word w_i.
void WriteT(const char *pfilename)
Write Model.