Search in sources :

Example 11 with TIntDoubleIterator

use of gnu.trove.iterator.TIntDoubleIterator in project ProPPR by TeamCohen.

the class DprSRW method inference.

@Override
protected void inference(ParamVector<String, ?> params, PosNegRWExample example, StatusLogger status) {
    DprExample ex = (DprExample) example;
    // startNode maps node->weight
    TIntDoubleMap query = ex.getQueryVec();
    if (query.size() > 1)
        throw new UnsupportedOperationException("Can't do multi-node queries");
    // maps storing the probability and remainder weights of the nodes:
    ex.p = new double[ex.getGraph().node_hi];
    ex.r = new double[ex.getGraph().node_hi];
    // initializing the above maps:
    Arrays.fill(ex.p, 0.0);
    Arrays.fill(ex.r, 0.0);
    for (TIntDoubleIterator it = query.iterator(); it.hasNext(); ) {
        it.advance();
        ex.r[it.key()] = it.value();
    }
    // maps storing the gradients of p and r for each node:
    ex.dp = new TIntDoubleMap[ex.getGraph().node_hi];
    ex.dr = new TIntDoubleMap[ex.getGraph().node_hi];
    // initializing the above maps:
    //		for(int node : example.getGraph().getNodes()) {
    //			dp.put(node, new TObjectDoubleHashMap<String>());
    //			dr.put(node, new TObjectDoubleHashMap<String>());
    //			for(String feature : (example.getGraph().getFeatureSet()))
    //			{
    //				dp.get(node).put(feature, 0.0);
    //				dr.get(node).put(feature, 0.0);
    //			}
    //		}
    // APR Algorithm:
    int completeCount = 0;
    while (completeCount < ex.getGraph().node_hi) {
        if (log.isDebugEnabled())
            log.debug("Starting pass");
        completeCount = 0;
        for (int u = 0; u < ex.getGraph().node_hi; u++) {
            double ru = ex.r[u];
            int udeg = ex.getGraph().node_near_hi[u] - ex.getGraph().node_near_lo[u];
            if (ru / (double) udeg > c.apr.epsilon)
                while (ru / udeg > c.apr.epsilon) {
                    this.push(u, params, ex);
                    if (ex.r[u] > ru)
                        throw new IllegalStateException("r increasing! :(");
                    ru = ex.r[u];
                }
            else {
                completeCount++;
                if (log.isDebugEnabled())
                    log.debug("Counting " + u);
            }
        }
        if (log.isDebugEnabled())
            log.debug(completeCount + " of " + ex.getGraph().node_hi + " completed this pass");
        else if (log.isInfoEnabled() && status.due(3))
            log.info(Thread.currentThread() + " inference: " + completeCount + " of " + ex.getGraph().node_hi + " completed this pass");
    }
//		GradientComponents g = new GradientComponents();
//		g.p = p;
//		g.d = dp;
//		return g;
}
Also used : DprExample(edu.cmu.ml.proppr.examples.DprExample) TIntDoubleMap(gnu.trove.map.TIntDoubleMap) TIntDoubleIterator(gnu.trove.iterator.TIntDoubleIterator)

Example 12 with TIntDoubleIterator

use of gnu.trove.iterator.TIntDoubleIterator in project ProPPR by TeamCohen.

the class NormalizedPosLoss method computeLossGradient.

@Override
public int computeLossGradient(ParamVector params, PosNegRWExample example, TIntDoubleMap gradient, LossData lossdata, SRWOptions c) {
    PosNegRWExample ex = (PosNegRWExample) example;
    int nonzero = 0;
    double mag = 0;
    //is zero or 1, and the empirical loss gradient is zero.
    if (ex.getNegList().length == 0 || ex.getPosList().length == 0)
        return nonzero;
    double sumPos = 0;
    for (int a : ex.getPosList()) {
        sumPos += clip(ex.p[a]);
    }
    sumPos = clip(sumPos);
    for (int a : ex.getPosList()) {
        for (TIntDoubleIterator da = ex.dp[a].iterator(); da.hasNext(); ) {
            da.advance();
            if (da.value() == 0)
                continue;
            nonzero++;
            double aterm = -da.value() / sumPos;
            gradient.adjustOrPutValue(da.key(), aterm, aterm);
        }
    }
    lossdata.add(LOSS.LOG, -Math.log(sumPos));
    double sumPosNeg = 0;
    for (int pa : ex.getPosList()) {
        sumPosNeg += clip(ex.p[pa]);
    }
    for (int pa : ex.getNegList()) {
        sumPosNeg += clip(ex.p[pa]);
    }
    sumPosNeg = clip(sumPosNeg);
    for (int a : ex.getPosList()) {
        for (TIntDoubleIterator da = ex.dp[a].iterator(); da.hasNext(); ) {
            da.advance();
            if (da.value() == 0)
                continue;
            nonzero++;
            double bterm = da.value() / sumPosNeg;
            gradient.adjustOrPutValue(da.key(), bterm, bterm);
        }
    }
    for (int b : ex.getNegList()) {
        for (TIntDoubleIterator db = ex.dp[b].iterator(); db.hasNext(); ) {
            db.advance();
            if (db.value() == 0)
                continue;
            nonzero++;
            double bterm = db.value() / sumPosNeg;
            gradient.adjustOrPutValue(db.key(), bterm, bterm);
        }
    }
    lossdata.add(LOSS.LOG, Math.log(sumPosNeg));
    return nonzero;
}
Also used : PosNegRWExample(edu.cmu.ml.proppr.examples.PosNegRWExample) TIntDoubleIterator(gnu.trove.iterator.TIntDoubleIterator)

Example 13 with TIntDoubleIterator

use of gnu.trove.iterator.TIntDoubleIterator in project DynamicSurroundings by OreCruncher.

the class PacketServerData method toBytes.

@Override
public void toBytes(@Nonnull final ByteBuf buf) {
    buf.writeDouble(this.meanTickTime);
    buf.writeInt(this.tMap.size());
    final TIntDoubleIterator i = this.tMap.iterator();
    while (i.hasNext()) {
        i.advance();
        buf.writeInt(i.key());
        buf.writeDouble(i.value());
    }
    buf.writeInt(this.free);
    buf.writeInt(this.total);
    buf.writeInt(this.max);
}
Also used : TIntDoubleIterator(gnu.trove.iterator.TIntDoubleIterator)

Aggregations

TIntDoubleIterator (gnu.trove.iterator.TIntDoubleIterator)13 TIntDoubleMap (gnu.trove.map.TIntDoubleMap)6 PosNegRWExample (edu.cmu.ml.proppr.examples.PosNegRWExample)3 TIntDoubleHashMap (gnu.trove.map.hash.TIntDoubleHashMap)3 PprExample (edu.cmu.ml.proppr.examples.PprExample)2 DprExample (edu.cmu.ml.proppr.examples.DprExample)1 Feature (edu.cmu.ml.proppr.prove.wam.Feature)1 SimpleParamVector (edu.cmu.ml.proppr.util.math.SimpleParamVector)1 TIntIterator (gnu.trove.iterator.TIntIterator)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 BufferedWriter (java.io.BufferedWriter)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1