Search in sources :

Example 11 with TensorFunction

use of com.yahoo.tensor.functions.TensorFunction 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 12 with TensorFunction

use of com.yahoo.tensor.functions.TensorFunction 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

TensorFunction (com.yahoo.tensor.functions.TensorFunction)12 OrderedTensorType (com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType)6 Reduce (com.yahoo.tensor.functions.Reduce)6 TensorType (com.yahoo.tensor.TensorType)4 ExpressionNode (com.yahoo.searchlib.rankingexpression.rule.ExpressionNode)3 VariableTensor (com.yahoo.tensor.evaluation.VariableTensor)3 Rename (com.yahoo.tensor.functions.Rename)3 ArrayList (java.util.ArrayList)3 GeneratorLambdaFunctionNode (com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode)2 TensorFunctionNode (com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode)2 MapEvaluationContext (com.yahoo.tensor.evaluation.MapEvaluationContext)2 ConstantTensor (com.yahoo.tensor.functions.ConstantTensor)2 Generate (com.yahoo.tensor.functions.Generate)2 Join (com.yahoo.tensor.functions.Join)2 RankingExpression (com.yahoo.searchlib.rankingexpression.RankingExpression)1 DoubleValue (com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue)1 ParseException (com.yahoo.searchlib.rankingexpression.parser.ParseException)1 ComparisonNode (com.yahoo.searchlib.rankingexpression.rule.ComparisonNode)1 CompositeNode (com.yahoo.searchlib.rankingexpression.rule.CompositeNode)1 ConstantNode (com.yahoo.searchlib.rankingexpression.rule.ConstantNode)1