Search in sources :

Example 11 with Feature

use of edu.cmu.ml.proppr.prove.wam.Feature in project ProPPR by TeamCohen.

the class InnerProductWeighterTest method test.

@Test
public void test() {
    BasicConfigurator.configure();
    Logger.getRootLogger().setLevel(Level.INFO);
    HashMap<Feature, Double> weights = new HashMap<Feature, Double>();
    weights.put(new Feature("feathers"), 0.5);
    weights.put(new Feature("scales"), 0.3);
    weights.put(new Feature("fur"), 0.7);
    FeatureDictWeighter w = new InnerProductWeighter(weights);
    Feature ng = new Feature("hair");
    HashMap<Feature, Double> featureDict = new HashMap<Feature, Double>();
    featureDict.put(ng, 0.9);
    featureDict.putAll(weights);
    assertFalse("Should start empty!", w.unknownFeatures.contains(ng));
    for (Map.Entry<Feature, Double> e : featureDict.entrySet()) {
        e.setValue(e.getValue() - Math.random() / 10);
    }
    w.w(featureDict);
    assertTrue("Wasn't added!", w.unknownFeatures.contains(ng));
}
Also used : HashMap(java.util.HashMap) Feature(edu.cmu.ml.proppr.prove.wam.Feature) Map(java.util.Map) HashMap(java.util.HashMap) InnerProductWeighter(edu.cmu.ml.proppr.prove.InnerProductWeighter) Test(org.junit.Test)

Example 12 with Feature

use of edu.cmu.ml.proppr.prove.wam.Feature in project ProPPR by TeamCohen.

the class ProverTestTemplate method testProveState.

@Test
public void testProveState() throws LogicProgramException {
    log.info("testProveState");
    FeatureDictWeighter w = new InnerProductWeighter();
    SymbolTable<Feature> featureTab = new SimpleSymbolTable<Feature>();
    int milk = featureTab.getId(new Feature("milk"));
    w.put(featureTab.getSymbol(milk), 2);
    prover.setWeighter(w);
    ProofGraph pg = prover.makeProofGraph(new InferenceExample(Query.parse("isa(elsie,X)"), null, null), apr, featureTab, lpMilk, fMilk);
    //("isa","elsie","X"));
    Map<State, Double> dist = prover.prove(pg, new StatusLogger());
    double query = 0.0;
    double platypus = 0.0;
    double others = 0.0;
    double all = 0.0;
    for (Map.Entry<State, Double> s : dist.entrySet()) {
        Query q = pg.fill(s.getKey());
        String arg2 = q.getRhs()[0].getArg(1).getName();
        if ("platypus".equals(arg2)) {
            platypus = Math.max(platypus, s.getValue());
        } else if ("X1".equals(arg2)) {
            query = Math.max(query, s.getValue());
        } else {
            others = Math.max(others, s.getValue());
        }
        System.out.println(q + "\t" + s.getValue());
        all += s.getValue();
    }
    System.out.println();
    System.out.println("query    weight: " + query);
    System.out.println("platypus weight: " + platypus);
    System.out.println("others   weight: " + others);
    //		assertTrue("query should retain most weight",query > Math.max(platypus,others));
    assertTrue("milk-featured paths should score higher than others", platypus > others);
    assertEquals("Total weight of all states should be around 1.0", 1.0, all, 10 * this.apr.epsilon);
    assertEquals("Known features", 1, prover.weighter.numKnownFeatures);
    assertEquals("Unknown features", 5, prover.weighter.numUnknownFeatures);
}
Also used : StatusLogger(edu.cmu.ml.proppr.util.StatusLogger) Query(edu.cmu.ml.proppr.prove.wam.Query) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph) ProofGraph(edu.cmu.ml.proppr.prove.wam.ProofGraph) Feature(edu.cmu.ml.proppr.prove.wam.Feature) InferenceExample(edu.cmu.ml.proppr.examples.InferenceExample) FeatureDictWeighter(edu.cmu.ml.proppr.prove.FeatureDictWeighter) SimpleSymbolTable(edu.cmu.ml.proppr.util.SimpleSymbolTable) State(edu.cmu.ml.proppr.prove.wam.State) Map(java.util.Map) InnerProductWeighter(edu.cmu.ml.proppr.prove.InnerProductWeighter) Test(org.junit.Test)

Example 13 with Feature

use of edu.cmu.ml.proppr.prove.wam.Feature in project ProPPR by TeamCohen.

the class QueryAnswerer method addParams.

public void addParams(Prover<P> prover, ParamVector<String, ?> params, SquashingFunction<Goal> f) {
    InnerProductWeighter w = InnerProductWeighter.fromParamVec(params, f);
    prover.setWeighter(w);
    for (Feature g : w.getWeights().keySet()) this.featureTable.insert(g);
}
Also used : Feature(edu.cmu.ml.proppr.prove.wam.Feature) InnerProductWeighter(edu.cmu.ml.proppr.prove.InnerProductWeighter)

Example 14 with Feature

use of edu.cmu.ml.proppr.prove.wam.Feature in project ProPPR by TeamCohen.

the class LightweightStateGraph method getOutlinks.

/** Return the neighbors of node u. */
public List<Outlink> getOutlinks(State u) {
    // wwc: why do we need to recompute these each time?
    List<Outlink> result = new ArrayList<Outlink>();
    for (State v : near(u)) {
        Map<Feature, Double> fd = getFeatures(u, v);
        result.add(new Outlink(fd, v));
    }
    return result;
}
Also used : Outlink(edu.cmu.ml.proppr.prove.wam.Outlink) State(edu.cmu.ml.proppr.prove.wam.State) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) Feature(edu.cmu.ml.proppr.prove.wam.Feature)

Example 15 with Feature

use of edu.cmu.ml.proppr.prove.wam.Feature in project ProPPR by TeamCohen.

the class LightweightStateGraph method getFeatures.

public Map<Feature, Double> getFeatures(State u, State v) {
    int ui = this.nodeTab.getId(u), vi = this.nodeTab.getId(v);
    if (!edgeFeatureDict.containsKey(ui))
        return DEFAULT_FD;
    TIntObjectHashMap<TIntDoubleHashMap> fu = edgeFeatureDict.get(ui);
    if (!fu.containsKey(vi))
        return DEFAULT_FD;
    TIntDoubleHashMap fuvi = fu.get(vi);
    final HashMap<Feature, Double> ret = new HashMap<Feature, Double>();
    fuvi.forEachEntry(new TIntDoubleProcedure() {

        @Override
        public boolean execute(int fi, double wt) {
            ret.put(featureTab.getSymbol(fi), wt);
            return true;
        }
    });
    return ret;
}
Also used : TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) HashMap(java.util.HashMap) TIntDoubleHashMap(gnu.trove.map.hash.TIntDoubleHashMap) TIntDoubleHashMap(gnu.trove.map.hash.TIntDoubleHashMap) TIntDoubleProcedure(gnu.trove.procedure.TIntDoubleProcedure) Feature(edu.cmu.ml.proppr.prove.wam.Feature)

Aggregations

Feature (edu.cmu.ml.proppr.prove.wam.Feature)17 InferenceExample (edu.cmu.ml.proppr.examples.InferenceExample)7 ProofGraph (edu.cmu.ml.proppr.prove.wam.ProofGraph)7 Query (edu.cmu.ml.proppr.prove.wam.Query)7 StateProofGraph (edu.cmu.ml.proppr.prove.wam.StateProofGraph)7 WamProgram (edu.cmu.ml.proppr.prove.wam.WamProgram)7 Test (org.junit.Test)7 GroundedExample (edu.cmu.ml.proppr.examples.GroundedExample)6 DprProver (edu.cmu.ml.proppr.prove.DprProver)6 Prover (edu.cmu.ml.proppr.prove.Prover)6 Grounder (edu.cmu.ml.proppr.Grounder)5 Outlink (edu.cmu.ml.proppr.prove.wam.Outlink)5 State (edu.cmu.ml.proppr.prove.wam.State)5 APROptions (edu.cmu.ml.proppr.util.APROptions)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 InnerProductWeighter (edu.cmu.ml.proppr.prove.InnerProductWeighter)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)3 ArrayList (java.util.ArrayList)3 TIntDoubleHashMap (gnu.trove.map.hash.TIntDoubleHashMap)2