Search in sources :

Example 36 with structures._SparseFeature

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

the class CLRWithDP method gradientByFunc.

protected void gradientByFunc(_AdaptStruct u, _Doc review, double weight, double[] g) {
    _DPAdaptStruct user = (_DPAdaptStruct) u;
    // feature index
    int n;
    int cIndex = user.getThetaStar().getIndex();
    if (cIndex < 0 || cIndex >= m_kBar)
        System.err.println("Error,cannot find the theta star!");
    int offset = m_dim * cIndex;
    double delta = weight * (review.getYLabel() - logit(review.getSparse(), user));
    if (m_LNormFlag)
        delta /= getAdaptationSize(user);
    // 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._SparseFeature(structures._SparseFeature)

Example 37 with structures._SparseFeature

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

the class CLRWithDP method predict.

public int predict(_AdaptStruct user, _thetaStar theta) {
    double[] As;
    double sum;
    int m, n, predL = 0, count = 0;
    for (_Review r : user.getReviews()) {
        if (r.getType() == rType.TEST) {
            As = theta.getModel();
            // Bias term: w_s0*a0+b0.
            sum = As[0] * MTCLinAdaptWithDP.m_supWeights[0] + As[m_dim];
            for (_SparseFeature fv : r.getSparse()) {
                n = fv.getIndex() + 1;
                m = m_featureGroupMap[n];
                sum += (As[m] * MTCLinAdaptWithDP.m_supWeights[n] + As[m_dim + m]) * fv.getValue();
            }
            if (sum > 0.5)
                predL = 1;
            if (predL == r.getYLabel())
                count++;
        }
    }
    return count;
}
Also used : structures._Review(structures._Review) structures._SparseFeature(structures._SparseFeature)

Example 38 with structures._SparseFeature

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

the class CLinAdaptWithKmeans method linearFunc.

@Override
protected double linearFunc(_SparseFeature[] fvs, _AdaptStruct u) {
    _CLinAdaptStruct user = (_CLinAdaptStruct) u;
    int clusterIndex = m_userClusterIndex[user.getId()];
    double scaling, shifting;
    scaling = m_u * user.getGlobalScaling(0) + m_c * user.getClusterScaling(clusterIndex, 0) + m_i * user.getScaling(0);
    shifting = m_u * user.getGlobalShifting(0) + m_c * user.getClusterShifting(clusterIndex, 0) + m_i * user.getShifting(0);
    // Bias term: w0*a0+b0.
    double value = scaling * m_gWeights[0] + shifting;
    // feature index and feature group index
    int n = 0, k = 0;
    for (_SparseFeature fv : fvs) {
        n = fv.getIndex() + 1;
        k = m_featureGroupMap[n];
        scaling = m_u * user.getGlobalScaling(k) + m_c * user.getClusterScaling(clusterIndex, k) + m_i * user.getScaling(k);
        shifting = m_u * user.getGlobalShifting(k) + m_c * user.getClusterShifting(clusterIndex, k) + m_i * user.getShifting(k);
        value += (scaling * m_gWeights[n] + shifting) * fv.getValue();
    }
    return value;
}
Also used : structures._SparseFeature(structures._SparseFeature)

Example 39 with structures._SparseFeature

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

the class MTCLinAdaptWithDP method gradientByFunc.

@Override
protected void gradientByFunc(_AdaptStruct u, _Doc review, double weight, double[] g) {
    _DPAdaptStruct user = (_DPAdaptStruct) u;
    // feature index
    int n, k, s;
    int cIndex = user.getThetaStar().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 delta = (review.getYLabel() - logit(review.getSparse(), user)) * weight;
    if (m_LNormFlag)
        delta /= getAdaptationSize(user);
    // 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 * user.getScaling(0) * m_gWeights[0];
    // b_s[0] = a_i0*x_d0
    g[offsetSup + m_dimSup] -= delta * user.getScaling(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 * user.getScaling(k) * m_gWeights[n] * fv.getValue();
        // a_i*x_di
        g[offsetSup + m_dimSup + s] -= delta * user.getScaling(k) * fv.getValue();
    }
}
Also used : structures._SparseFeature(structures._SparseFeature)

Example 40 with structures._SparseFeature

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

the class MTCLinAdaptWithDP method logit.

// Logit function is different from the father class.
@Override
protected double logit(_SparseFeature[] fvs, _AdaptStruct u) {
    int k, n;
    double[] Au = ((_DPAdaptStruct) u).getThetaStar().getModel();
    // Bias term: w_s0*a0+b0.
    double value = Au[0] * getSupWeights(0) + Au[m_dim];
    for (_SparseFeature fv : fvs) {
        n = fv.getIndex() + 1;
        k = m_featureGroupMap[n];
        value += (Au[k] * getSupWeights(n) + Au[m_dim + k]) * fv.getValue();
    }
    return Utils.logistic(value);
}
Also used : structures._SparseFeature(structures._SparseFeature)

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