Search in sources :

Example 6 with Reduce

use of com.yahoo.tensor.functions.Reduce in project vespa by vespa-engine.

the class RankingExpressionTestCase method testProgrammaticBuilding.

@Test
public void testProgrammaticBuilding() throws ParseException {
    ReferenceNode input = new ReferenceNode("input");
    ReferenceNode constant = new ReferenceNode("constant");
    ArithmeticNode product = new ArithmeticNode(input, ArithmeticOperator.MULTIPLY, constant);
    Reduce sum = new Reduce(new TensorFunctionNode.TensorFunctionExpressionNode(product), Reduce.Aggregator.sum);
    RankingExpression expression = new RankingExpression(new TensorFunctionNode(sum));
    RankingExpression expected = new RankingExpression("sum(input * constant)");
    assertEquals(expected.toString(), expression.toString());
}
Also used : ArithmeticNode(com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode) ReferenceNode(com.yahoo.searchlib.rankingexpression.rule.ReferenceNode) TensorFunctionNode(com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode) Reduce(com.yahoo.tensor.functions.Reduce) Test(org.junit.Test)

Example 7 with Reduce

use of com.yahoo.tensor.functions.Reduce in project vespa by vespa-engine.

the class TensorFunctionBenchmark method dotProduct.

private double dotProduct(Tensor tensor, List<Tensor> tensors) {
    double largest = Double.MIN_VALUE;
    TensorFunction dotProductFunction = new Reduce(new Join(new ConstantTensor(tensor), new VariableTensor("argument"), (a, b) -> a * b), Reduce.Aggregator.sum).toPrimitive();
    MapEvaluationContext context = new MapEvaluationContext();
    for (Tensor tensorElement : tensors) {
        // tensors.size() = 1 for larger tensor
        context.put("argument", tensorElement);
        double dotProduct = dotProductFunction.evaluate(context).asDouble();
        if (dotProduct > largest) {
            largest = dotProduct;
        }
    }
    return largest;
}
Also used : VariableTensor(com.yahoo.tensor.evaluation.VariableTensor) ConstantTensor(com.yahoo.tensor.functions.ConstantTensor) VariableTensor(com.yahoo.tensor.evaluation.VariableTensor) TensorFunction(com.yahoo.tensor.functions.TensorFunction) Join(com.yahoo.tensor.functions.Join) Reduce(com.yahoo.tensor.functions.Reduce) ConstantTensor(com.yahoo.tensor.functions.ConstantTensor) MapEvaluationContext(com.yahoo.tensor.evaluation.MapEvaluationContext)

Example 8 with Reduce

use of com.yahoo.tensor.functions.Reduce in project vespa by vespa-engine.

the class TensorTestCase method dotProduct.

private double dotProduct(Tensor tensor, List<Tensor> tensors) {
    double sum = 0;
    TensorFunction dotProductFunction = new Reduce(new Join(new ConstantTensor(tensor), new VariableTensor("argument"), (a, b) -> a * b), Reduce.Aggregator.sum).toPrimitive();
    MapEvaluationContext context = new MapEvaluationContext();
    for (Tensor tensorElement : tensors) {
        // tensors.size() = 1 for larger tensor
        context.put("argument", tensorElement);
        // System.out.println("Dot product of " + tensor + " and " + tensorElement + ": " + dotProductFunction.evaluate(context).asDouble());
        sum += dotProductFunction.evaluate(context).asDouble();
    }
    return sum;
}
Also used : VariableTensor(com.yahoo.tensor.evaluation.VariableTensor) ConstantTensor(com.yahoo.tensor.functions.ConstantTensor) VariableTensor(com.yahoo.tensor.evaluation.VariableTensor) TensorFunction(com.yahoo.tensor.functions.TensorFunction) Join(com.yahoo.tensor.functions.Join) Reduce(com.yahoo.tensor.functions.Reduce) ConstantTensor(com.yahoo.tensor.functions.ConstantTensor) MapEvaluationContext(com.yahoo.tensor.evaluation.MapEvaluationContext)

Aggregations

Reduce (com.yahoo.tensor.functions.Reduce)8 TensorFunction (com.yahoo.tensor.functions.TensorFunction)6 TensorType (com.yahoo.tensor.TensorType)4 OrderedTensorType (com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType)3 ExpressionNode (com.yahoo.searchlib.rankingexpression.rule.ExpressionNode)3 TensorFunctionNode (com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode)3 GeneratorLambdaFunctionNode (com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode)2 ReferenceNode (com.yahoo.searchlib.rankingexpression.rule.ReferenceNode)2 MapEvaluationContext (com.yahoo.tensor.evaluation.MapEvaluationContext)2 VariableTensor (com.yahoo.tensor.evaluation.VariableTensor)2 ConstantTensor (com.yahoo.tensor.functions.ConstantTensor)2 Generate (com.yahoo.tensor.functions.Generate)2 Join (com.yahoo.tensor.functions.Join)2 ArrayList (java.util.ArrayList)2 DoubleValue (com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue)1 ArithmeticNode (com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode)1 ComparisonNode (com.yahoo.searchlib.rankingexpression.rule.ComparisonNode)1 ConstantNode (com.yahoo.searchlib.rankingexpression.rule.ConstantNode)1 Test (org.junit.Test)1