Search in sources :

Example 1 with Value

use of com.yahoo.searchlib.rankingexpression.evaluation.Value in project vespa by vespa-engine.

the class ConstantTensorTransformer method transformConstantReference.

private ExpressionNode transformConstantReference(ReferenceNode node, RankProfileTransformContext context) {
    Value value = context.constants().get(node.getName());
    if (value == null || value.type().rank() == 0) {
        return node;
    }
    TensorValue tensorValue = (TensorValue) value;
    String featureName = CONSTANT + "(" + node.getName() + ")";
    String tensorType = tensorValue.asTensor().type().toString();
    context.rankPropertiesOutput().put(featureName + ".value", tensorValue.toString());
    context.rankPropertiesOutput().put(featureName + ".type", tensorType);
    // TODO: This allows us to reference constant "a" as "a" instead of "constant(a)", but we shouldn't allow that
    return new ReferenceNode(CONSTANT, Arrays.asList(new NameNode(node.getName())), null);
}
Also used : TensorValue(com.yahoo.searchlib.rankingexpression.evaluation.TensorValue) NameNode(com.yahoo.searchlib.rankingexpression.rule.NameNode) ReferenceNode(com.yahoo.searchlib.rankingexpression.rule.ReferenceNode) TensorValue(com.yahoo.searchlib.rankingexpression.evaluation.TensorValue) Value(com.yahoo.searchlib.rankingexpression.evaluation.Value)

Example 2 with Value

use of com.yahoo.searchlib.rankingexpression.evaluation.Value in project vespa by vespa-engine.

the class ConstantDereferencerTestCase method testConstantDereferencer.

@Test
public void testConstantDereferencer() throws ParseException {
    ConstantDereferencer c = new ConstantDereferencer();
    Map<String, Value> constants = new HashMap<>();
    constants.put("a", Value.parse("1.0"));
    constants.put("b", Value.parse("2"));
    constants.put("c", Value.parse("3.5"));
    TransformContext context = new TransformContext(constants);
    assertEquals("1.0 + 2.0 + 3.5", c.transform(new RankingExpression("a + b + c"), context).toString());
    assertEquals("myMacro(1.0,2.0)", c.transform(new RankingExpression("myMacro(a, b)"), context).toString());
}
Also used : RankingExpression(com.yahoo.searchlib.rankingexpression.RankingExpression) HashMap(java.util.HashMap) Value(com.yahoo.searchlib.rankingexpression.evaluation.Value) Test(org.junit.Test)

Example 3 with Value

use of com.yahoo.searchlib.rankingexpression.evaluation.Value in project vespa by vespa-engine.

the class TensorFlowImporter method importConstant.

private static Optional<TensorFunction> importConstant(TensorFlowModel model, TensorFlowOperation operation, SavedModelBundle bundle) {
    String name = operation.vespaName();
    if (model.largeConstants().containsKey(name) || model.smallConstants().containsKey(name)) {
        return operation.function();
    }
    Tensor tensor;
    if (operation.getConstantValue().isPresent()) {
        Value value = operation.getConstantValue().get();
        if (!(value instanceof TensorValue)) {
            // scalar values are inserted directly into the expression
            return operation.function();
        }
        tensor = value.asTensor();
    } else {
        // Here we use the type from the operation, which will have correct dimension names after name resolving
        tensor = TensorConverter.toVespaTensor(readVariable(operation.node().getName(), bundle), operation.type().get());
        operation.setConstantValue(new TensorValue(tensor));
    }
    if (tensor.type().rank() == 0 || tensor.size() <= 1) {
        model.smallConstant(name, tensor);
    } else {
        model.largeConstant(name, tensor);
    }
    return operation.function();
}
Also used : TensorValue(com.yahoo.searchlib.rankingexpression.evaluation.TensorValue) Tensor(com.yahoo.tensor.Tensor) TensorValue(com.yahoo.searchlib.rankingexpression.evaluation.TensorValue) Value(com.yahoo.searchlib.rankingexpression.evaluation.Value)

Example 4 with Value

use of com.yahoo.searchlib.rankingexpression.evaluation.Value in project vespa by vespa-engine.

the class FieldTermMatch method compute.

/**
 * Computes the fieldTermMatch features:
 * <ul>
 *     <li><code>firstPosition</code> - the position of the first occurrence of this query term in this index field</li>
 *     <li><code>occurrences</code> - the position of the first occurrence of this query term in this index field</li>
 * </ul>
 * @param queryTerm the term to return these features for
 * @param field the field value to compute over, assumed to be a space-separated string of tokens
 * @return a features object containing the two values described above
 */
public static Features compute(String queryTerm, String field) {
    Map<String, Value> features = new HashMap<>();
    String[] tokens = field.split(" ");
    int occurrences = 0;
    int firstPosition = 1000000;
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i].equals(queryTerm)) {
            if (occurrences == 0)
                firstPosition = i;
            occurrences++;
        }
    }
    features.put("firstPosition", new DoubleValue(firstPosition));
    features.put("occurrences", new DoubleValue(occurrences));
    return new Features(features);
}
Also used : HashMap(java.util.HashMap) DoubleValue(com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue) DoubleValue(com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue) Value(com.yahoo.searchlib.rankingexpression.evaluation.Value)

Example 5 with Value

use of com.yahoo.searchlib.rankingexpression.evaluation.Value in project vespa by vespa-engine.

the class RankingExpressionWithTensorFlowTestCase method assertSmallConstant.

private void assertSmallConstant(String name, TensorType type, RankProfileSearchFixture search) {
    Value value = search.rankProfile("my_profile").getConstants().get(name);
    assertNotNull(value);
    assertEquals(type, value.type());
}
Also used : Value(com.yahoo.searchlib.rankingexpression.evaluation.Value)

Aggregations

Value (com.yahoo.searchlib.rankingexpression.evaluation.Value)9 DoubleValue (com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue)4 HashMap (java.util.HashMap)3 TensorValue (com.yahoo.searchlib.rankingexpression.evaluation.TensorValue)2 RankingExpression (com.yahoo.searchlib.rankingexpression.RankingExpression)1 ArithmeticNode (com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode)1 ConstantNode (com.yahoo.searchlib.rankingexpression.rule.ConstantNode)1 ExpressionNode (com.yahoo.searchlib.rankingexpression.rule.ExpressionNode)1 NameNode (com.yahoo.searchlib.rankingexpression.rule.NameNode)1 ReferenceNode (com.yahoo.searchlib.rankingexpression.rule.ReferenceNode)1 Tensor (com.yahoo.tensor.Tensor)1 Test (org.junit.Test)1