Search in sources :

Example 11 with Tensor

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

the class SparseBinaryFormat method decode.

@Override
public Tensor decode(Optional<TensorType> optionalType, GrowableByteBuffer buffer) {
    TensorType type;
    if (optionalType.isPresent()) {
        type = optionalType.get();
        TensorType serializedType = decodeType(buffer);
        if (!serializedType.isAssignableTo(type))
            throw new IllegalArgumentException("Type/instance mismatch: A tensor of type " + serializedType + " cannot be assigned to type " + type);
    } else {
        type = decodeType(buffer);
    }
    Tensor.Builder builder = Tensor.Builder.of(type);
    decodeCells(buffer, builder, type);
    return builder.build();
}
Also used : Tensor(com.yahoo.tensor.Tensor) TensorType(com.yahoo.tensor.TensorType)

Example 12 with Tensor

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

the class Reshape method lazyGetType.

@Override
protected OrderedTensorType lazyGetType() {
    if (!allInputTypesPresent(2)) {
        return null;
    }
    TensorFlowOperation newShape = inputs.get(1);
    if (!newShape.getConstantValue().isPresent()) {
        throw new IllegalArgumentException("Reshape in " + node.getName() + ": " + "shape input must be a constant.");
    }
    Tensor shape = newShape.getConstantValue().get().asTensor();
    OrderedTensorType inputType = inputs.get(0).type().get();
    OrderedTensorType.Builder outputTypeBuilder = new OrderedTensorType.Builder(node);
    int dimensionIndex = 0;
    for (Iterator<Tensor.Cell> cellIterator = shape.cellIterator(); cellIterator.hasNext(); ) {
        Tensor.Cell cell = cellIterator.next();
        int size = cell.getValue().intValue();
        if (size < 0) {
            size = -1 * (int) shape.reduce(Reduce.Aggregator.prod).asDouble() / tensorSize(inputType.type()).intValue();
        }
        outputTypeBuilder.add(TensorType.Dimension.indexed(String.format("%s_%d", vespaName(), dimensionIndex), size));
        dimensionIndex++;
    }
    return outputTypeBuilder.build();
}
Also used : Tensor(com.yahoo.tensor.Tensor) OrderedTensorType(com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType)

Example 13 with Tensor

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

the class EvaluationTester method assertEvaluates.

// TODO: Test both bound and unbound indexed
public RankingExpression assertEvaluates(String expectedTensor, String expressionString, boolean mappedTensors, String... tensorArgumentStrings) {
    MapContext context = defaultContext.thawedCopy();
    int argumentIndex = 0;
    for (String argumentString : tensorArgumentStrings) {
        Tensor argument;
        if (// explicitly decided type
        argumentString.startsWith("tensor("))
            argument = Tensor.from(argumentString);
        else
            // use mappedTensors+dimensions in tensor to decide type
            argument = Tensor.from(typeFrom(argumentString, mappedTensors), argumentString);
        context.put("tensor" + (argumentIndex++), new TensorValue(argument));
    }
    return assertEvaluates(new TensorValue(Tensor.from(expectedTensor)), expressionString, context, mappedTensors ? "Mapped tensors" : "Indexed tensors");
}
Also used : Tensor(com.yahoo.tensor.Tensor)

Example 14 with Tensor

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

the class JsonSerializationHelper method serializeTensorField.

public static void serializeTensorField(JsonGenerator generator, FieldBase field, TensorFieldValue value) {
    wrapIOException(() -> {
        fieldNameIfNotNull(generator, field);
        generator.writeStartObject();
        if (value.getTensor().isPresent()) {
            Tensor tensor = value.getTensor().get();
            serializeTensorCells(generator, tensor);
        }
        generator.writeEndObject();
    });
}
Also used : Tensor(com.yahoo.tensor.Tensor)

Example 15 with Tensor

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

the class JsonReaderTestCase method testParsingOfMappedTensorWithCells.

@Test
public void testParsingOfMappedTensorWithCells() {
    Tensor tensor = assertMappedTensorField("{{x:a,y:b}:2.0,{x:c,y:b}:3.0}}", createPutWithMappedTensor("{ " + "  \"cells\": [ " + "    { \"address\": { \"x\": \"a\", \"y\": \"b\" }, " + "      \"value\": 2.0 }, " + "    { \"address\": { \"x\": \"c\", \"y\": \"b\" }, " + "      \"value\": 3.0 } " + "  ]" + "}"));
    // any functional instance is fine
    assertTrue(tensor instanceof MappedTensor);
}
Also used : Tensor(com.yahoo.tensor.Tensor) IndexedTensor(com.yahoo.tensor.IndexedTensor) MappedTensor(com.yahoo.tensor.MappedTensor) MappedTensor(com.yahoo.tensor.MappedTensor) Test(org.junit.Test)

Aggregations

Tensor (com.yahoo.tensor.Tensor)58 Test (org.junit.Test)26 TensorType (com.yahoo.tensor.TensorType)17 IndexedTensor (com.yahoo.tensor.IndexedTensor)10 TensorAddress (com.yahoo.tensor.TensorAddress)7 MixedTensor (com.yahoo.tensor.MixedTensor)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 MapContext (com.yahoo.searchlib.rankingexpression.evaluation.MapContext)3 TensorValue (com.yahoo.searchlib.rankingexpression.evaluation.TensorValue)3 OrderedTensorType (com.yahoo.searchlib.rankingexpression.integration.tensorflow.importer.OrderedTensorType)3 DimensionSizes (com.yahoo.tensor.DimensionSizes)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ImmutableList (com.google.common.collect.ImmutableList)2 MappedTensor (com.yahoo.tensor.MappedTensor)2 IOException (java.io.IOException)2 List (java.util.List)2 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)1 Path (com.yahoo.path.Path)1