Search in sources :

Example 1 with structures._Review

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

the class MTCLinAdaptWithHDP method gradientByFunc.

@Override
protected void gradientByFunc(_AdaptStruct u, _Doc review, double weight, double[] g) {
    _Review r = (_Review) review;
    _HDPThetaStar theta = r.getHDPThetaStar();
    // feature index
    int n, k, s;
    int cIndex = theta.getIndex();
    if (cIndex < 0 || cIndex >= m_kBar)
        System.err.println("Error,cannot find the theta star!");
    int offset = m_dim * 2 * cIndex, offsetSup = m_dim * 2 * m_kBar;
    double[] Au = theta.getModel();
    double delta = (review.getYLabel() - logit(review.getSparse(), r)) * weight;
    // Bias term for individual user.
    // a[0] = ws0*x0; x0=1
    g[offset] -= delta * getSupWeights(0);
    // b[0]
    g[offset + m_dim] -= delta;
    // Bias term for super user.
    // a_s[0] = a_i0*w_g0*x_d0
    g[offsetSup] -= delta * Au[0] * m_gWeights[0];
    // b_s[0] = a_i0*x_d0
    g[offsetSup + m_dimSup] -= delta * Au[0];
    // Traverse all the feature dimension to calculate the gradient for both individual users and super user.
    for (_SparseFeature fv : review.getSparse()) {
        n = fv.getIndex() + 1;
        k = m_featureGroupMap[n];
        // w_si*x_di
        g[offset + k] -= delta * getSupWeights(n) * fv.getValue();
        // x_di
        g[offset + m_dim + k] -= delta * fv.getValue();
        s = m_featureGroupMap4SupUsr[n];
        // a_i*w_gi*x_di
        g[offsetSup + s] -= delta * Au[k] * m_gWeights[n] * fv.getValue();
        // a_i*x_di
        g[offsetSup + m_dimSup + s] -= delta * Au[k] * fv.getValue();
    }
}
Also used : structures._Review(structures._Review) structures._HDPThetaStar(structures._HDPThetaStar) structures._SparseFeature(structures._SparseFeature)

Example 2 with structures._Review

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

the class MTCLinAdaptWithHDPMultipleE method gradientByFunc.

@Override
protected void gradientByFunc(_AdaptStruct u, _Doc review, double weight, double[] g) {
    int index = -1;
    double confidence = 1;
    _Review r = (_Review) review;
    _HDPThetaStar oldTheta = r.getHDPThetaStar();
    HashMap<_HDPThetaStar, Integer> thetaCountMap = r.getThetaCountMap();
    for (_HDPThetaStar theta : thetaCountMap.keySet()) {
        index = findHDPThetaStar(theta);
        // some of the cluster may disappear, ignore them.
        if (index >= m_kBar || index < 0)
            continue;
        r.setHDPThetaStar(theta);
        confidence = thetaCountMap.get(theta);
        // confidence plays the role of weight here, how many times the review shows in the cluster.
        super.gradientByFunc(u, review, confidence, g);
    }
    r.setHDPThetaStar(oldTheta);
}
Also used : structures._Review(structures._Review) structures._HDPThetaStar(structures._HDPThetaStar)

Example 3 with structures._Review

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

the class _HDPAdaptStruct method evaluate.

@Override
public double evaluate(_Doc doc) {
    _Review r = (_Review) doc;
    double prob = 0, sum = 0;
    double[] probs = r.getCluPosterior();
    int n, m, k;
    // not adaptation based
    if (m_dim == 0) {
        for (k = 0; k < probs.length; k++) {
            // need to be fixed: here we assumed binary classification
            sum = Utils.dotProduct(CLRWithHDP.m_hdpThetaStars[k].getModel(), doc.getSparse(), 0);
            if (MTCLRWithHDP.m_supWeights != null && MTCLRWithHDP.m_q != 0)
                sum += CLRWithDP.m_q * Utils.dotProduct(MTCLRWithHDP.m_supWeights, doc.getSparse(), 0);
            // to maintain numerical precision, compute the expectation in log space as well
            if (k == 0)
                prob = probs[k] + Math.log(Utils.logistic(sum));
            else
                prob = Utils.logSum(prob, probs[k] + Math.log(Utils.logistic(sum)));
        }
    } else {
        double[] As;
        for (k = 0; k < probs.length; k++) {
            As = CLRWithHDP.m_hdpThetaStars[k].getModel();
            // Bias term: w_s0*a0+b0.
            sum = As[0] * CLinAdaptWithHDP.m_supWeights[0] + As[m_dim];
            for (_SparseFeature fv : doc.getSparse()) {
                n = fv.getIndex() + 1;
                m = m_featureGroupMap[n];
                sum += (As[m] * CLinAdaptWithHDP.m_supWeights[n] + As[m_dim + m]) * fv.getValue();
            }
            // to maintain numerical precision, compute the expectation in log space as well
            if (k == 0)
                prob = probs[k] + Math.log(Utils.logistic(sum));
            else
                prob = Utils.logSum(prob, probs[k] + Math.log(Utils.logistic(sum)));
        }
    }
    // accumulate the prediction results during sampling procedure
    doc.m_pCount++;
    // >0.5?1:0;
    doc.m_prob += Math.exp(prob);
    return prob;
}
Also used : structures._Review(structures._Review) structures._SparseFeature(structures._SparseFeature)

Example 4 with structures._Review

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

the class _HDPAdaptStruct method evaluateG.

// Evaluate the performance of the global part.
public double evaluateG(_Doc doc) {
    _Review r = (_Review) doc;
    double prob = 0, sum = 0;
    double[] probs = r.getCluPosterior();
    int n, k;
    for (k = 0; k < probs.length; k++) {
        // As = CLRWithHDP.m_hdpThetaStars[k].getModel();
        // Bias term: w_s0*a0+b0.
        sum = CLinAdaptWithHDP.m_supWeights[0];
        for (_SparseFeature fv : doc.getSparse()) {
            n = fv.getIndex() + 1;
            sum += CLinAdaptWithHDP.m_supWeights[n] * fv.getValue();
        }
        // to maintain numerical precision, compute the expectation in log space as well
        if (k == 0)
            prob = probs[k] + Math.log(Utils.logistic(sum));
        else
            prob = Utils.logSum(prob, probs[k] + Math.log(Utils.logistic(sum)));
    }
    // accumulate the prediction results during sampling procedure
    doc.m_pCount_g++;
    // >0.5?1:0;
    doc.m_prob_g += Math.exp(prob);
    return prob;
}
Also used : structures._Review(structures._Review) structures._SparseFeature(structures._SparseFeature)

Example 5 with structures._Review

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

the class CLRWithMMB method calculateMixture4TestUser.

// calculate the mixture for test user based on review assignment
public void calculateMixture4TestUser(_MMBAdaptStruct user) {
    int cIndex = 0;
    double prob, logSum, sum = 0;
    double[] probs = new double[m_kBar];
    _HDPThetaStar curTheta;
    // calculate the cluster assignment for each review first
    for (_Review r : user.getReviews()) {
        // suppose all reviews are test review in this setting
        if (r.getType() != rType.TEST)
            continue;
        for (int k = 0; k < probs.length; k++) {
            curTheta = m_hdpThetaStars[k];
            r.setHDPThetaStar(curTheta);
            prob = calcLogLikelihoodX(r) + Math.log(calcGroupPopularity(user, k, curTheta.getGamma()));
            probs[k] = prob;
        }
        // normalize the prob
        logSum = Utils.logSumOfExponentials(probs);
        for (int k = 0; k < probs.length; k++) probs[k] -= logSum;
        // take the cluster that has maximum prob as the review's cluster assignment
        curTheta = m_hdpThetaStars[Utils.argmax(probs)];
        r.setHDPThetaStar(curTheta);
        // update the cluster assignment for the user
        user.incHDPThetaStarMemSize(r.getHDPThetaStar(), 1);
    }
    // calculate the mixture: get the review assignment and normalize it
    Arrays.fill(probs, 0);
    // calculate the sum first
    for (_HDPThetaStar theta : user.getHDPTheta4Rvw()) {
        sum += user.getHDPThetaMemSize(theta);
    }
    // calculate the prob for each dim
    for (_HDPThetaStar theta : user.getHDPTheta4Rvw()) {
        cIndex = theta.getIndex();
        probs[cIndex] = user.getHDPThetaMemSize(theta) / sum;
    }
    user.setMixture(probs);
}
Also used : structures._Review(structures._Review) structures._HDPThetaStar(structures._HDPThetaStar)

Aggregations

structures._Review (structures._Review)44 structures._SparseFeature (structures._SparseFeature)24 structures._HDPThetaStar (structures._HDPThetaStar)9 ArrayList (java.util.ArrayList)8 Feature (Classifier.supervised.liblinear.Feature)6 Classifier.supervised.modelAdaptation._AdaptStruct (Classifier.supervised.modelAdaptation._AdaptStruct)6 structures._PerformanceStat (structures._PerformanceStat)6 IOException (java.io.IOException)5 File (java.io.File)4 structures._User (structures._User)4 FeatureNode (Classifier.supervised.liblinear.FeatureNode)3 Parameter (Classifier.supervised.liblinear.Parameter)3 Problem (Classifier.supervised.liblinear.Problem)3 structures._RankItem (structures._RankItem)3 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 PrintWriter (java.io.PrintWriter)2 MyPriorityQueue (structures.MyPriorityQueue)2 SolverType (Classifier.supervised.liblinear.SolverType)1