Search in sources :

Example 6 with structures._ChildDoc4BaseWithPhi

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

Example 7 with structures._ChildDoc4BaseWithPhi

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

Example 8 with structures._ChildDoc4BaseWithPhi

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

the class ACCTM_CZLR method initTest4Spam.

public void initTest4Spam(ArrayList<_Doc> sampleTestSet, _Doc d) {
    _ParentDoc pDoc = (_ParentDoc) d;
    pDoc.setTopics4Gibbs(number_of_topics, 0);
    for (_Stn stnObj : pDoc.getSentences()) {
        stnObj.setTopicsVct(number_of_topics);
    }
    sampleTestSet.add(pDoc);
    for (_ChildDoc cDoc : pDoc.m_childDocs) {
        ((_ChildDoc4BaseWithPhi) cDoc).createXSpace(number_of_topics, m_gamma.length, vocabulary_size, d_beta);
        ((_ChildDoc4BaseWithPhi) cDoc).setTopics4Gibbs(number_of_topics, 0);
        sampleTestSet.add(cDoc);
        cDoc.setParentDoc(pDoc);
        computeMu4Doc(cDoc);
    }
    setFeatures4Word(sampleTestSet);
}
Also used : structures._ChildDoc4BaseWithPhi(structures._ChildDoc4BaseWithPhi) structures._Stn(structures._Stn) structures._ChildDoc(structures._ChildDoc) structures._ParentDoc(structures._ParentDoc)

Example 9 with structures._ChildDoc4BaseWithPhi

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

the class ACCTM_CZLR method calculate_log_likelihood4Child.

@Override
protected double calculate_log_likelihood4Child(_Doc d) {
    _ChildDoc4BaseWithPhi cDoc = (_ChildDoc4BaseWithPhi) d;
    double docLogLikelihood = 0;
    for (_Word w : d.getWords()) {
        int wid = w.getIndex();
        double wordLogLikelihood = 0;
        for (int k = 0; k < number_of_topics; k++) {
            double wordPerTopicLikelihood = childWordByTopicProb(k, wid) * childTopicInDocProb(k, cDoc) * xProb4Word(0, w, cDoc);
            wordLogLikelihood += wordPerTopicLikelihood;
        }
        double wordPerTopicLikelihood = childLocalWordByTopicProb(wid, cDoc) * xProb4Word(1, w, cDoc);
        wordLogLikelihood += wordPerTopicLikelihood;
        if (Math.abs(wordLogLikelihood) < 1e-10) {
            wordLogLikelihood += 1e-10;
        }
        wordLogLikelihood = Math.log(wordLogLikelihood);
        docLogLikelihood += wordLogLikelihood;
    }
    return docLogLikelihood;
}
Also used : structures._ChildDoc4BaseWithPhi(structures._ChildDoc4BaseWithPhi) structures._Word(structures._Word)

Example 10 with structures._ChildDoc4BaseWithPhi

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

the class ACCTM_C_test method debugOutput.

public void debugOutput(int topK, String filePrefix) {
    File parentTopicFolder = new File(filePrefix + "parentTopicAssignment");
    File childTopicFolder = new File(filePrefix + "childTopicAssignment");
    File childLocalWordTopicFolder = new File(filePrefix + "childLocalTopic");
    if (!parentTopicFolder.exists()) {
        System.out.println("creating directory" + parentTopicFolder);
        parentTopicFolder.mkdir();
    }
    if (!childTopicFolder.exists()) {
        System.out.println("creating directory" + childTopicFolder);
        childTopicFolder.mkdir();
    }
    if (!childLocalWordTopicFolder.exists()) {
        System.out.println("creating directory" + childLocalWordTopicFolder);
        childLocalWordTopicFolder.mkdir();
    }
    File parentPhiFolder = new File(filePrefix + "parentPhi");
    File childPhiFolder = new File(filePrefix + "childPhi");
    if (!parentPhiFolder.exists()) {
        System.out.println("creating directory" + parentPhiFolder);
        parentPhiFolder.mkdir();
    }
    if (!childPhiFolder.exists()) {
        System.out.println("creating directory" + childPhiFolder);
        childPhiFolder.mkdir();
    }
    File childXFolder = new File(filePrefix + "xValue");
    if (!childXFolder.exists()) {
        System.out.println("creating x Value directory" + childXFolder);
        childXFolder.mkdir();
    }
    for (_Doc d : m_trainSet) {
        if (d instanceof _ParentDoc) {
            printParentTopicAssignment(d, parentTopicFolder);
            printParentPhi(d, parentPhiFolder);
        } else if (d instanceof _ChildDoc) {
            printChildTopicAssignment(d, childTopicFolder);
            printChildLocalWordTopicDistribution((_ChildDoc4BaseWithPhi) d, childLocalWordTopicFolder);
            printXValue(d, childXFolder);
        }
    }
    String parentParameterFile = filePrefix + "parentParameter.txt";
    String childParameterFile = filePrefix + "childParameter.txt";
    printParameter(parentParameterFile, childParameterFile, m_trainSet);
    String xProportionFile = filePrefix + "childXProportion.txt";
    printXProportion(xProportionFile, m_trainSet);
    String similarityFile = filePrefix + "topicSimilarity.txt";
    printEntropy(filePrefix);
    int topKStn = 10;
    int topKChild = 10;
    printTopKChild4Stn(filePrefix, topKChild);
}
Also used : structures._ChildDoc4BaseWithPhi(structures._ChildDoc4BaseWithPhi) structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc(structures._ParentDoc) File(java.io.File)

Aggregations

structures._ChildDoc4BaseWithPhi (structures._ChildDoc4BaseWithPhi)15 structures._Word (structures._Word)10 structures._ParentDoc (structures._ParentDoc)9 structures._ChildDoc (structures._ChildDoc)7 structures._Doc (structures._Doc)4 structures._Stn (structures._Stn)4 File (java.io.File)2 structures._SparseFeature (structures._SparseFeature)2 FileNotFoundException (java.io.FileNotFoundException)1 PrintWriter (java.io.PrintWriter)1 JSONObject (json.JSONObject)1