use of structures._ParentDoc4DCM in project IR_Base by Linda-sunshine.
the class DCMLDA4AC method updateBeta.
protected void updateBeta(int tid) {
double diff = 0;
int iteration = 0;
double smoothingBeta = 0.1;
do {
diff = 0;
double deltaBeta = 0;
double wordNum4Tid = 0;
double[] wordNum4Tid4V = new double[vocabulary_size];
double totalBetaDenominator = 0;
double[] totalBetaNumerator = new double[vocabulary_size];
Arrays.fill(totalBetaNumerator, 0);
Arrays.fill(wordNum4Tid4V, 0);
m_totalBeta[tid] = Utils.sumOfArray(m_beta[tid]);
double digBeta4Tid = Utils.digamma(m_totalBeta[tid]);
for (_Doc d : m_trainSet) {
if (d instanceof _ParentDoc4DCM) {
_ParentDoc4DCM pDoc = (_ParentDoc4DCM) d;
totalBetaDenominator += Utils.digamma(m_totalBeta[tid] + pDoc.m_topic_stat[tid]) - digBeta4Tid;
for (int v = 0; v < vocabulary_size; v++) {
wordNum4Tid += pDoc.m_wordTopic_stat[tid][v];
wordNum4Tid4V[v] += pDoc.m_wordTopic_stat[tid][v];
totalBetaNumerator[v] += Utils.digamma(m_beta[tid][v] + pDoc.m_wordTopic_stat[tid][v]);
totalBetaNumerator[v] -= Utils.digamma(m_beta[tid][v]);
}
}
}
for (int v = 0; v < vocabulary_size; v++) {
if (wordNum4Tid == 0)
break;
if (wordNum4Tid4V[v] == 0) {
deltaBeta = 0;
} else {
deltaBeta = totalBetaNumerator[v] / totalBetaDenominator;
}
double newBeta = m_beta[tid][v] * deltaBeta + d_beta;
double t_diff = Math.abs(m_beta[tid][v] - newBeta);
if (t_diff > diff)
diff = t_diff;
m_beta[tid][v] = newBeta;
}
iteration++;
System.out.println("beta iteration\t" + iteration);
} while (diff > m_newtonConverge);
System.out.println("iteration\t" + iteration);
}
use of structures._ParentDoc4DCM in project IR_Base by Linda-sunshine.
the class DCMLDA4AC method cal_logLikelihood_partial4Parent.
protected double cal_logLikelihood_partial4Parent(_ParentDoc4DCM d) {
double likelihood = 0;
for (_Word w : d.getWords()) {
int wid = w.getIndex();
double wordLikelihood = 0;
for (int k = 0; k < number_of_topics; k++) {
wordLikelihood += d.m_topics[k] * d.m_wordTopic_prob[k][wid];
}
likelihood += Math.log(wordLikelihood);
}
return likelihood;
}
use of structures._ParentDoc4DCM in project IR_Base by Linda-sunshine.
the class DCMCorrLDA method cal_logLikelihood_partial4Child.
protected double cal_logLikelihood_partial4Child(_ChildDoc cDoc) {
double childLikelihood = 0;
_ParentDoc4DCM pDoc = (_ParentDoc4DCM) cDoc.m_parentDoc;
for (_Word w : cDoc.getTestWords()) {
int wid = w.getIndex();
double wordLogLikelihood = 0;
for (int k = 0; k < number_of_topics; k++) {
double wordPerTopicLikelihood = cDoc.m_topics[k] * pDoc.m_wordTopic_prob[k][wid];
wordLogLikelihood += wordPerTopicLikelihood;
}
childLikelihood += Math.log(wordLogLikelihood);
}
return childLikelihood;
}
use of structures._ParentDoc4DCM in project IR_Base by Linda-sunshine.
the class DCMCorrLDA method updateAlphaC.
protected void updateAlphaC() {
double diff = 0;
int iteration = 0;
do {
diff = 0;
double totalAlphaDenominator = 0;
double[] totalAlphaNumerator = new double[number_of_topics];
Arrays.fill(totalAlphaNumerator, 0);
m_totalAlpha_c = Utils.sumOfArray(m_alpha_c);
double deltaAlpha = 0;
for (_Doc d : m_trainSet) {
if (d instanceof _ParentDoc) {
_ParentDoc4DCM pDoc = (_ParentDoc4DCM) d;
double pDocLen = pDoc.getTotalDocLength();
for (_ChildDoc cDoc : pDoc.m_childDocs) {
double muDp = cDoc.getMu() / pDocLen;
double t_totalAlpha_c = m_totalAlpha_c + cDoc.getMu();
double digAlpha = Utils.digamma(t_totalAlpha_c);
totalAlphaDenominator += Utils.digamma(cDoc.getTotalDocLength() + t_totalAlpha_c) - digAlpha;
for (int k = 0; k < number_of_topics; k++) totalAlphaNumerator[k] += Utils.digamma(m_alpha_c[k] + muDp * pDoc.m_sstat[k] + cDoc.m_sstat[k]) - Utils.digamma(m_alpha_c[k] + muDp * pDoc.m_sstat[k]);
}
}
}
for (int k = 0; k < number_of_topics; k++) {
deltaAlpha = totalAlphaNumerator[k] * 1.0 / totalAlphaDenominator;
double newAlpha = m_alpha_c[k] * deltaAlpha;
double t_diff = Math.abs(m_alpha_c[k] - newAlpha);
if (t_diff > diff)
diff = t_diff;
m_alpha_c[k] = newAlpha;
}
iteration++;
if (iteration > m_newtonIter)
break;
} while (diff > m_newtonConverge);
// System.out.println("iteration\t" + iteration);
m_totalAlpha_c = 0;
for (int k = 0; k < number_of_topics; k++) {
m_totalAlpha_c += m_alpha_c[k];
}
}
use of structures._ParentDoc4DCM in project IR_Base by Linda-sunshine.
the class DCMCorrLDA method initTest.
protected void initTest(ArrayList<_Doc> sampleTestSet, _Doc d) {
_ParentDoc4DCM pDoc = (_ParentDoc4DCM) d;
for (_Stn stnObj : pDoc.getSentences()) {
stnObj.setTopicsVct(number_of_topics);
}
int testLength = 0;
pDoc.setTopics4GibbsTest(number_of_topics, 0, testLength, vocabulary_size);
sampleTestSet.add(pDoc);
for (_ChildDoc cDoc : pDoc.m_childDocs) {
testLength = (int) (m_testWord4PerplexityProportion * cDoc.getTotalDocLength());
cDoc.setTopics4GibbsTest(number_of_topics, d_alpha, testLength);
for (_Word w : cDoc.getWords()) {
int wid = w.getIndex();
int tid = w.getTopic();
pDoc.m_wordTopic_stat[tid][wid]++;
pDoc.m_topic_stat[tid]++;
}
sampleTestSet.add(cDoc);
cDoc.createSparseVct4Infer();
// cDoc computeMu
computeTestMu4Doc(cDoc);
}
}
Aggregations