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;
}
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;
}
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;
}
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);
}
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();
}
}
Aggregations