Search in sources :

Example 1 with PprExample

use of edu.cmu.ml.proppr.examples.PprExample 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;
        }
    }
}
Also used : TIntDoubleHashMap(gnu.trove.map.hash.TIntDoubleHashMap) TIntDoubleMap(gnu.trove.map.TIntDoubleMap) TIntDoubleIterator(gnu.trove.iterator.TIntDoubleIterator) PprExample(edu.cmu.ml.proppr.examples.PprExample)

Example 2 with PprExample

use of edu.cmu.ml.proppr.examples.PprExample in project ProPPR by TeamCohen.

the class SRW method inferenceUpdate.

protected void inferenceUpdate(PosNegRWExample example, StatusLogger status) {
    PprExample ex = (PprExample) example;
    double[] pNext = new double[ex.getGraph().node_hi];
    TIntDoubleMap[] dNext = new TIntDoubleMap[ex.getGraph().node_hi];
    // p: 2. for each node u
    for (int uid = 0; uid < ex.getGraph().node_hi; uid++) {
        if (log.isInfoEnabled() && status.due(4))
            log.info("Inference: node " + (uid + 1) + " of " + (ex.getGraph().node_hi));
        // p: 2(a) p_u^{t+1} += alpha * s_u
        pNext[uid] += c.apr.alpha * Dictionary.safeGet(ex.getQueryVec(), uid, 0.0);
        // p: 2(b) 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];
            // p: 2(b)i. p_v^{t+1} += (1-alpha) * p_u^t * M_uv
            if (vid >= pNext.length) {
                throw new IllegalStateException("vid=" + vid + " > pNext.length=" + pNext.length);
            }
            pNext[vid] += (1 - c.apr.alpha) * ex.p[uid] * ex.M[uid][xvi];
            // d: i. for each feature i in dM_uv:
            if (dNext[vid] == null)
                dNext[vid] = new TIntDoubleHashMap(ex.dM_hi[uid][xvi] - ex.dM_lo[uid][xvi]);
            for (int dmi = ex.dM_lo[uid][xvi]; dmi < ex.dM_hi[uid][xvi]; dmi++) {
                // d_vi^{t+1} += (1-alpha) * p_u^{t} * dM_uvi
                if (ex.dM_value[dmi] == 0)
                    continue;
                double inc = (1 - c.apr.alpha) * ex.p[uid] * ex.dM_value[dmi];
                dNext[vid].adjustOrPutValue(ex.dM_feature_id[dmi], inc, inc);
            }
            // skip when d is empty
            if (ex.dp[uid] == null)
                continue;
            for (TIntDoubleIterator it = ex.dp[uid].iterator(); it.hasNext(); ) {
                it.advance();
                if (it.value() == 0)
                    continue;
                // d_vi^{t+1} += (1-alpha) * d_ui^t * M_uv
                double inc = (1 - c.apr.alpha) * it.value() * ex.M[uid][xvi];
                dNext[vid].adjustOrPutValue(it.key(), inc, inc);
            }
        }
    }
    // sanity check on p
    if (log.isDebugEnabled()) {
        double sum = 0;
        for (double d : pNext) sum += d;
        if (Math.abs(sum - 1.0) > c.apr.epsilon)
            log.error("invalid p computed: " + sum);
    }
    ex.p = pNext;
    ex.dp = dNext;
}
Also used : TIntDoubleHashMap(gnu.trove.map.hash.TIntDoubleHashMap) TIntDoubleMap(gnu.trove.map.TIntDoubleMap) TIntDoubleIterator(gnu.trove.iterator.TIntDoubleIterator) PprExample(edu.cmu.ml.proppr.examples.PprExample)

Aggregations

PprExample (edu.cmu.ml.proppr.examples.PprExample)2 TIntDoubleIterator (gnu.trove.iterator.TIntDoubleIterator)2 TIntDoubleMap (gnu.trove.map.TIntDoubleMap)2 TIntDoubleHashMap (gnu.trove.map.hash.TIntDoubleHashMap)2