Search in sources :

Example 16 with structures._ChildDoc

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

the class corrLDA_Gibbs method initTest.

protected void initTest(ArrayList<_Doc> sampleTestSet, _Doc d) {
    _ParentDoc pDoc = (_ParentDoc) d;
    for (_Stn stnObj : pDoc.getSentences()) {
        stnObj.setTopicsVct(number_of_topics);
    }
    int testLength = (int) (m_testWord4PerplexityProportion * pDoc.getTotalDocLength());
    pDoc.setTopics4GibbsTest(number_of_topics, 0, testLength);
    sampleTestSet.add(pDoc);
    for (_ChildDoc cDoc : pDoc.m_childDocs) {
        testLength = (int) (m_testWord4PerplexityProportion * cDoc.getTotalDocLength());
        cDoc.setTopics4GibbsTest(number_of_topics, 0, testLength);
        sampleTestSet.add(cDoc);
    }
}
Also used : structures._Stn(structures._Stn) structures._ChildDoc(structures._ChildDoc) structures._ParentDoc(structures._ParentDoc)

Example 17 with structures._ChildDoc

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

the class weightedCorrespondenceModel method updateZeta4Child.

public void updateZeta4Child(_ParentDoc4DCM pDoc) {
    double totalGamma4Parent = Utils.sumOfArray(pDoc.m_sstat);
    for (_ChildDoc cDoc : pDoc.m_childDocs) {
        double totalPi4Child = Utils.sumOfArray(cDoc.m_sstat);
        double gammaPiInnerProd = Utils.dotProduct(pDoc.m_sstat, cDoc.m_sstat);
        cDoc.m_zeta = gammaPiInnerProd / (totalGamma4Parent * totalPi4Child);
    }
}
Also used : structures._ChildDoc(structures._ChildDoc)

Example 18 with structures._ChildDoc

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

the class weightedCorrespondenceModel method gammaFuncGradientVal.

public double gammaFuncGradientVal(_ParentDoc4DCM pDoc, double[] gamma, double[] gammaGradient) {
    Arrays.fill(gammaGradient, 0);
    double funcVal = 0;
    double[] expGamma = new double[number_of_topics];
    double expGammaSum = 0;
    for (int k = 0; k < number_of_topics; k++) {
        expGamma[k] = Math.exp(gamma[k]);
        expGammaSum += expGamma[k];
    }
    funcVal -= Utils.lgamma(expGammaSum);
    double constantGradient = 0;
    for (int k = 0; k < number_of_topics; k++) {
        constantGradient += (m_alpha[k] - expGamma[k]) * Utils.trigamma(expGammaSum);
    }
    for (int k = 0; k < number_of_topics; k++) {
        gammaGradient[k] = (m_alpha[k] - expGamma[k]) * Utils.trigamma(expGamma[k]);
        funcVal += (m_alpha[k] - expGamma[k]) * (Utils.digamma(expGamma[k]) - Utils.digamma(expGammaSum));
        funcVal += Utils.lgamma(expGamma[k]);
    }
    _SparseFeature[] fvs = pDoc.getSparse();
    for (int n = 0; n < fvs.length; n++) {
        int wID = fvs[n].getIndex();
        double wVal = fvs[n].getValue();
        for (int k = 0; k < number_of_topics; k++) {
            funcVal += pDoc.m_phi[n][k] * wVal * (Utils.digamma(expGamma[k]) - Utils.digamma(expGammaSum));
            gammaGradient[k] += pDoc.m_phi[n][k] * wVal * Utils.trigamma(expGamma[k]);
            constantGradient += pDoc.m_phi[n][k] * wVal * Utils.trigamma(expGammaSum);
        // gammaGradient[k] -= pDoc.m_phi[n][k]*wVal*Utils.trigamma(expGammaSum);
        }
    }
    for (_ChildDoc cDoc : pDoc.m_childDocs) {
        double piSum = Utils.sumOfArray(cDoc.m_sstat);
        _SparseFeature[] cDocFvs = cDoc.getSparse();
        for (int n = 0; n < cDocFvs.length; n++) {
            int wID = cDocFvs[n].getIndex();
            double wVal = cDocFvs[n].getValue();
            for (int k = 0; k < number_of_topics; k++) {
                funcVal += cDoc.m_phi[n][k] * wVal * (Utils.digamma(expGamma[k]) - Utils.digamma(expGammaSum));
                funcVal -= cDoc.m_phi[n][k] * wVal * (Utils.dotProduct(cDoc.m_sstat, expGamma)) / (piSum * expGammaSum * cDoc.m_zeta);
                gammaGradient[k] += cDoc.m_phi[n][k] * wVal * Utils.trigamma(expGamma[k]);
                constantGradient += cDoc.m_phi[n][k] * wVal * Utils.trigamma(expGammaSum);
                // gammaGradient[k] -= cDoc.m_phi[n][k]*wVal*Utils.trigamma(expGammaSum);
                double temp = cDoc.m_sstat[k] * expGammaSum - Utils.dotProduct(cDoc.m_sstat, expGamma);
                gammaGradient[k] -= cDoc.m_phi[n][k] * wVal * temp / (piSum * expGammaSum * expGammaSum * cDoc.m_zeta);
            }
        }
    }
    for (int k = 0; k < number_of_topics; k++) {
        gammaGradient[k] -= constantGradient;
        gammaGradient[k] *= expGamma[k];
        gammaGradient[k] = 0 - gammaGradient[k];
    }
    return -funcVal;
}
Also used : structures._ChildDoc(structures._ChildDoc) structures._SparseFeature(structures._SparseFeature)

Example 19 with structures._ChildDoc

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

the class weightedCorrespondenceModel method calculate_E_step.

@Override
public double calculate_E_step(_Doc d) {
    if (d instanceof _ChildDoc)
        return 0;
    _ParentDoc4DCM pDoc = (_ParentDoc4DCM) d;
    double last = 1;
    if (m_varConverge > 0)
        last = calculate_log_likelihood(pDoc);
    double current = last, converge, logSum, wVal;
    int iter = 0, wID;
    do {
        updateEta4Parent(pDoc);
        updateGamma4Parent(pDoc);
        // updateLambda(pDoc);
        updatePi4Child(pDoc);
        updateZeta4Child(pDoc);
        updateEta4Child(pDoc);
        updateLambda(pDoc);
        if (m_varConverge > 0) {
            current = calculate_log_likelihood(pDoc);
            converge = Math.abs((current - last) / last);
            last = current;
            if (converge < m_varConverge)
                break;
        }
    } while (++iter < m_varMaxIter);
    collectStats(pDoc);
    return current;
}
Also used : structures._ChildDoc(structures._ChildDoc) structures._ParentDoc4DCM(structures._ParentDoc4DCM)

Example 20 with structures._ChildDoc

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

the class weightedCorrespondenceModel method updateLambda.

public void updateLambda(_ParentDoc4DCM pDoc) {
    _SparseFeature[] fvs = pDoc.getSparse();
    int totalWord = 0;
    double totalLambda = 0;
    for (int k = 0; k < number_of_topics; k++) for (int v = 0; v < vocabulary_size; v++) pDoc.m_lambda_stat[k][v] = m_beta[k][v];
    for (int n = 0; n < fvs.length; n++) {
        int wID = fvs[n].getIndex();
        double wVal = fvs[n].getValue();
        totalWord += wVal;
        double phiSum = Utils.sumOfArray(pDoc.m_phi[n]);
        if (Math.abs(phiSum - 1) > 1) {
            System.out.println("inequal to 1\t" + n + phiSum);
        }
        for (int k = 0; k < number_of_topics; k++) {
            pDoc.m_lambda_stat[k][wID] += wVal * pDoc.m_phi[n][k];
            if (Double.isNaN(pDoc.m_lambda_stat[k][wID])) {
                System.out.println("nan error article\t" + n + " " + wID);
            }
        }
    }
    for (_ChildDoc cDoc : pDoc.m_childDocs) {
        _SparseFeature[] cFvs = cDoc.getSparse();
        for (int n = 0; n < cFvs.length; n++) {
            int wID = cFvs[n].getIndex();
            double wVal = cFvs[n].getValue();
            totalWord += wVal;
            double phiSum = Utils.sumOfArray(cDoc.m_phi[n]);
            if (Math.abs(phiSum - 1) > 1) {
                System.out.println("inequal to 1\t" + n + phiSum);
                for (int k = 0; k < number_of_topics; k++) {
                    System.out.println("\t\t" + cDoc.m_phi[n][k]);
                }
            }
            for (int k = 0; k < number_of_topics; k++) {
                pDoc.m_lambda_stat[k][wID] += wVal * cDoc.m_phi[n][k];
                if (Double.isNaN(pDoc.m_lambda_stat[k][wID])) {
                    System.out.println("nan error comment\t" + n + " " + wID);
                }
            }
        }
    }
    for (int k = 0; k < number_of_topics; k++) {
        pDoc.m_lambda_topicStat[k] = Utils.sumOfArray(pDoc.m_lambda_stat[k]);
        totalLambda += pDoc.m_lambda_topicStat[k];
    }
// System.out.println("total Words in this doc\t"+pDoc.getName()+"\t"+totalWord+"\t"+totalLambda);
}
Also used : structures._ChildDoc(structures._ChildDoc) structures._SparseFeature(structures._SparseFeature)

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