Search in sources :

Example 71 with structures._ChildDoc

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

the class DCMLDA4AC method initialize_probability.

protected void initialize_probability(Collection<_Doc> collection) {
    m_alpha = new double[number_of_topics];
    m_beta = new double[number_of_topics][vocabulary_size];
    m_totalAlpha = 0;
    m_totalBeta = new double[number_of_topics];
    m_topic_word_prob = new double[number_of_topics][vocabulary_size];
    for (_Doc d : collection) {
        if (d instanceof _ParentDoc4DCM) {
            _ParentDoc4DCM pDoc = (_ParentDoc4DCM) d;
            pDoc.setTopics4Gibbs(number_of_topics, 0, vocabulary_size);
            for (_Word w : pDoc.getWords()) {
                int wid = w.getIndex();
                int tid = w.getTopic();
                word_topic_sstat[tid][wid]++;
            }
            for (_ChildDoc cDoc : pDoc.m_childDocs) {
                cDoc.setTopics4Gibbs_LDA(number_of_topics, 0);
                for (_Word w : cDoc.getWords()) {
                    int wid = w.getIndex();
                    int tid = w.getTopic();
                    pDoc.m_wordTopic_stat[tid][wid]++;
                    pDoc.m_topic_stat[tid]++;
                    word_topic_sstat[tid][wid]++;
                }
            }
        }
    }
    initialAlphaBeta();
    imposePrior();
}
Also used : structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc4DCM(structures._ParentDoc4DCM) structures._Word(structures._Word)

Example 72 with structures._ChildDoc

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

the class DCMLDA4AC method calculate_log_likelihood.

protected double calculate_log_likelihood(_ParentDoc4DCM d) {
    double docLogLikelihood = 0.0;
    double parentDocLength = d.getTotalDocLength();
    for (int k = 0; k < number_of_topics; k++) {
        double term = Utils.lgamma(d.m_sstat[k] + m_alpha[k]);
        docLogLikelihood += term;
        term = Utils.lgamma(m_alpha[k]);
        docLogLikelihood -= term;
    }
    docLogLikelihood += Utils.lgamma(m_totalAlpha);
    docLogLikelihood -= Utils.lgamma(parentDocLength + m_totalAlpha);
    for (int k = 0; k < number_of_topics; k++) {
        for (int v = 0; v < vocabulary_size; v++) {
            double term = Utils.lgamma(d.m_wordTopic_stat[k][v] + m_beta[k][v]);
            docLogLikelihood += term;
            term = Utils.lgamma(m_beta[k][v]);
            docLogLikelihood -= term;
        }
        docLogLikelihood += Utils.lgamma(m_totalBeta[k]);
        docLogLikelihood -= Utils.lgamma(d.m_topic_stat[k] + m_totalBeta[k]);
    }
    for (_ChildDoc cDoc : d.m_childDocs) {
        int cDocLength = cDoc.getTotalDocLength();
        for (int k = 0; k < number_of_topics; k++) {
            double term = Utils.lgamma(cDoc.m_sstat[k] + m_alpha[k]);
            docLogLikelihood += term;
            term = Utils.lgamma(m_alpha[k]);
            docLogLikelihood -= term;
        }
        docLogLikelihood += Utils.lgamma(m_totalAlpha);
        docLogLikelihood -= Utils.lgamma(cDocLength + m_totalAlpha);
    }
    return docLogLikelihood;
}
Also used : structures._ChildDoc(structures._ChildDoc)

Example 73 with structures._ChildDoc

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

the class DCMLDA4AC method cal_logLikelihood_partial4Child.

protected double cal_logLikelihood_partial4Child(_ChildDoc d) {
    double likelihood = 0;
    _ParentDoc4DCM pDoc = (_ParentDoc4DCM) d.m_parentDoc;
    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] * pDoc.m_wordTopic_prob[k][wid];
        }
        likelihood += Math.log(wordLikelihood);
    }
    return likelihood;
}
Also used : structures._ParentDoc4DCM(structures._ParentDoc4DCM) structures._Word(structures._Word)

Example 74 with structures._ChildDoc

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

the class DCMLDA4AC method initialAlphaBeta.

protected void initialAlphaBeta() {
    Arrays.fill(m_sstat, 0);
    for (int k = 0; k < number_of_topics; k++) {
        Arrays.fill(topic_term_probabilty[k], 0);
    }
    for (_Doc d : m_trainSet) {
        if (d instanceof _ParentDoc4DCM) {
            _ParentDoc4DCM pDoc = (_ParentDoc4DCM) d;
            for (int k = 0; k < number_of_topics; k++) {
                double tempProb = pDoc.m_sstat[k] / pDoc.getTotalDocLength();
                m_sstat[k] += tempProb;
                if (pDoc.m_sstat[k] == 0)
                    continue;
                for (int v = 0; v < vocabulary_size; v++) {
                    tempProb = pDoc.m_wordTopic_prob[k][v] / pDoc.m_sstat[k];
                    topic_term_probabilty[k][v] += tempProb;
                }
            }
            for (_ChildDoc cDoc : pDoc.m_childDocs) {
                for (int k = 0; k < number_of_topics; k++) {
                    double tempProb = cDoc.m_sstat[k] / cDoc.getTotalDocLength();
                    m_sstat[k] += tempProb;
                }
            }
        }
    }
    int trainSetSize = m_trainSet.size();
    for (int k = 0; k < number_of_topics; k++) {
        m_sstat[k] /= trainSetSize;
        for (int v = 0; v < vocabulary_size; v++) {
            topic_term_probabilty[k][v] /= trainSetSize;
        }
    }
    for (int k = 0; k < number_of_topics; k++) {
        m_alpha[k] = m_sstat[k];
        for (int v = 0; v < vocabulary_size; v++) {
            m_beta[k][v] = topic_term_probabilty[k][v] + d_beta;
        }
    }
    m_totalAlpha = Utils.sumOfArray(m_alpha);
    for (int k = 0; k < number_of_topics; k++) {
        m_totalBeta[k] = Utils.sumOfArray(m_beta[k]);
    }
}
Also used : structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc4DCM(structures._ParentDoc4DCM)

Example 75 with structures._ChildDoc

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

the class DCMLDA4AC method initTestDoc.

public void initTestDoc(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 : d.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();
    }
}
Also used : structures._Stn(structures._Stn) structures._ChildDoc(structures._ChildDoc) structures._ParentDoc4DCM(structures._ParentDoc4DCM) structures._Word(structures._Word)

Aggregations

structures._ChildDoc (structures._ChildDoc)77 structures._ParentDoc (structures._ParentDoc)47 structures._Doc (structures._Doc)35 structures._Stn (structures._Stn)25 structures._Word (structures._Word)22 File (java.io.File)18 structures._ParentDoc4DCM (structures._ParentDoc4DCM)16 structures._SparseFeature (structures._SparseFeature)16 HashMap (java.util.HashMap)14 PrintWriter (java.io.PrintWriter)12 FileNotFoundException (java.io.FileNotFoundException)11 structures._ChildDoc4BaseWithPhi (structures._ChildDoc4BaseWithPhi)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)2 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