Search in sources :

Example 36 with structures._ChildDoc

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

the class ACCTM_test method printMu.

protected void printMu(String childMuFile) {
    System.out.println("print mu");
    try {
        PrintWriter muPW = new PrintWriter(new File(childMuFile));
        for (_Doc d : m_trainSet) {
            if (d instanceof _ChildDoc) {
                muPW.println(d.getName() + "\t" + ((_ChildDoc) d).getMu());
            }
        }
        muPW.flush();
        muPW.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter)

Example 37 with structures._ChildDoc

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

the class ACCTM_test method debugOutput.

public void debugOutput(int topK, String filePrefix) {
    File parentTopicFolder = new File(filePrefix + "parentTopicAssignment");
    File childTopicFolder = new File(filePrefix + "childTopicAssignment");
    if (!parentTopicFolder.exists()) {
        System.out.println("creating directory" + parentTopicFolder);
        parentTopicFolder.mkdir();
    }
    if (!childTopicFolder.exists()) {
        System.out.println("creating directory" + childTopicFolder);
        childTopicFolder.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((_ParentDoc) d, parentTopicFolder);
            printParentPhi((_ParentDoc) d, parentPhiFolder);
        } else if (d instanceof _ChildDoc) {
            printChildTopicAssignment(d, childTopicFolder);
        }
    }
    String parentParameterFile = filePrefix + "parentParameter.txt";
    String childParameterFile = filePrefix + "childParameter.txt";
    printParameter(parentParameterFile, childParameterFile, m_trainSet);
    String similarityFile = filePrefix + "topicSimilarity.txt";
    printEntropy(filePrefix);
    printTopKChild4Stn(filePrefix, topK);
    String childMuFile = filePrefix + "childMu.txt";
    printMu(childMuFile);
}
Also used : structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc(structures._ParentDoc) File(java.io.File)

Example 38 with structures._ChildDoc

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

the class DCMCorrLDA method initialAlphaBeta.

protected void initialAlphaBeta() {
    double parentDocNum = 0;
    double childDocNum = 0;
    Arrays.fill(m_sstat, 0);
    Arrays.fill(m_alphaAuxilary, 0);
    for (int k = 0; k < number_of_topics; k++) {
        Arrays.fill(topic_term_probabilty[k], 0);
        Arrays.fill(word_topic_sstat[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_stat[k][v] / pDoc.m_topic_stat[k];
                    topic_term_probabilty[k][v] += tempProb;
                }
            }
            parentDocNum += 1;
            for (_ChildDoc cDoc : pDoc.m_childDocs) {
                for (int k = 0; k < number_of_topics; k++) {
                    double tempProb = cDoc.m_sstat[k] / cDoc.getTotalDocLength();
                    m_alphaAuxilary[k] += tempProb;
                }
                childDocNum += 1;
            }
        }
    }
    for (int k = 0; k < number_of_topics; k++) {
        m_sstat[k] /= parentDocNum;
        m_alphaAuxilary[k] /= childDocNum;
        for (int v = 0; v < vocabulary_size; v++) {
            topic_term_probabilty[k][v] /= (parentDocNum + childDocNum);
        }
    }
    for (int k = 0; k < number_of_topics; k++) {
        m_alpha[k] = m_sstat[k];
        m_alpha_c[k] = m_alphaAuxilary[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);
    m_totalAlpha_c = Utils.sumOfArray(m_alpha_c);
    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 39 with structures._ChildDoc

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

the class DCMCorrLDA method calculate_log_likelihood.

protected double calculate_log_likelihood(_ParentDoc4DCM d) {
    double docLogLikelihood = 0;
    int docID = d.getID();
    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) {
        double muDp = cDoc.getMu() / parentDocLength;
        docLogLikelihood += Utils.digamma(m_totalAlpha_c + cDoc.getMu());
        docLogLikelihood += Utils.digamma(m_totalAlpha_c + cDoc.getMu() + cDoc.getTotalDocLength());
        for (int k = 0; k < number_of_topics; k++) {
            double term = Utils.digamma(m_alpha_c[k] + muDp * d.m_sstat[k] + cDoc.m_sstat[k]);
            term -= Utils.digamma(m_alpha_c[k] + muDp * d.m_sstat[k]);
            docLogLikelihood += term;
        }
    }
    return docLogLikelihood;
}
Also used : structures._ChildDoc(structures._ChildDoc)

Example 40 with structures._ChildDoc

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

the class DCMCorrLDA method initialize_probability.

protected void initialize_probability(Collection<_Doc> collection) {
    m_alpha_c = new double[number_of_topics];
    m_alphaAuxilary = new double[number_of_topics];
    m_alpha = new double[number_of_topics];
    m_beta = new double[number_of_topics][vocabulary_size];
    m_totalAlpha = 0;
    m_totalAlpha_c = 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 (_Stn stnObj : d.getSentences()) {
                stnObj.setTopicsVct(number_of_topics);
            }
            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]++;
                }
                computeMu4Doc(cDoc);
            }
        }
    }
    initialAlphaBeta();
    imposePrior();
}
Also used : structures._Stn(structures._Stn) structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) 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