Search in sources :

Example 1 with MapEvaluationContext

use of com.yahoo.tensor.evaluation.MapEvaluationContext 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 2 with MapEvaluationContext

use of com.yahoo.tensor.evaluation.MapEvaluationContext in project vespa by vespa-engine.

the class ConcatTestCase method assertConcat.

private void assertConcat(String expectedType, String expected, Tensor a, Tensor b, String dimension) {
    Tensor expectedAsTensor = Tensor.from(expected);
    TensorType inferredType = new Concat(new ConstantTensor(a), new ConstantTensor(b), dimension).type(new MapEvaluationContext());
    Tensor result = a.concat(b, dimension);
    if (expectedType != null)
        assertEquals(TensorType.fromSpec(expectedType), inferredType);
    else
        assertEquals(expectedAsTensor.type(), inferredType);
    assertEquals(expectedAsTensor, result);
}
Also used : Tensor(com.yahoo.tensor.Tensor) TensorType(com.yahoo.tensor.TensorType) MapEvaluationContext(com.yahoo.tensor.evaluation.MapEvaluationContext)

Example 3 with MapEvaluationContext

use of com.yahoo.tensor.evaluation.MapEvaluationContext 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

MapEvaluationContext (com.yahoo.tensor.evaluation.MapEvaluationContext)3 VariableTensor (com.yahoo.tensor.evaluation.VariableTensor)2 ConstantTensor (com.yahoo.tensor.functions.ConstantTensor)2 Join (com.yahoo.tensor.functions.Join)2 Reduce (com.yahoo.tensor.functions.Reduce)2 TensorFunction (com.yahoo.tensor.functions.TensorFunction)2 Tensor (com.yahoo.tensor.Tensor)1 TensorType (com.yahoo.tensor.TensorType)1