Search in sources :

Example 1 with GeneratorLambdaFunctionNode

use of com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode in project vespa by vespa-engine.

the class TensorFlowFeatureConverter method expandBatchDimensionsAtOutput.

/**
 * If batch dimensions have been reduced away above, bring them back here
 * for any following computation of the tensor.
 * Todo: determine when this is not necessary!
 */
private ExpressionNode expandBatchDimensionsAtOutput(ExpressionNode node, TensorType before, TensorType after) {
    if (after.equals(before)) {
        return node;
    }
    TensorType.Builder typeBuilder = new TensorType.Builder();
    for (TensorType.Dimension dimension : before.dimensions()) {
        if (dimension.size().orElse(-1L) == 1 && !after.dimensionNames().contains(dimension.name())) {
            typeBuilder.indexed(dimension.name(), 1);
        }
    }
    TensorType expandDimensionsType = typeBuilder.build();
    if (expandDimensionsType.dimensions().size() > 0) {
        ExpressionNode generatedExpression = new ConstantNode(new DoubleValue(1.0));
        Generate generatedFunction = new Generate(expandDimensionsType, new GeneratorLambdaFunctionNode(expandDimensionsType, generatedExpression).asLongListToDoubleOperator());
        Join expand = new Join(TensorFunctionNode.wrapArgument(node), generatedFunction, ScalarFunctions.multiply());
        return new TensorFunctionNode(expand);
    }
    return node;
}
Also used : GeneratorLambdaFunctionNode(com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode) ConstantNode(com.yahoo.searchlib.rankingexpression.rule.ConstantNode) DoubleValue(com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue) TensorFunctionNode(com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) Generate(com.yahoo.tensor.functions.Generate) Join(com.yahoo.tensor.functions.Join) TensorType(com.yahoo.tensor.TensorType)

Example 2 with GeneratorLambdaFunctionNode

use of com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode in project vespa by vespa-engine.

the class Reshape method reshape.

public static TensorFunction reshape(TensorFunction inputFunction, TensorType inputType, TensorType outputType) {
    if (!tensorSize(inputType).equals(tensorSize(outputType))) {
        throw new IllegalArgumentException("New and old shape of tensor must have the same size when reshaping");
    }
    // Conceptually, reshaping consists on unrolling a tensor to an array using the dimension order,
    // then use the dimension order of the new shape to roll back into a tensor.
    // Here we create a transformation tensor that is multiplied with the from tensor to map into
    // the new shape. We have to introduce temporary dimension names and rename back if dimension names
    // in the new and old tensor type overlap.
    ExpressionNode unrollFrom = unrollTensorExpression(inputType);
    ExpressionNode unrollTo = unrollTensorExpression(outputType);
    ExpressionNode transformExpression = new ComparisonNode(unrollFrom, TruthOperator.EQUAL, unrollTo);
    TensorType transformationType = new TensorType.Builder(inputType, outputType).build();
    Generate transformTensor = new Generate(transformationType, new GeneratorLambdaFunctionNode(transformationType, transformExpression).asLongListToDoubleOperator());
    TensorFunction outputFunction = new Reduce(new com.yahoo.tensor.functions.Join(inputFunction, transformTensor, ScalarFunctions.multiply()), Reduce.Aggregator.sum, inputType.dimensions().stream().map(TensorType.Dimension::name).collect(Collectors.toList()));
    return outputFunction;
}
Also used : GeneratorLambdaFunctionNode(com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode) OrderedTensorType(com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType) TensorType(com.yahoo.tensor.TensorType) Reduce(com.yahoo.tensor.functions.Reduce) ComparisonNode(com.yahoo.searchlib.rankingexpression.rule.ComparisonNode) TensorFunction(com.yahoo.tensor.functions.TensorFunction) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) Generate(com.yahoo.tensor.functions.Generate)

Example 3 with GeneratorLambdaFunctionNode

use of com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode in project vespa by vespa-engine.

the class Mean method lazyGetFunction.

// todo: optimization: if keepDims and one reduce dimension that has size 1: same as identity.
@Override
protected TensorFunction lazyGetFunction() {
    if (!allInputTypesPresent(2)) {
        return null;
    }
    TensorFunction inputFunction = inputs.get(0).function().get();
    TensorFunction output = new Reduce(inputFunction, Reduce.Aggregator.avg, reduceDimensions);
    if (shouldKeepDimensions()) {
        // multiply with a generated tensor created from the reduced dimensions
        TensorType.Builder typeBuilder = new TensorType.Builder();
        for (String name : reduceDimensions) {
            typeBuilder.indexed(name, 1);
        }
        TensorType generatedType = typeBuilder.build();
        ExpressionNode generatedExpression = new ConstantNode(new DoubleValue(1));
        Generate generatedFunction = new Generate(generatedType, new GeneratorLambdaFunctionNode(generatedType, generatedExpression).asLongListToDoubleOperator());
        output = new com.yahoo.tensor.functions.Join(output, generatedFunction, ScalarFunctions.multiply());
    }
    return output;
}
Also used : GeneratorLambdaFunctionNode(com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode) OrderedTensorType(com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType) TensorType(com.yahoo.tensor.TensorType) Reduce(com.yahoo.tensor.functions.Reduce) ConstantNode(com.yahoo.searchlib.rankingexpression.rule.ConstantNode) TensorFunction(com.yahoo.tensor.functions.TensorFunction) DoubleValue(com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) Generate(com.yahoo.tensor.functions.Generate)

Example 4 with GeneratorLambdaFunctionNode

use of com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode in project vespa by vespa-engine.

the class ExpandDims method lazyGetFunction.

@Override
protected TensorFunction lazyGetFunction() {
    if (!allInputFunctionsPresent(2)) {
        return null;
    }
    // multiply with a generated tensor created from the reduced dimensions
    TensorType.Builder typeBuilder = new TensorType.Builder();
    for (String name : expandDimensions) {
        typeBuilder.indexed(name, 1);
    }
    TensorType generatedType = typeBuilder.build();
    ExpressionNode generatedExpression = new ConstantNode(new DoubleValue(1));
    Generate generatedFunction = new Generate(generatedType, new GeneratorLambdaFunctionNode(generatedType, generatedExpression).asLongListToDoubleOperator());
    return new com.yahoo.tensor.functions.Join(inputs().get(0).function().get(), generatedFunction, ScalarFunctions.multiply());
}
Also used : GeneratorLambdaFunctionNode(com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode) ConstantNode(com.yahoo.searchlib.rankingexpression.rule.ConstantNode) DoubleValue(com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue) ExpressionNode(com.yahoo.searchlib.rankingexpression.rule.ExpressionNode) Generate(com.yahoo.tensor.functions.Generate) OrderedTensorType(com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType) TensorType(com.yahoo.tensor.TensorType)

Aggregations

ExpressionNode (com.yahoo.searchlib.rankingexpression.rule.ExpressionNode)4 GeneratorLambdaFunctionNode (com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode)4 TensorType (com.yahoo.tensor.TensorType)4 Generate (com.yahoo.tensor.functions.Generate)4 DoubleValue (com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue)3 OrderedTensorType (com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType)3 ConstantNode (com.yahoo.searchlib.rankingexpression.rule.ConstantNode)3 Reduce (com.yahoo.tensor.functions.Reduce)2 TensorFunction (com.yahoo.tensor.functions.TensorFunction)2 ComparisonNode (com.yahoo.searchlib.rankingexpression.rule.ComparisonNode)1 TensorFunctionNode (com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode)1 Join (com.yahoo.tensor.functions.Join)1