Search in sources :

Example 36 with TensorType

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

the class Rename method evaluate.

@Override
public <NAMETYPE extends TypeContext.Name> Tensor evaluate(EvaluationContext<NAMETYPE> context) {
    Tensor tensor = argument.evaluate(context);
    TensorType renamedType = type(tensor.type());
    // an array which lists the index of each label in the renamed type
    int[] toIndexes = new int[tensor.type().dimensions().size()];
    for (int i = 0; i < tensor.type().dimensions().size(); i++) {
        String dimensionName = tensor.type().dimensions().get(i).name();
        String newDimensionName = fromToMap.getOrDefault(dimensionName, dimensionName);
        toIndexes[i] = renamedType.indexOfDimension(newDimensionName).get();
    }
    Tensor.Builder builder = Tensor.Builder.of(renamedType);
    for (Iterator<Tensor.Cell> i = tensor.cellIterator(); i.hasNext(); ) {
        Map.Entry<TensorAddress, Double> cell = i.next();
        TensorAddress renamedAddress = rename(cell.getKey(), toIndexes);
        builder.cell(renamedAddress, cell.getValue());
    }
    return builder.build();
}
Also used : TensorAddress(com.yahoo.tensor.TensorAddress) Tensor(com.yahoo.tensor.Tensor) TensorType(com.yahoo.tensor.TensorType) Map(java.util.Map) HashMap(java.util.HashMap)

Example 37 with TensorType

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

the class TensorConverter method toVespaTensor.

public static Tensor toVespaTensor(org.tensorflow.Tensor<?> tfTensor, String dimensionPrefix) {
    TensorType type = toVespaTensorType(tfTensor.shape(), dimensionPrefix);
    Values values = readValuesOf(tfTensor);
    IndexedTensor.BoundBuilder builder = (IndexedTensor.BoundBuilder) Tensor.Builder.of(type);
    for (int i = 0; i < values.size(); i++) builder.cellByDirectIndex(i, values.get(i));
    return builder.build();
}
Also used : TensorType(com.yahoo.tensor.TensorType) IndexedTensor(com.yahoo.tensor.IndexedTensor)

Example 38 with TensorType

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

TensorType (com.yahoo.tensor.TensorType)38 Tensor (com.yahoo.tensor.Tensor)18 ExpressionNode (com.yahoo.searchlib.rankingexpression.rule.ExpressionNode)8 IndexedTensor (com.yahoo.tensor.IndexedTensor)7 MixedTensor (com.yahoo.tensor.MixedTensor)6 TensorAddress (com.yahoo.tensor.TensorAddress)6 Test (org.junit.Test)6 DoubleValue (com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue)4 OrderedTensorType (com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType)4 ConstantNode (com.yahoo.searchlib.rankingexpression.rule.ConstantNode)4 GeneratorLambdaFunctionNode (com.yahoo.searchlib.rankingexpression.rule.GeneratorLambdaFunctionNode)4 Generate (com.yahoo.tensor.functions.Generate)4 List (java.util.List)4 ImmutableList (com.google.common.collect.ImmutableList)3 RankProfile (com.yahoo.searchdefinition.RankProfile)3 RankingExpression (com.yahoo.searchlib.rankingexpression.RankingExpression)3 DimensionSizes (com.yahoo.tensor.DimensionSizes)3 Reduce (com.yahoo.tensor.functions.Reduce)3 TensorFunction (com.yahoo.tensor.functions.TensorFunction)3 ArrayList (java.util.ArrayList)3