Search in sources :

Example 41 with structures._SparseFeature

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

the class CLRWithHDP method gradientByFunc.

@Override
protected void gradientByFunc(_AdaptStruct u, _Doc r, double weight, double[] g) {
    _Review review = (_Review) r;
    // feature index
    int n;
    int cIndex = review.getHDPThetaStar().getIndex();
    if (cIndex < 0 || cIndex >= m_kBar)
        System.err.println("Error,cannot find the HDP theta star!");
    int offset = m_dim * cIndex;
    double delta = weight * (review.getYLabel() - logit(review.getSparse(), review));
    // Bias term.
    // x0=1
    g[offset] -= delta;
    // Traverse all the feature dimension to calculate the gradient.
    for (_SparseFeature fv : review.getSparse()) {
        n = fv.getIndex() + 1;
        g[offset + n] -= delta * fv.getValue();
    }
}
Also used : structures._Review(structures._Review) structures._SparseFeature(structures._SparseFeature)

Example 42 with structures._SparseFeature

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

the class CLRWithHDP method calcLogLikelihoodX.

protected double calcLogLikelihoodX(_Review r) {
    if (r.getHDPThetaStar().getLMStat() == null) {
        return r.getL4NewCluster();
    } else {
        double[] Ns = r.getHDPThetaStar().getLMStat();
        double N = Utils.sumOfArray(Ns);
        double n = r.getLMSum();
        _SparseFeature[] fvs = r.getLMSparse();
        double L = Utils.lgamma(m_betaSum + N) - Utils.lgamma(m_betaSum + N + n);
        for (_SparseFeature fv : fvs) {
            L += logGammaDivision((int) fv.getValue(), m_betas[fv.getIndex()], Ns[fv.getIndex()]);
        }
        return L;
    }
}
Also used : structures._SparseFeature(structures._SparseFeature)

Example 43 with structures._SparseFeature

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

the class CLinAdaptWithHDP method logit.

@Override
protected double logit(_SparseFeature[] fvs, _Review r) {
    int k, n;
    double[] Au = r.getHDPThetaStar().getModel();
    // Bias term: w_s0*a0+b0.
    double sum = Au[0] * m_gWeights[0] + Au[m_dim];
    for (_SparseFeature fv : fvs) {
        n = fv.getIndex() + 1;
        k = m_featureGroupMap[n];
        sum += (Au[k] * m_gWeights[n] + Au[m_dim + k]) * fv.getValue();
    }
    return Utils.logistic(sum);
}
Also used : structures._SparseFeature(structures._SparseFeature)

Example 44 with structures._SparseFeature

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

the class ACCTM_C method calculate_log_likelihood4Child.

@Override
protected double calculate_log_likelihood4Child(_Doc d) {
    _ChildDoc4BaseWithPhi cDoc = (_ChildDoc4BaseWithPhi) d;
    double docLogLikelihood = 0.0;
    double gammaLen = Utils.sumOfArray(m_gamma);
    double cDocXSum = Utils.sumOfArray(cDoc.m_xSstat);
    // prepare compute the normalizers
    _SparseFeature[] fv = cDoc.getSparse();
    for (int i = 0; i < fv.length; i++) {
        int wid = fv[i].getIndex();
        double value = fv[i].getValue();
        double wordLogLikelihood = 0;
        for (int k = 0; k < number_of_topics; k++) {
            double wordPerTopicLikelihood = childWordByTopicProb(k, wid) * childTopicInDocProb(k, cDoc) * childXInDocProb(0, cDoc) / (cDocXSum + gammaLen);
            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 += value * wordLogLikelihood;
    }
    return docLogLikelihood;
}
Also used : structures._ChildDoc4BaseWithPhi(structures._ChildDoc4BaseWithPhi) structures._SparseFeature(structures._SparseFeature)

Example 45 with structures._SparseFeature

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

the class ACCTM_test method printParentPhi.

protected void printParentPhi(_Doc d, File phiFolder) {
    _ParentDoc pDoc = (_ParentDoc) d;
    String parentPhiFileName = pDoc.getName() + ".txt";
    _SparseFeature[] fv = pDoc.getSparse();
    try {
        PrintWriter parentPW = new PrintWriter(new File(phiFolder, parentPhiFileName));
        for (int n = 0; n < fv.length; n++) {
            int index = fv[n].getIndex();
            String featureName = m_corpus.getFeature(index);
            parentPW.print(featureName + ":\t");
            for (int k = 0; k < number_of_topics; k++) parentPW.print(pDoc.m_phi[n][k] + "\t");
            parentPW.println();
        }
        parentPW.flush();
        parentPW.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : structures._ParentDoc(structures._ParentDoc) structures._SparseFeature(structures._SparseFeature) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter)

Aggregations

structures._SparseFeature (structures._SparseFeature)94 structures._ChildDoc (structures._ChildDoc)14 structures._Doc (structures._Doc)14 structures._Review (structures._Review)14 HashMap (java.util.HashMap)7 structures._ParentDoc (structures._ParentDoc)7 structures._Stn (structures._Stn)7 Feature (Classifier.supervised.liblinear.Feature)6 FeatureNode (Classifier.supervised.liblinear.FeatureNode)6 structures._RankItem (structures._RankItem)5 File (java.io.File)3 PrintWriter (java.io.PrintWriter)3 Classifier.supervised.modelAdaptation._AdaptStruct (Classifier.supervised.modelAdaptation._AdaptStruct)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 structures._ChildDoc4BaseWithPhi (structures._ChildDoc4BaseWithPhi)2 structures._HDPThetaStar (structures._HDPThetaStar)2