use of gnu.trove.map.TIntDoubleMap in project ProPPR by TeamCohen.
the class RWExampleParser method parse.
public PosNegRWExample parse(String line, LearningGraphBuilder builder, SRW learner) throws GraphFormatException {
//String[] parts = line.trim().split(MAJOR_DELIM,5);
// first parse the query metadata
//LearningGraphBuilder.split(line,'\t',4);
String[] parts = new String[4];
int last = 0, i = 0;
for (int next = last; i < parts.length; last = next + 1, i++) {
if (next == -1)
throw new GraphFormatException("Need 8 distinct tsv fields in the grounded example:" + line);
next = line.indexOf(MAJOR_DELIM, last);
parts[i] = next < 0 ? line.substring(last) : line.substring(last, next);
}
TIntDoubleMap queryVec = new TIntDoubleHashMap();
//for(String u : parts[1].split(MINOR_DELIM)) queryVec.put(Integer.parseInt(u), 1.0);
for (int u : parseNodes(parts[1])) queryVec.put(u, 1.0);
int[] posList, negList;
if (//stringToInt(parts[2].split(MINOR_DELIM));
parts[2].length() > 0)
//stringToInt(parts[2].split(MINOR_DELIM));
posList = parseNodes(parts[2]);
else
posList = new int[0];
if (//stringToInt(parts[3].split(MINOR_DELIM));
parts[3].length() > 0)
//stringToInt(parts[3].split(MINOR_DELIM));
negList = parseNodes(parts[3]);
else
negList = new int[0];
LearningGraph g = builder.deserialize(line.substring(last));
return learner.makeExample(parts[0], g, queryVec, posList, negList);
}
use of gnu.trove.map.TIntDoubleMap in project ProPPR by TeamCohen.
the class AdaGradSRW method agd.
/**
* AdaGrad Descent Algo
*
* edits params using totSqGrad as well
*
* @author rosecatherinek
*/
protected void agd(ParamVector<String, ?> params, PosNegRWExample ex) {
TIntDoubleMap gradient = gradient(params, ex);
// apply gradient to param vector
for (TIntDoubleIterator grad = gradient.iterator(); grad.hasNext(); ) {
grad.advance();
// avoid underflow since we're summing the square
if (Math.abs(grad.value()) < MIN_GRADIENT)
continue;
String feature = ex.getGraph().featureLibrary.getSymbol(grad.key());
if (trainable(feature)) {
Double g = grad.value();
//first update the running total of the square of the gradient
totSqGrad.adjustValue(feature, g * g);
//now get the running total
// Double rt = totSqGrad.get(feature);
//w_{t+1, i} = w_{t, i} - \eta * g_{t,i} / \sqrt{ G,i }
// Double descentVal = - c.eta * g / Math.sqrt(rt);
params.adjustValue(feature, -learningRate(feature) * g);
if (params.get(feature).isInfinite()) {
log.warn("Infinity at " + feature + "; gradient " + grad.value() + "; rt " + totSqGrad.get(feature));
}
}
}
}
use of gnu.trove.map.TIntDoubleMap in project ProPPR by TeamCohen.
the class SRW method load.
/** fills M, dM in ex **/
protected void load(ParamVector<String, ?> params, PosNegRWExample example) {
PprExample ex = (PprExample) example;
int dM_cursor = 0;
for (int uid = 0; uid < ex.getGraph().node_hi; uid++) {
// (a); (b): initialization
double tu = 0;
TIntDoubleMap dtu = new TIntDoubleHashMap();
int udeg = ex.getGraph().node_near_hi[uid] - ex.getGraph().node_near_lo[uid];
double[] suv = new double[udeg];
double[][] dfu = new double[udeg][];
// begin (c): for each neighbor v of u,
for (int eid = ex.getGraph().node_near_lo[uid], xvi = 0; eid < ex.getGraph().node_near_hi[uid]; eid++, xvi++) {
int vid = ex.getGraph().edge_dest[eid];
// i. s_{uv} = w * phi_{uv}, a scalar:
suv[xvi] = 0;
for (int lid = ex.getGraph().edge_labels_lo[eid]; lid < ex.getGraph().edge_labels_hi[eid]; lid++) {
suv[xvi] += params.get(ex.getGraph().featureLibrary.getSymbol(ex.getGraph().label_feature_id[lid])) * ex.getGraph().label_feature_weight[lid];
}
// ii. t_u += f(s_{uv}), a scalar:
tu += c.squashingFunction.edgeWeight(suv[xvi]);
// iii. df_{uv} = f'(s_{uv})* phi_{uv}, a vector, as sparse as phi_{uv}
// by looping over features i in phi_{uv}
double[] dfuv = new double[ex.getGraph().edge_labels_hi[eid] - ex.getGraph().edge_labels_lo[eid]];
double cee = c.squashingFunction.computeDerivative(suv[xvi]);
for (int lid = ex.getGraph().edge_labels_lo[eid], dfuvi = 0; lid < ex.getGraph().edge_labels_hi[eid]; lid++, dfuvi++) {
// iii. again
dfuv[dfuvi] = cee * ex.getGraph().label_feature_weight[lid];
// iv. dt_u += df_{uv}, a vector, as sparse as sum_{v'} phi_{uv'}
// by looping over features i in df_{uv}
// (identical to features i in phi_{uv}, so we use the same loop)
dtu.adjustOrPutValue(ex.getGraph().label_feature_id[lid], dfuv[dfuvi], dfuv[dfuvi]);
}
dfu[xvi] = dfuv;
}
// end (c)
// begin (d): for each neighbor v of u,
double scale = (1 / (tu * tu));
for (int eid = ex.getGraph().node_near_lo[uid], xvi = 0; eid < ex.getGraph().node_near_hi[uid]; eid++, xvi++) {
int vid = ex.getGraph().edge_dest[eid];
//dM_features.size();
ex.dM_lo[uid][xvi] = dM_cursor;
// create the vector dM_{uv} = (1/t^2_u) * (t_u * df_{uv} - f(s_{uv}) * dt_u)
// by looping over features i in dt_u
// getting the df offset for features in dt_u is awkward, so we'll first iterate over features in df_uv,
// then fill in the rest
int[] seenFeatures = new int[ex.getGraph().edge_labels_hi[eid] - ex.getGraph().edge_labels_lo[eid]];
for (int lid = ex.getGraph().edge_labels_lo[eid], dfuvi = 0; lid < ex.getGraph().edge_labels_hi[eid]; lid++, dfuvi++) {
int fid = ex.getGraph().label_feature_id[lid];
//dM_features.add(fid);
ex.dM_feature_id[dM_cursor] = fid;
double dMuvi = (tu * dfu[xvi][dfuvi] - c.squashingFunction.edgeWeight(suv[xvi]) * dtu.get(fid));
if (tu == 0) {
if (dMuvi != 0)
throw new IllegalStateException("tu=0 at u=" + uid + "; example " + ex.toString());
} else
dMuvi *= scale;
//dM_values.add(dMuvi);
ex.dM_value[dM_cursor] = dMuvi;
dM_cursor++;
//save this feature so we can skip it later
seenFeatures[dfuvi] = fid;
}
Arrays.sort(seenFeatures);
// we've hit all the features in df_uv, now we do the remaining features in dt_u:
for (TIntDoubleIterator it = dtu.iterator(); it.hasNext(); ) {
it.advance();
// skip features we already added in the df_uv loop
if (Arrays.binarySearch(seenFeatures, it.key()) >= 0)
continue;
//dM_features.add(it.key());
ex.dM_feature_id[dM_cursor] = it.key();
// zero the first term, since df_uv doesn't cover this feature
double dMuvi = scale * (-c.squashingFunction.edgeWeight(suv[xvi]) * it.value());
//dM_values.add(dMuvi);
ex.dM_value[dM_cursor] = dMuvi;
dM_cursor++;
}
//dM_features.size();
ex.dM_hi[uid][xvi] = dM_cursor;
// also create the scalar M_{uv} = f(s_{uv}) / t_u
ex.M[uid][xvi] = c.squashingFunction.edgeWeight(suv[xvi]);
if (tu == 0) {
if (ex.M[uid][xvi] != 0)
throw new IllegalStateException("tu=0 at u=" + uid + "; example " + ex.toString());
} else
ex.M[uid][xvi] /= tu;
}
}
}
use of gnu.trove.map.TIntDoubleMap in project ProPPR by TeamCohen.
the class SRW method accumulateGradient.
public void accumulateGradient(ParamVector<String, ?> params, PosNegRWExample example, ParamVector<String, ?> accumulator, StatusLogger status) {
log.debug("Gradient calculating on " + example);
initializeFeatures(params, example.getGraph());
ParamVector<String, Double> prepare = new SimpleParamVector<String>();
regularizer.prepareForExample(params, example.getGraph(), prepare);
load(params, example);
inference(params, example, status);
TIntDoubleMap gradient = gradient(params, example);
for (Map.Entry<String, Double> e : prepare.entrySet()) {
if (trainable(e.getKey()))
accumulator.adjustValue(e.getKey(), -e.getValue() / example.length());
}
for (TIntDoubleIterator it = gradient.iterator(); it.hasNext(); ) {
it.advance();
String feature = example.getGraph().featureLibrary.getSymbol(it.key());
if (trainable(feature))
accumulator.adjustValue(example.getGraph().featureLibrary.getSymbol(it.key()), it.value() / example.length());
}
}
use of gnu.trove.map.TIntDoubleMap in project ProPPR by TeamCohen.
the class RedBlueGraph method colorPart.
public TIntDoubleMap colorPart(final Set<String> color, TIntDoubleMap vec) {
final TIntDoubleMap result = new TIntDoubleHashMap();
vec.forEachEntry(new TIntDoubleProcedure() {
@Override
public boolean execute(int k, double v) {
if (color.contains(nodes.getSymbol(k)))
result.put(k, v);
return true;
}
});
return result;
}
Aggregations