Search in sources :

Example 1 with MapContext

use of com.yahoo.searchlib.rankingexpression.evaluation.MapContext in project vespa by vespa-engine.

the class TestableTensorFlowModel method contextFrom.

private Context contextFrom(TensorFlowModel result) {
    MapContext context = new MapContext();
    result.largeConstants().forEach((name, tensor) -> context.put("constant(" + name + ")", new TensorValue(tensor)));
    result.smallConstants().forEach((name, tensor) -> context.put("constant(" + name + ")", new TensorValue(tensor)));
    return context;
}
Also used : TensorValue(com.yahoo.searchlib.rankingexpression.evaluation.TensorValue) MapContext(com.yahoo.searchlib.rankingexpression.evaluation.MapContext)

Example 2 with MapContext

use of com.yahoo.searchlib.rankingexpression.evaluation.MapContext in project vespa by vespa-engine.

the class GBDTOptimizerTestCase method testNodeOptimization.

public void testNodeOptimization() throws ParseException {
    String gbdtString = "if (LW_NEWS_SEARCHES_RATIO < 1.72971, 0.0697159, if (LW_USERS < 0.10496, if (SEARCHES < 0.0329127, 0.151257, 0.117501), if (SUGG_OVERLAP < 18.5, 0.0897622, 0.0756903))) + \n" + "if (LW_NEWS_SEARCHES_RATIO < 1.73156, if (NEWS_USERS < 0.0737993, -0.00481646, 0.00110018), if (LW_USERS < 0.0844616, 0.0488919, if (SUGG_OVERLAP < 32.5, 0.0136917, 9.85328E-4))) + \n" + "if (LW_NEWS_SEARCHES_RATIO < 1.74451, -0.00298257, if (LW_USERS < 0.116207, if (SEARCHES < 0.0329127, 0.0676105, 0.0340198), if (NUM_WORDS < 1.5, -8.55514E-5, 0.0112406))) + \n" + "if (LW_NEWS_SEARCHES_RATIO < 1.72995, if (NEWS_USERS < 0.0737993, -0.00407515, 0.00139088), if (LW_USERS == 0.0509035, 0.0439466, if (LW_USERS < 0.325818, 0.0187156, 0.00236949)))";
    RankingExpression gbdt = new RankingExpression(gbdtString);
    // Regular evaluation
    MapContext arguments = new MapContext();
    arguments.put("LW_NEWS_SEARCHES_RATIO", 1d);
    arguments.put("SUGG_OVERLAP", 17d);
    double result1 = gbdt.evaluate(arguments).asDouble();
    arguments.put("LW_NEWS_SEARCHES_RATIO", 2d);
    arguments.put("SUGG_OVERLAP", 20d);
    double result2 = gbdt.evaluate(arguments).asDouble();
    arguments.put("LW_NEWS_SEARCHES_RATIO", 2d);
    arguments.put("SUGG_OVERLAP", 40d);
    double result3 = gbdt.evaluate(arguments).asDouble();
    // Optimized evaluation
    ArrayContext fArguments = new ArrayContext(gbdt);
    ExpressionOptimizer optimizer = new ExpressionOptimizer();
    optimizer.getOptimizer(GBDTForestOptimizer.class).setEnabled(false);
    OptimizationReport report = optimizer.optimize(gbdt, fArguments);
    assertEquals(4, report.getMetric("Optimized GDBT trees"));
    fArguments.put("LW_NEWS_SEARCHES_RATIO", 1d);
    fArguments.put("SUGG_OVERLAP", 17d);
    double oResult1 = gbdt.evaluate(fArguments).asDouble();
    fArguments.put("LW_NEWS_SEARCHES_RATIO", 2d);
    fArguments.put("SUGG_OVERLAP", 20d);
    double oResult2 = gbdt.evaluate(fArguments).asDouble();
    fArguments.put("LW_NEWS_SEARCHES_RATIO", 2d);
    fArguments.put("SUGG_OVERLAP", 40d);
    double oResult3 = gbdt.evaluate(fArguments).asDouble();
    // Assert the same results are produced
    assertEquals(result1, oResult1);
    assertEquals(result2, oResult2);
    assertEquals(result3, oResult3);
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) ArrayContext(com.yahoo.searchlib.rankingexpression.evaluation.ArrayContext) OptimizationReport(com.yahoo.searchlib.rankingexpression.evaluation.OptimizationReport) ExpressionOptimizer(com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer) MapContext(com.yahoo.searchlib.rankingexpression.evaluation.MapContext)

Example 3 with MapContext

use of com.yahoo.searchlib.rankingexpression.evaluation.MapContext in project vespa by vespa-engine.

the class GBDTOptimizerTestCase method testFeatureNamesWithDots.

public void testFeatureNamesWithDots() throws ParseException {
    String gbdtString = "if (a.b < 1.72971, 0.0697159, if (a.b.c < 0.10496, if (a.c < 0.0329127, 0.151257, 0.117501), if (a < 18.5, 0.0897622, 0.0756903))) + 1";
    RankingExpression gbdt = new RankingExpression(gbdtString);
    // Regular evaluation
    MapContext arguments = new MapContext();
    arguments.put("a.b", 1d);
    arguments.put("a.b.c", 0.1d);
    arguments.put("a.c", 0.01d);
    arguments.put("a", 19d);
    double result = gbdt.evaluate(arguments).asDouble();
    // Optimized evaluation
    ArrayContext fArguments = new ArrayContext(gbdt);
    OptimizationReport report = new OptimizationReport();
    new GBDTOptimizer().optimize(gbdt, fArguments, report);
    assertEquals("Optimization result is as expected:\n" + report, 1, report.getMetric("Optimized GDBT trees"));
    fArguments.put("a.b", 1d);
    fArguments.put("a.b.c", 0.1d);
    fArguments.put("a.c", 0.01d);
    fArguments.put("a", 19d);
    double oResult = gbdt.evaluate(fArguments).asDouble();
    // Assert the same results are produced
    assertEquals(result, oResult);
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) ArrayContext(com.yahoo.searchlib.rankingexpression.evaluation.ArrayContext) OptimizationReport(com.yahoo.searchlib.rankingexpression.evaluation.OptimizationReport) MapContext(com.yahoo.searchlib.rankingexpression.evaluation.MapContext)

Example 4 with MapContext

use of com.yahoo.searchlib.rankingexpression.evaluation.MapContext in project vespa by vespa-engine.

the class SimplifierTestCase method testSimplifyComplexExpression.

// A black box test verifying we are not screwing up real expressions
@Test
public void testSimplifyComplexExpression() throws ParseException {
    RankingExpression initial = new RankingExpression("sqrt(if (if (INFERRED * 0.9 < INFERRED, GMP, (1 + 1.1) * INFERRED) < INFERRED * INFERRED - INFERRED, if (GMP < 85.80799542793133 * GMP, INFERRED, if (GMP < GMP, tanh(INFERRED), log(76.89956221113943))), tanh(tanh(INFERRED))) * sqrt(sqrt(GMP + INFERRED)) * GMP ) + 13.5 * (1 - GMP) * pow(GMP * 0.1, 2 + 1.1 * 0)");
    TransformContext c = new TransformContext(Collections.emptyMap());
    RankingExpression simplified = new Simplifier().transform(initial, c);
    Context context = new MapContext();
    context.put("INFERRED", 0.5);
    context.put("GMP", 80.0);
    context.put("value", 50.0);
    assertEquals(initial.evaluate(context), simplified.evaluate(context));
    context.put("INFERRED", 38.0);
    context.put("GMP", 80.0);
    context.put("value", 50.0);
    assertEquals(initial.evaluate(context), simplified.evaluate(context));
    context.put("INFERRED", 38.0);
    context.put("GMP", 90.0);
    context.put("value", 100.0);
    assertEquals(initial.evaluate(context), simplified.evaluate(context));
    context.put("INFERRED", 500.0);
    context.put("GMP", 90.0);
    context.put("value", 100.0);
    assertEquals(initial.evaluate(context), simplified.evaluate(context));
}
Also used : Context(com.yahoo.searchlib.rankingexpression.evaluation.Context) MapContext(com.yahoo.searchlib.rankingexpression.evaluation.MapContext) RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) MapContext(com.yahoo.searchlib.rankingexpression.evaluation.MapContext) Test(org.junit.Test)

Example 5 with MapContext

use of com.yahoo.searchlib.rankingexpression.evaluation.MapContext in project vespa by vespa-engine.

the class TensorConformanceTest method testCase.

private boolean testCase(String test, int count) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readTree(test);
        if (node.has("num_tests")) {
            Assert.assertEquals(node.get("num_tests").asInt(), count);
            return true;
        }
        if (!node.has("expression")) {
            // ignore
            return true;
        }
        String expression = node.get("expression").asText();
        MapContext context = getInput(node.get("inputs"));
        Tensor expect = getTensor(node.get("result").get("expect").asText());
        Tensor result = evaluate(expression, context);
        boolean equals = Tensor.equals(result, expect);
        if (!equals) {
            System.out.println(count + " : Tensors not equal. Result: " + result.toString() + " Expected: " + expect.toString() + " -> expression \"" + expression + "\"");
        }
        return equals;
    } catch (Exception e) {
        System.out.println(count + " : " + e.toString());
    }
    return false;
}
Also used : Tensor(com.yahoo.tensor.Tensor) JsonNode(com.fasterxml.jackson.databind.JsonNode) MapContext(com.yahoo.searchlib.rankingexpression.evaluation.MapContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ParseException(com.yahoo.searchlib.rankingexpression.parser.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

MapContext (com.yahoo.searchlib.rankingexpression.evaluation.MapContext)6 RankingExpression (com.yahoo.searchlib.rankingexpression.RankingExpression)3 ArrayContext (com.yahoo.searchlib.rankingexpression.evaluation.ArrayContext)2 OptimizationReport (com.yahoo.searchlib.rankingexpression.evaluation.OptimizationReport)2 TensorValue (com.yahoo.searchlib.rankingexpression.evaluation.TensorValue)2 Tensor (com.yahoo.tensor.Tensor)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Context (com.yahoo.searchlib.rankingexpression.evaluation.Context)1 ExpressionOptimizer (com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer)1 ParseException (com.yahoo.searchlib.rankingexpression.parser.ParseException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Test (org.junit.Test)1