Search in sources :

Example 31 with structures._SparseFeature

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

the class Utils method diffVector.

// x_i - x_j
public static _SparseFeature[] diffVector(_SparseFeature[] spVcti, _SparseFeature[] spVctj) {
    // first deal with special case
    if (spVcti == null && spVctj == null)
        return null;
    else if (spVctj == null)
        return spVcti;
    else if (spVcti == null)
        return negSpVct(spVctj);
    ArrayList<_SparseFeature> vectorList = new ArrayList<_SparseFeature>();
    int i = 0, j = 0;
    _SparseFeature fi = spVcti[i], fj = spVctj[j];
    double fv;
    while (i < spVcti.length && j < spVctj.length) {
        fi = spVcti[i];
        fj = spVctj[j];
        if (fi.getIndex() == fj.getIndex()) {
            fv = fi.getValue() - fj.getValue();
            if (// otherwise it is too small
            Math.abs(fv) > Double.MIN_VALUE)
                vectorList.add(new _SparseFeature(fi.getIndex(), fv));
            i++;
            j++;
        } else if (fi.getIndex() > fj.getIndex()) {
            vectorList.add(new _SparseFeature(fj.getIndex(), -fj.getValue()));
            j++;
        } else {
            vectorList.add(new _SparseFeature(fi.getIndex(), fi.getValue()));
            i++;
        }
    }
    while (i < spVcti.length) {
        fi = spVcti[i];
        vectorList.add(new _SparseFeature(fi.getIndex(), fi.getValue()));
        i++;
    }
    while (j < spVctj.length) {
        fj = spVctj[j];
        vectorList.add(new _SparseFeature(fj.getIndex(), -fj.getValue()));
        j++;
    }
    return vectorList.toArray(new _SparseFeature[vectorList.size()]);
}
Also used : ArrayList(java.util.ArrayList) structures._SparseFeature(structures._SparseFeature)

Example 32 with structures._SparseFeature

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

the class CoLinAdaptWithDiffFeatureGroups method calcPosterior.

public void calcPosterior(_SparseFeature[] fvs, _AdaptStruct u) {
    // We want get p(y=0|x) and p(y=1|x) based on ylabel.
    _CoLinAdaptDiffFvGroupsStruct user = (_CoLinAdaptDiffFvGroupsStruct) u;
    double exp0 = 0, exp1 = 0;
    // feature index and feature group index
    int n = 0, k = 0;
    // w0*x
    // Bias term: w0*a0+b0.
    exp0 = user.getScaling(0) * m_gWeights[0] * m_g0 + user.getShifting(0);
    // Bias term.
    exp1 = user.getScalingB(0) * m_gWeights[0] * m_g1 + user.getShiftingB(0);
    for (_SparseFeature fv : fvs) {
        n = fv.getIndex() + 1;
        k = m_featureGroupMap[n];
        exp0 += (user.getScaling(k) * m_gWeights[n] * m_g0 + user.getShifting(k)) * fv.getValue();
        k = m_featureGroupMapB[n];
        exp1 += (user.getScalingB(k) * m_gWeights[n] * m_g1 + user.getShiftingB(k)) * fv.getValue();
    }
    exp0 = Math.exp(exp0);
    exp1 = Math.exp(exp1);
    m_cache[0] = exp0 / (exp0 + exp1);
    m_cache[1] = exp1 / (exp0 + exp1);
}
Also used : structures._SparseFeature(structures._SparseFeature)

Example 33 with structures._SparseFeature

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

the class CoLinAdaptWithDiffFeatureGroups method gradientByFunc.

// shared gradient calculation by batch and online updating
@Override
protected void gradientByFunc(_AdaptStruct u, _Doc review, double weight) {
    _CoLinAdaptDiffFvGroupsStruct user = (_CoLinAdaptDiffFvGroupsStruct) u;
    // feature index and feature group index
    int n, k;
    // general enough to accommodate both LinAdapt and CoLinAdapt
    int offsetA = 2 * m_dimA * user.getId(), offsetB = getASize() + 2 * m_dimB * user.getId();
    double deltaA, deltaB;
    if (review.getYLabel() == 0) {
        deltaA = 1.0 - m_cache[0];
        deltaB = -m_cache[1];
    } else {
        deltaA = -m_cache[0];
        deltaB = 1.0 - m_cache[1];
    }
    if (m_LNormFlag) {
        deltaA /= getAdaptationSize(user);
        deltaB /= getAdaptationSize(user);
    }
    // Bias term.
    // a[0] = w0*x0; x0=1
    m_g[offsetA] -= weight * deltaA * m_gWeights[0] * m_g0;
    // b[0]
    m_g[offsetA + m_dimA] -= weight * deltaA;
    // a[0]
    m_g[offsetB] -= weight * deltaB * m_gWeights[0] * m_g1;
    // b[0]
    m_g[offsetB + m_dimB] -= weight * deltaB;
    // Traverse all the feature dimension to calculate the gradient.
    for (_SparseFeature fv : review.getSparse()) {
        n = fv.getIndex() + 1;
        k = m_featureGroupMap[n];
        m_g[offsetA + k] -= weight * deltaA * m_gWeights[n] * m_g0 * fv.getValue();
        m_g[offsetA + m_dimA + k] -= weight * deltaA * fv.getValue();
        k = m_featureGroupMapB[n];
        m_g[offsetB + k] -= weight * deltaB * m_gWeights[n] * m_g1 * fv.getValue();
        m_g[offsetB + m_dimB + k] -= weight * deltaB * fv.getValue();
    }
}
Also used : structures._SparseFeature(structures._SparseFeature)

Example 34 with structures._SparseFeature

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

the class MTLinAdaptWithSupUserNoAdapt method gradientByFunc.

// Gradients from loglikelihood, contributes to both individual user's gradients and super user's gradients.
protected void gradientByFunc(_AdaptStruct u, _Doc review, double weight) {
    _CoLinAdaptStruct ui = (_CoLinAdaptStruct) u;
    // feature index and feature group index
    int n, k;
    // general enough to accommodate both LinAdapt and CoLinAdapt
    int offset = 2 * m_dim * ui.getId();
    int offsetSup = 2 * m_dim * m_userList.size();
    double delta = weight * (review.getYLabel() - logit(review.getSparse(), ui));
    if (m_LNormFlag)
        delta /= getAdaptationSize(ui);
    // Bias term for individual user.
    // a[0] = (p*w_s0+q*w_g0)*x0; x0=1
    m_g[offset] -= delta * getSupWeights(0);
    // b[0]
    m_g[offset + m_dim] -= delta;
    // Bias term for super user.
    // a_s[0] = a_i0*p*x_d0
    m_g[offsetSup] -= delta * ui.getScaling(0) * m_p;
    // 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];
        // (p*w_si+q*w_gi)*x_di
        m_g[offset + k] -= delta * getSupWeights(n) * fv.getValue();
        // x_di
        m_g[offset + m_dim + k] -= delta * fv.getValue();
        // a_i*p*x_di
        m_g[offsetSup + n] -= delta * ui.getScaling(k) * m_p * fv.getValue();
    }
}
Also used : structures._SparseFeature(structures._SparseFeature)

Example 35 with structures._SparseFeature

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

the class WeightedAvgTransAdapt method gradientByFunc.

// shared gradient calculation by batch and online updating
@Override
protected void gradientByFunc(_AdaptStruct u, _Doc review, double weight) {
    _CoLinAdaptStruct ui = (_CoLinAdaptStruct) u;
    int n, k, offsetj;
    // general enough to accommodate both LinAdapt and CoLinAdapt
    int offset = m_dim * ui.getId();
    double delta = weight * (review.getYLabel() - logit(review.getSparse(), ui));
    if (m_LNormFlag)
        delta /= getAdaptationSize(ui);
    // Current user's info: Bias term + other features.
    // \theta_{ii}*w_g[0]*x_0 and x_0=1
    m_g[offset] -= delta * ui.getSelfSim() * m_gWeights[0];
    // \theta_{ii}*x_0
    m_g[offset + m_dim] -= delta * ui.getSelfSim();
    for (_SparseFeature fv : review.getSparse()) {
        n = fv.getIndex() + 1;
        k = m_featureGroupMap[n];
        // \theta_{ii}*x_d
        m_g[offset + k] -= delta * ui.getSelfSim() * m_gWeights[n] * fv.getValue();
        m_g[offset + k + m_dim] -= delta * ui.getSelfSim() * fv.getValue();
    }
    // Neighbors' info: Bias term + other features.
    for (_RankItem nit : ui.getNeighbors()) {
        offsetj = 2 * m_dim * nit.m_index;
        // Bias term.
        // neighbors' bias term.
        m_g[offsetj] -= delta * nit.m_value * m_gWeights[0];
        m_g[offsetj + m_dim] -= delta * nit.m_value;
        for (_SparseFeature fv : review.getSparse()) {
            n = fv.getIndex() + 1;
            k = m_featureGroupMap[n];
            // neighbors' other features.
            m_g[offsetj + k] -= delta * nit.m_value * m_gWeights[n] * fv.getValue();
            m_g[offsetj + m_dim + k] -= delta * nit.m_value * fv.getValue();
        }
    }
}
Also used : structures._RankItem(structures._RankItem) 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