Search in sources :

Example 56 with structures._ChildDoc

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

the class ACCTM_CHard method initTest.

@Override
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 * d.getTotalDocLength());
    testLength = 0;
    pDoc.setTopics4GibbsTest(number_of_topics, 0, testLength);
    sampleTestSet.add(pDoc);
    pDoc.createSparseVct4Infer();
    for (_ChildDoc cDoc : pDoc.m_childDocs) {
        testLength = (int) (m_testWord4PerplexityProportion * cDoc.getTotalDocLength());
        ((_ChildDoc4BaseWithPhi_Hard) cDoc).createXSpace(number_of_topics, m_gamma.length, vocabulary_size, d_beta);
        ((_ChildDoc4BaseWithPhi_Hard) cDoc).setTopics4GibbsTest(number_of_topics, 0, testLength);
        sampleTestSet.add(cDoc);
        cDoc.createSparseVct4Infer();
        computeTestMu4Doc(cDoc);
    }
}
Also used : structures._ChildDoc4BaseWithPhi_Hard(structures._ChildDoc4BaseWithPhi_Hard) structures._Stn(structures._Stn) structures._ChildDoc(structures._ChildDoc) structures._ParentDoc(structures._ParentDoc)

Example 57 with structures._ChildDoc

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

the class ACCTM_CZLR method update_M_step.

protected void update_M_step(int iter, File weightFolder) {
    if (m_statisticsNormalized) {
        System.err.println("The statistics collector has been normlaized before, cannot further accumulate the samples!");
        System.exit(-1);
    }
    for (int i = 0; i < this.number_of_topics; i++) {
        for (int v = 0; v < this.vocabulary_size; v++) {
            // collect the current sample
            topic_term_probabilty[i][v] += word_topic_sstat[i][v];
        }
    }
    // used to estimate final theta for each document
    for (_Doc d : m_trainSet) {
        if (d instanceof _ParentDoc)
            collectParentStats((_ParentDoc) d);
        else if (d instanceof _ChildDoc)
            collectChildStats((_ChildDoc) d);
    }
    File weightIterFolder = new File(weightFolder, "_" + iter);
    if (!weightIterFolder.exists()) {
        weightIterFolder.mkdir();
    }
    for (_Doc d : m_trainSet) {
        if (d instanceof _ParentDoc)
            updateFeatureWeight((_ParentDoc) d, iter, weightIterFolder);
    }
}
Also used : structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc(structures._ParentDoc) File(java.io.File)

Example 58 with structures._ChildDoc

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

the class ACCTM_CZLR method updateFeatureWeight.

public void updateFeatureWeight(_ParentDoc pDoc, int iter, File weightIterFolder) {
    int totalChildWordNum = 0;
    int featureLen = 0;
    ArrayList<Double> targetValList = new ArrayList<Double>();
    ArrayList<Feature[]> featureList = new ArrayList<Feature[]>();
    for (_ChildDoc cDoc : pDoc.m_childDocs) {
        for (_Word w : cDoc.getWords()) {
            double[] wordFeatures = w.getFeatures();
            double x = w.getX();
            featureLen = wordFeatures.length;
            Feature[] featureVec = new Feature[featureLen];
            for (int i = 0; i < featureLen; i++) {
                featureVec[i] = new FeatureNode(i + 1, wordFeatures[i]);
            }
            featureList.add(featureVec);
            targetValList.add(x);
        }
    }
    totalChildWordNum = featureList.size();
    double[] targetVal = new double[totalChildWordNum];
    Feature[][] featureMatrix = new Feature[totalChildWordNum][];
    for (int i = 0; i < totalChildWordNum; i++) {
        featureMatrix[i] = featureList.get(i);
    }
    for (int i = 0; i < totalChildWordNum; i++) {
        targetVal[i] = targetValList.get(i);
    }
    Problem problem = new Problem();
    problem.l = totalChildWordNum;
    // featureNum
    problem.n = featureLen + 1;
    problem.x = featureMatrix;
    problem.y = targetVal;
    SolverType solver = SolverType.L2R_LR;
    double C = 1.0;
    double eps = 0.01;
    Parameter param = new Parameter(solver, C, eps);
    Model model = Linear.train(problem, param);
    int featureNum = model.getNrFeature();
    for (int i = 0; i < featureNum; i++) pDoc.m_featureWeight[i] = model.getDecfunCoef(i, 0);
    String weightFile = pDoc.getName() + ".txt";
    File modelFile = new File(weightIterFolder, weightFile);
    try {
        // if((iter>200)&&(iter%100==0))
        model.save(modelFile);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) structures._Word(structures._Word) SolverType(Classifier.supervised.liblinear.SolverType) Feature(Classifier.supervised.liblinear.Feature) structures._SparseFeature(structures._SparseFeature) structures._ChildDoc(structures._ChildDoc) FeatureNode(Classifier.supervised.liblinear.FeatureNode) Model(Classifier.supervised.liblinear.Model) Parameter(Classifier.supervised.liblinear.Parameter) Problem(Classifier.supervised.liblinear.Problem) File(java.io.File)

Example 59 with structures._ChildDoc

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

the class ACCTM_C_test method printParameter.

public void printParameter(String parentParameterFile, String childParameterFile, ArrayList<_Doc> docList) {
    System.out.println("printing parameter");
    try {
        System.out.println(parentParameterFile);
        System.out.println(childParameterFile);
        PrintWriter parentParaOut = new PrintWriter(new File(parentParameterFile));
        PrintWriter childParaOut = new PrintWriter(new File(childParameterFile));
        for (_Doc d : docList) {
            if (d instanceof _ParentDoc) {
                parentParaOut.print(d.getName() + "\t");
                parentParaOut.print("topicProportion\t");
                for (int k = 0; k < number_of_topics; k++) {
                    parentParaOut.print(d.m_topics[k] + "\t");
                }
                for (_Stn stnObj : d.getSentences()) {
                    parentParaOut.print("sentence" + (stnObj.getIndex() + 1) + "\t");
                    for (int k = 0; k < number_of_topics; k++) {
                        parentParaOut.print(stnObj.m_topics[k] + "\t");
                    }
                }
                parentParaOut.println();
                for (_ChildDoc cDoc : ((_ParentDoc) d).m_childDocs) {
                    childParaOut.print(d.getName() + "\t");
                    childParaOut.print(cDoc.getName() + "\t");
                    childParaOut.print("topicProportion\t");
                    for (int k = 0; k < number_of_topics; k++) {
                        childParaOut.print(cDoc.m_xTopics[0][k] + "\t");
                    }
                    childParaOut.print("xProportion\t");
                    for (int x = 0; x < m_gamma.length; x++) {
                        childParaOut.print(cDoc.m_xProportion[x] + "\t");
                    }
                    childParaOut.println();
                }
            }
        }
        parentParaOut.flush();
        parentParaOut.close();
        childParaOut.flush();
        childParaOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : structures._Stn(structures._Stn) structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc(structures._ParentDoc) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter)

Example 60 with structures._ChildDoc

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

the class ACCTM_C_test method printXProportion.

public void printXProportion(String xProportionFile, ArrayList<_Doc> docList) {
    System.out.println("x proportion for parent doc");
    try {
        PrintWriter pw = new PrintWriter(new File(xProportionFile));
        for (_Doc d : docList) {
            if (d instanceof _ParentDoc) {
                for (_ChildDoc doc : ((_ParentDoc) d).m_childDocs) {
                    _ChildDoc4BaseWithPhi cDoc = (_ChildDoc4BaseWithPhi) doc;
                    pw.print(d.getName() + "\t");
                    pw.print(cDoc.getName() + "\t");
                    pw.print(cDoc.m_xProportion[0] + "\t");
                    pw.print(cDoc.m_xProportion[1]);
                    pw.println();
                }
            }
        }
        pw.flush();
        pw.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : structures._ChildDoc4BaseWithPhi(structures._ChildDoc4BaseWithPhi) structures._ChildDoc(structures._ChildDoc) structures._Doc(structures._Doc) structures._ParentDoc(structures._ParentDoc) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter)

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