Search in sources :

Example 41 with structures._Word

use of structures._Word in project IR_Base by Linda-sunshine.

the class LDA_Gibbs_test method calculate_log_likelihood.

protected double calculate_log_likelihood(_Doc d) {
    int wid = 0;
    double docLogLikelihood = 0;
    double docTopicSum = Utils.sumOfArray(d.m_sstat);
    for (_Word w : d.getWords()) {
        wid = w.getIndex();
        double wordLogLikelihood = 0;
        for (int k = 0; k < number_of_topics; k++) {
            wordLogLikelihood += wordByTopicProb(k, wid) * topicInDocProb(k, d) / (docTopicSum + number_of_topics * d_alpha);
        }
        wordLogLikelihood = Math.log(wordLogLikelihood);
        docLogLikelihood += wordLogLikelihood;
    }
    return docLogLikelihood;
}
Also used : structures._Word(structures._Word)

Example 42 with structures._Word

use of structures._Word in project IR_Base by Linda-sunshine.

the class ACCTM method sampleInChildDoc.

protected void sampleInChildDoc(_Doc d) {
    _ChildDoc cDoc = (_ChildDoc) d;
    int wid, tid;
    double normalizedProb;
    for (_Word w : cDoc.getWords()) {
        wid = w.getIndex();
        tid = w.getTopic();
        cDoc.m_sstat[tid]--;
        if (m_collectCorpusStats) {
            word_topic_sstat[tid][wid]--;
            m_sstat[tid]--;
        }
        normalizedProb = 0;
        for (tid = 0; tid < number_of_topics; tid++) {
            double pWordTopic = childWordByTopicProb(tid, wid);
            double pTopicCDoc = childTopicInDocProb(tid, cDoc);
            m_topicProbCache[tid] = pWordTopic * pTopicCDoc;
            normalizedProb += m_topicProbCache[tid];
        }
        normalizedProb *= m_rand.nextDouble();
        for (tid = 0; tid < number_of_topics; tid++) {
            normalizedProb -= m_topicProbCache[tid];
            if (normalizedProb <= 0)
                break;
        }
        if (tid == number_of_topics)
            tid--;
        w.setTopic(tid);
        cDoc.m_sstat[tid]++;
        if (m_collectCorpusStats) {
            word_topic_sstat[tid][wid]++;
            m_sstat[tid]++;
        }
    }
}
Also used : structures._ChildDoc(structures._ChildDoc) structures._Word(structures._Word)

Example 43 with structures._Word

use of structures._Word in project IR_Base by Linda-sunshine.

the class ACCTM_C method cal_logLikelihood_partial4Child.

@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 = childTopicInDocProb(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;
}
Also used : structures._ChildDoc4BaseWithPhi(structures._ChildDoc4BaseWithPhi) structures._Word(structures._Word)

Example 44 with structures._Word

use of structures._Word in project IR_Base by Linda-sunshine.

the class ACCTM_CHard method initialize_probability.

@Override
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.setTopic(number_of_topics);
        } else if (d instanceof _ChildDoc4BaseWithPhi) {
            ((_ChildDoc4BaseWithPhi_Hard) d).createXSpace(number_of_topics, m_gamma.length, vocabulary_size, d_beta);
            ((_ChildDoc4BaseWithPhi_Hard) 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;
}
Also used : structures._ChildDoc4BaseWithPhi(structures._ChildDoc4BaseWithPhi) structures._Stn(structures._Stn) structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc(structures._ParentDoc) structures._Word(structures._Word)

Example 45 with structures._Word

use of structures._Word in project IR_Base by Linda-sunshine.

the class ACCTM_CHard method sampleInChildDoc.

@Override
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]--;
            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--;
        }
        _ParentDoc pDocObj = cDoc.m_parentDoc;
        if (Utils.indexOf(pDocObj.getSparse(), wid) != -1) {
            normalizedProb = 0;
            for (tid = 0; tid < number_of_topics; tid++) {
                double pWordTopic = childWordByTopicProb(tid, wid);
                double pTopic = childTopicInDocProb(tid, cDoc);
                m_topicProbCache[tid] = pWordTopic * pTopic;
                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 (m_collectCorpusStats) {
                    word_topic_sstat[tid][wid]++;
                    m_sstat[tid]++;
                }
            } else if (tid == (number_of_topics)) {
                System.out.println("error on hard differentiate");
            }
        } else {
            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 (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++;
            }
        }
    }
}
Also used : structures._ChildDoc4BaseWithPhi(structures._ChildDoc4BaseWithPhi) structures._ParentDoc(structures._ParentDoc) structures._Word(structures._Word)

Aggregations

structures._Word (structures._Word)69 structures._ChildDoc (structures._ChildDoc)18 File (java.io.File)16 FileNotFoundException (java.io.FileNotFoundException)15 PrintWriter (java.io.PrintWriter)15 structures._ParentDoc (structures._ParentDoc)14 structures._Doc (structures._Doc)12 structures._Stn (structures._Stn)11 structures._ParentDoc4DCM (structures._ParentDoc4DCM)10 structures._ChildDoc4BaseWithPhi (structures._ChildDoc4BaseWithPhi)9 HashMap (java.util.HashMap)5 structures._Doc4DCMLDA (structures._Doc4DCMLDA)4 structures._Doc4SparseDCMLDA (structures._Doc4SparseDCMLDA)4 structures._SparseFeature (structures._SparseFeature)3 Feature (Classifier.supervised.liblinear.Feature)1 FeatureNode (Classifier.supervised.liblinear.FeatureNode)1 Model (Classifier.supervised.liblinear.Model)1 Parameter (Classifier.supervised.liblinear.Parameter)1 Problem (Classifier.supervised.liblinear.Problem)1 SolverType (Classifier.supervised.liblinear.SolverType)1