Search in sources :

Example 16 with structures._ParentDoc4DCM

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);
}
Also used : structures._Doc(structures._Doc) structures._ParentDoc4DCM(structures._ParentDoc4DCM)

Example 17 with structures._ParentDoc4DCM

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

Example 18 with structures._ParentDoc4DCM

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;
}
Also used : structures._ParentDoc4DCM(structures._ParentDoc4DCM) structures._Word(structures._Word)

Example 19 with structures._ParentDoc4DCM

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];
    }
}
Also used : structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc(structures._ParentDoc) structures._ParentDoc4DCM(structures._ParentDoc4DCM)

Example 20 with structures._ParentDoc4DCM

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

Aggregations

structures._ParentDoc4DCM (structures._ParentDoc4DCM)28 structures._ChildDoc (structures._ChildDoc)23 structures._Doc (structures._Doc)14 structures._Word (structures._Word)13 File (java.io.File)7 structures._Stn (structures._Stn)6 FileNotFoundException (java.io.FileNotFoundException)5 PrintWriter (java.io.PrintWriter)5 structures._SparseFeature (structures._SparseFeature)5 structures._ParentDoc (structures._ParentDoc)4 MyPriorityQueue (structures.MyPriorityQueue)3 structures._RankItem (structures._RankItem)3 HashMap (java.util.HashMap)2 LBFGS (LBFGS.LBFGS)1 JSONArray (json.JSONArray)1 JSONException (json.JSONException)1 JSONObject (json.JSONObject)1