Search in sources :

Example 11 with PosNegRWExample

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

the class PairwiseL2SqLoss method computeLossGradient.

@Override
public int computeLossGradient(ParamVector params, PosNegRWExample example, TIntDoubleMap gradient, LossData lossdata, SRWOptions c) {
    PosNegRWExample ex = (PosNegRWExample) example;
    int nonzero = 0;
    // add empirical loss gradient term
    // positive examples
    double pmax = 0;
    for (int b : ex.getNegList()) {
        for (int a : ex.getPosList()) {
            double delta = ex.p[b] - ex.p[a];
            int[] keys = getKeys(ex.dp[b], ex.dp[a]);
            for (int feature : keys) {
                double db = ex.dp[b].get(feature);
                if (db != 0.0)
                    nonzero++;
                double da = ex.dp[a].get(feature);
                if (da != 0.0)
                    nonzero++;
                double del = derivLoss(delta) * (db - da);
                gradient.adjustOrPutValue(feature, del, del);
            }
            if (log.isDebugEnabled())
                log.debug("+pa=" + ex.p[a] + " pb = " + ex.p[b]);
            lossdata.add(LOSS.L2, loss(delta));
        }
    }
    return nonzero;
}
Also used : PosNegRWExample(edu.cmu.ml.proppr.examples.PosNegRWExample)

Example 12 with PosNegRWExample

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

the class CachingTrainer method train.

@Override
public ParamVector<String, ?> train(SymbolTable<String> masterFeatures, Iterable<String> exampleFile, LearningGraphBuilder builder, ParamVector<String, ?> initialParamVec, int numEpochs) {
    ArrayList<PosNegRWExample> examples = new ArrayList<PosNegRWExample>();
    RWExampleParser parser = new RWExampleParser();
    if (masterFeatures.size() > 0)
        LearningGraphBuilder.setFeatures(masterFeatures);
    int id = 0;
    StatusLogger stattime = new StatusLogger();
    TrainingStatistics total = new TrainingStatistics();
    boolean logged = false;
    for (String s : exampleFile) {
        total.updateReadingStatistics(stattime.sinceLast());
        id++;
        try {
            stattime.tick();
            PosNegRWExample ex = parser.parse(s, builder, masterLearner);
            total.updateParsingStatistics(stattime.sinceLast());
            examples.add(ex);
            if (status.due()) {
                log.info("Parsed " + id + " ...");
                logged = true;
            }
        } catch (GraphFormatException e) {
            log.error("Trouble with #" + id, e);
        }
        stattime.tick();
    }
    if (logged)
        log.info("Total parsed: " + id);
    return trainCached(examples, builder, initialParamVec, numEpochs, total);
}
Also used : StatusLogger(edu.cmu.ml.proppr.util.StatusLogger) PosNegRWExample(edu.cmu.ml.proppr.examples.PosNegRWExample) ArrayList(java.util.ArrayList) GraphFormatException(edu.cmu.ml.proppr.graph.GraphFormatException) RWExampleParser(edu.cmu.ml.proppr.learn.tools.RWExampleParser)

Aggregations

PosNegRWExample (edu.cmu.ml.proppr.examples.PosNegRWExample)12 SRW (edu.cmu.ml.proppr.learn.SRW)4 StatusLogger (edu.cmu.ml.proppr.util.StatusLogger)4 ExecutorService (java.util.concurrent.ExecutorService)4 NamedThreadFactory (edu.cmu.ml.proppr.util.multithreading.NamedThreadFactory)3 TIntDoubleIterator (gnu.trove.iterator.TIntDoubleIterator)3 TIntDoubleHashMap (gnu.trove.map.hash.TIntDoubleHashMap)3 RWExampleParser (edu.cmu.ml.proppr.learn.tools.RWExampleParser)2 StoppingCriterion (edu.cmu.ml.proppr.learn.tools.StoppingCriterion)2 TIntDoubleMap (gnu.trove.map.TIntDoubleMap)2 ArrayList (java.util.ArrayList)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 ArrayLearningGraphBuilder (edu.cmu.ml.proppr.graph.ArrayLearningGraphBuilder)1 GraphFormatException (edu.cmu.ml.proppr.graph.GraphFormatException)1 RegularizationSchedule (edu.cmu.ml.proppr.learn.RegularizationSchedule)1 RegularizeL2 (edu.cmu.ml.proppr.learn.RegularizeL2)1 ModuleConfiguration (edu.cmu.ml.proppr.util.ModuleConfiguration)1 ParsedFile (edu.cmu.ml.proppr.util.ParsedFile)1 SimpleParamVector (edu.cmu.ml.proppr.util.math.SimpleParamVector)1 Callable (java.util.concurrent.Callable)1