use of structures._Word in project IR_Base by Linda-sunshine.
the class ACCTM_C method initialize_probability.
protected void initialize_probability(Collection<_Doc> collection) {
createSpace();
for (int i = 0; i < number_of_topics; i++) Arrays.fill(word_topic_sstat[i], d_beta);
Arrays.fill(m_sstat, d_beta * vocabulary_size);
for (_Doc d : collection) {
if (d instanceof _ParentDoc) {
d.setTopics4Gibbs(number_of_topics, 0);
for (_Stn stnObj : d.getSentences()) stnObj.setTopicsVct(number_of_topics);
} else if (d instanceof _ChildDoc4BaseWithPhi) {
((_ChildDoc4BaseWithPhi) d).createXSpace(number_of_topics, m_gamma.length, vocabulary_size, d_beta);
((_ChildDoc4BaseWithPhi) d).setTopics4Gibbs(number_of_topics, 0);
computeMu4Doc((_ChildDoc) d);
}
if (d instanceof _ParentDoc) {
for (_Word w : d.getWords()) {
word_topic_sstat[w.getTopic()][w.getIndex()]++;
m_sstat[w.getTopic()]++;
}
} else if (d instanceof _ChildDoc4BaseWithPhi) {
for (_Word w : d.getWords()) {
int xid = w.getX();
int tid = w.getTopic();
int wid = w.getIndex();
// update global
if (xid == 0) {
word_topic_sstat[tid][wid]++;
m_sstat[tid]++;
}
}
}
}
imposePrior();
m_statisticsNormalized = false;
}
use of structures._Word in project IR_Base by Linda-sunshine.
the class ACCTM_C method sampleInChildDoc.
protected void sampleInChildDoc(_Doc d) {
_ChildDoc4BaseWithPhi cDoc = (_ChildDoc4BaseWithPhi) d;
int wid, tid, xid;
double normalizedProb;
for (_Word w : cDoc.getWords()) {
wid = w.getIndex();
tid = w.getTopic();
xid = w.getX();
if (xid == 0) {
cDoc.m_xTopicSstat[xid][tid]--;
cDoc.m_xSstat[xid]--;
cDoc.m_wordXStat.put(wid, cDoc.m_wordXStat.get(wid) - 1);
if (m_collectCorpusStats) {
word_topic_sstat[tid][wid]--;
m_sstat[tid]--;
}
} else if (xid == 1) {
cDoc.m_xTopicSstat[xid][wid]--;
cDoc.m_xSstat[xid]--;
cDoc.m_childWordSstat--;
}
normalizedProb = 0;
double pLambdaZero = childXInDocProb(0, cDoc);
double pLambdaOne = childXInDocProb(1, cDoc);
for (tid = 0; tid < number_of_topics; tid++) {
double pWordTopic = childWordByTopicProb(tid, wid);
double pTopic = childTopicInDocProb(tid, cDoc);
m_topicProbCache[tid] = pWordTopic * pTopic * pLambdaZero;
normalizedProb += m_topicProbCache[tid];
}
double pWordTopic = childLocalWordByTopicProb(wid, cDoc);
m_topicProbCache[tid] = pWordTopic * pLambdaOne;
normalizedProb += m_topicProbCache[tid];
normalizedProb *= m_rand.nextDouble();
for (tid = 0; tid < m_topicProbCache.length; tid++) {
normalizedProb -= m_topicProbCache[tid];
if (normalizedProb <= 0)
break;
}
if (tid == m_topicProbCache.length)
tid--;
if (tid < number_of_topics) {
xid = 0;
w.setX(xid);
w.setTopic(tid);
cDoc.m_xTopicSstat[xid][tid]++;
cDoc.m_xSstat[xid]++;
if (cDoc.m_wordXStat.containsKey(wid)) {
cDoc.m_wordXStat.put(wid, cDoc.m_wordXStat.get(wid) + 1);
} else {
cDoc.m_wordXStat.put(wid, 1);
}
if (m_collectCorpusStats) {
word_topic_sstat[tid][wid]++;
m_sstat[tid]++;
}
} else if (tid == (number_of_topics)) {
xid = 1;
w.setX(xid);
w.setTopic(tid);
cDoc.m_xTopicSstat[xid][wid]++;
cDoc.m_xSstat[xid]++;
cDoc.m_childWordSstat++;
}
}
}
use of structures._Word in project IR_Base by Linda-sunshine.
the class ACCTM_CZ method cal_logLikelihood_partial4Child.
// change it into proportion rather than the last sample
@Override
protected double cal_logLikelihood_partial4Child(_Doc d) {
_ChildDoc4BaseWithPhi cDoc = (_ChildDoc4BaseWithPhi) d;
double docLogLikelihood = 0.0;
double gammaLen = Utils.sumOfArray(m_gamma);
double cDocXSum = Utils.sumOfArray(cDoc.m_xSstat);
for (_Word w : cDoc.getTestWords()) {
int wid = w.getIndex();
double wordLogLikelihood = 0;
for (int k = 0; k < number_of_topics; k++) {
double term1 = childWordByTopicProb(k, wid);
double term2 = childTopicInDoc(k, cDoc);
double term3 = childXInDocProb(0, cDoc) / (cDocXSum + gammaLen);
double wordPerTopicLikelihood = term1 * term2 * term3;
wordLogLikelihood += wordPerTopicLikelihood;
}
double wordPerTopicLikelihood = childLocalWordByTopicProb(wid, cDoc) * childXInDocProb(1, cDoc) / (cDocXSum + gammaLen);
wordLogLikelihood += wordPerTopicLikelihood;
if (Math.abs(wordLogLikelihood) < 1e-10) {
System.out.println("wordLoglikelihood\t" + wordLogLikelihood);
wordLogLikelihood += 1e-10;
}
wordLogLikelihood = Math.log(wordLogLikelihood);
docLogLikelihood += wordLogLikelihood;
}
return docLogLikelihood;
}
use of structures._Word in project IR_Base by Linda-sunshine.
the class ACCTM_CZLR method xProb4Word.
public double xProb4Word(int xid, _Word w, _ChildDoc cDoc) {
double result = 0;
_ParentDoc pDoc = cDoc.m_parentDoc;
double temp1 = pDoc.m_featureWeight.length;
double temp2 = w.getFeatures().length;
result = Utils.dotProduct(pDoc.m_featureWeight, w.getFeatures());
if (xid == 1)
result = 1 / (1 + Math.exp(-result));
else
result = 1 / (1 + Math.exp(result));
return result;
}
use of structures._Word in project IR_Base by Linda-sunshine.
the class ACCTM_CZLR method sampleInChildDoc.
@Override
public void sampleInChildDoc(_Doc d) {
_ChildDoc4BaseWithPhi cDoc = (_ChildDoc4BaseWithPhi) d;
int wid, tid, xid;
double normalizedProb;
for (_Word w : cDoc.getWords()) {
wid = w.getIndex();
tid = w.getTopic();
xid = w.getX();
if (xid == 0) {
cDoc.m_xTopicSstat[xid][tid]--;
cDoc.m_xSstat[xid]--;
cDoc.m_wordXStat.put(wid, cDoc.m_wordXStat.get(wid) - 1);
if (m_collectCorpusStats) {
word_topic_sstat[tid][wid]--;
m_sstat[tid]--;
}
} else if (xid == 1) {
cDoc.m_xTopicSstat[xid][wid]--;
cDoc.m_xSstat[xid]--;
cDoc.m_childWordSstat--;
}
normalizedProb = 0;
double pLambdaZero = xProb4Word(0, w, cDoc);
double pLambdaOne = xProb4Word(1, w, cDoc);
for (tid = 0; tid < number_of_topics; tid++) {
double pWordTopic = childWordByTopicProb(tid, wid);
double pTopic = childTopicInDocProb(tid, cDoc);
m_topicProbCache[tid] = pWordTopic * pTopic * pLambdaZero;
normalizedProb += m_topicProbCache[tid];
}
double pWordTopic = childLocalWordByTopicProb(wid, cDoc);
m_topicProbCache[tid] = pWordTopic * pLambdaOne;
normalizedProb += m_topicProbCache[tid];
normalizedProb *= m_rand.nextDouble();
for (tid = 0; tid < m_topicProbCache.length; tid++) {
normalizedProb -= m_topicProbCache[tid];
if (normalizedProb <= 0)
break;
}
if (tid == m_topicProbCache.length)
tid--;
if (tid < number_of_topics) {
xid = 0;
w.setX(xid);
w.setTopic(tid);
cDoc.m_xTopicSstat[xid][tid]++;
cDoc.m_xSstat[xid]++;
if (cDoc.m_wordXStat.containsKey(wid)) {
cDoc.m_wordXStat.put(wid, cDoc.m_wordXStat.get(wid) + 1);
} else {
cDoc.m_wordXStat.put(wid, 1);
}
if (m_collectCorpusStats) {
word_topic_sstat[tid][wid]++;
m_sstat[tid]++;
}
} else if (tid == (number_of_topics)) {
xid = 1;
w.setX(xid);
w.setTopic(tid);
cDoc.m_xTopicSstat[xid][wid]++;
cDoc.m_xSstat[xid]++;
cDoc.m_childWordSstat++;
}
}
}
Aggregations