Search in sources :

Example 76 with structures._SparseFeature

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

the class Utils method createSpVct.

public static _SparseFeature[] createSpVct(HashMap<Integer, Double> vct) {
    _SparseFeature[] spVct = new _SparseFeature[vct.size()];
    int i = 0;
    Iterator<Entry<Integer, Double>> it = vct.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<Integer, Double> pairs = (Map.Entry<Integer, Double>) it.next();
        spVct[i] = new _SparseFeature(pairs.getKey(), pairs.getValue());
        i++;
    }
    Arrays.sort(spVct);
    return spVct;
}
Also used : Entry(java.util.Map.Entry) structures._SparseFeature(structures._SparseFeature) HashMap(java.util.HashMap) Map(java.util.Map)

Example 77 with structures._SparseFeature

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

the class Utils method createLibLinearFV.

public static Feature[] createLibLinearFV(_SparseFeature[] spVct, int fSize) {
    Feature[] node;
    if (// include bias term in the end
    fSize > 0)
        node = new Feature[1 + spVct.length];
    else
        // ignore bias term
        node = new Feature[spVct.length];
    int fid = 0;
    for (_SparseFeature fv : spVct) // svm's feature index starts from 1
    node[fid++] = new FeatureNode(1 + fv.getIndex(), fv.getValue());
    if (fSize > 0)
        node[fid] = new FeatureNode(1 + fSize, 1.0);
    return node;
}
Also used : FeatureNode(Classifier.supervised.liblinear.FeatureNode) structures._SparseFeature(structures._SparseFeature) Feature(Classifier.supervised.liblinear.Feature) structures._SparseFeature(structures._SparseFeature)

Example 78 with structures._SparseFeature

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

the class Utils method createSpVct.

public static _SparseFeature[] createSpVct(ArrayList<HashMap<Integer, Double>> vcts) {
    HashMap<Integer, _SparseFeature> spVcts = new HashMap<Integer, _SparseFeature>();
    HashMap<Integer, Double> vPtr;
    _SparseFeature spV;
    int dim = vcts.size();
    for (int i = 0; i < dim; i++) {
        vPtr = vcts.get(i);
        if (vPtr == null || vPtr.isEmpty())
            // it is possible that we are missing this dimension
            continue;
        // iterate through all the features in this section
        Iterator<Entry<Integer, Double>> it = vPtr.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Integer, Double> pairs = (Map.Entry<Integer, Double>) it.next();
            int index = pairs.getKey();
            double value = pairs.getValue();
            if (spVcts.containsKey(index)) {
                spV = spVcts.get(index);
                // increase the total value
                spV.addValue(value);
            } else {
                spV = new _SparseFeature(index, value, dim);
                spVcts.put(index, spV);
            }
            spV.setValue4Dim(value, i);
        }
    }
    int size = spVcts.size();
    _SparseFeature[] resultVct = spVcts.values().toArray(new _SparseFeature[size]);
    Arrays.sort(resultVct);
    return resultVct;
}
Also used : HashMap(java.util.HashMap) Entry(java.util.Map.Entry) structures._SparseFeature(structures._SparseFeature) HashMap(java.util.HashMap) Map(java.util.Map)

Example 79 with structures._SparseFeature

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

the class Utils method jaccard.

public static double jaccard(_SparseFeature[] spVct1, _SparseFeature[] spVct2) {
    if (spVct1 == null || spVct2 == null)
        // What is the minimal value of similarity?
        return 0;
    double overlap = 0;
    int p1 = 0, p2 = 0;
    while (p1 < spVct1.length && p2 < spVct2.length) {
        _SparseFeature t1 = spVct1[p1];
        _SparseFeature t2 = spVct2[p2];
        if (t1.getIndex() == t2.getIndex()) {
            overlap++;
            p1++;
            p2++;
        } else if (t1.getIndex() > t2.getIndex())
            p2++;
        else
            p1++;
    }
    return overlap / (spVct1.length + spVct2.length - overlap);
}
Also used : structures._SparseFeature(structures._SparseFeature)

Example 80 with structures._SparseFeature

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

the class LinAdapt method gradientByFunc.

// shared gradient calculation by batch and online updating
@Override
protected void gradientByFunc(_AdaptStruct u, _Doc review, double weight) {
    _LinAdaptStruct user = (_LinAdaptStruct) u;
    // feature index and feature group index
    int n, k;
    // general enough to accommodate both LinAdapt and CoLinAdapt
    int offset = 2 * m_dim * user.getId();
    double delta = weight * (review.getYLabel() - logit(review.getSparse(), user));
    if (m_LNormFlag)
        delta /= getAdaptationSize(user);
    // Bias term.
    // a[0] = w0*x0; x0=1
    m_g[offset] -= delta * m_gWeights[0];
    // b[0]
    m_g[offset + m_dim] -= delta;
    // Traverse all the feature dimension to calculate the gradient.
    for (_SparseFeature fv : review.getSparse()) {
        n = fv.getIndex() + 1;
        k = m_featureGroupMap[n];
        m_g[offset + k] -= delta * m_gWeights[n] * fv.getValue();
        m_g[offset + m_dim + k] -= delta * fv.getValue();
    }
}
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