Search in sources :

Example 36 with Tensor

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

the class Join method indexedGeneralJoin.

private Tensor indexedGeneralJoin(IndexedTensor a, IndexedTensor b, TensorType joinedType) {
    DimensionSizes joinedSize = joinedSize(joinedType, a, b);
    Tensor.Builder builder = Tensor.Builder.of(joinedType, joinedSize);
    int[] aToIndexes = mapIndexes(a.type(), joinedType);
    int[] bToIndexes = mapIndexes(b.type(), joinedType);
    joinTo(a, b, joinedType, joinedSize, aToIndexes, bToIndexes, false, builder);
    // joinTo(b, a, joinedType, joinedSize, bToIndexes, aToIndexes, true, builder);
    return builder.build();
}
Also used : IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) DimensionSizes(com.yahoo.tensor.DimensionSizes)

Example 37 with Tensor

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

the class Reduce method evaluate.

@Override
public <NAMETYPE extends TypeContext.Name> Tensor evaluate(EvaluationContext<NAMETYPE> context) {
    Tensor argument = this.argument.evaluate(context);
    if (!dimensions.isEmpty() && !argument.type().dimensionNames().containsAll(dimensions))
        throw new IllegalArgumentException("Cannot reduce " + argument + " over dimensions " + dimensions + ": Not all those dimensions are present in this tensor");
    // Special case: Reduce all
    if (dimensions.isEmpty() || dimensions.size() == argument.type().dimensions().size())
        if (argument.type().dimensions().size() == 1 && argument instanceof IndexedTensor)
            return reduceIndexedVector((IndexedTensor) argument);
        else
            return reduceAllGeneral(argument);
    TensorType reducedType = type(argument.type());
    // Reduce cells
    Map<TensorAddress, ValueAggregator> aggregatingCells = new HashMap<>();
    for (Iterator<Tensor.Cell> i = argument.cellIterator(); i.hasNext(); ) {
        Map.Entry<TensorAddress, Double> cell = i.next();
        TensorAddress reducedAddress = reduceDimensions(cell.getKey(), argument.type(), reducedType);
        aggregatingCells.putIfAbsent(reducedAddress, ValueAggregator.ofType(aggregator));
        aggregatingCells.get(reducedAddress).aggregate(cell.getValue());
    }
    Tensor.Builder reducedBuilder = Tensor.Builder.of(reducedType);
    for (Map.Entry<TensorAddress, ValueAggregator> aggregatingCell : aggregatingCells.entrySet()) reducedBuilder.cell(aggregatingCell.getKey(), aggregatingCell.getValue().aggregatedValue());
    return reducedBuilder.build();
}
Also used : TensorAddress(com.yahoo.tensor.TensorAddress) IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) HashMap(java.util.HashMap) TensorType(com.yahoo.tensor.TensorType) HashMap(java.util.HashMap) Map(java.util.Map) IndexedTensor(com.yahoo.tensor.IndexedTensor)

Example 38 with Tensor

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

the class DenseBinaryFormat method decode.

@Override
public Tensor decode(Optional<TensorType> optionalType, GrowableByteBuffer buffer) {
    TensorType type;
    DimensionSizes sizes;
    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);
        sizes = sizesFromType(serializedType);
    } else {
        type = decodeType(buffer);
        sizes = sizesFromType(type);
    }
    Tensor.Builder builder = Tensor.Builder.of(type, sizes);
    decodeCells(sizes, buffer, (IndexedTensor.BoundBuilder) builder);
    return builder.build();
}
Also used : IndexedTensor(com.yahoo.tensor.IndexedTensor) Tensor(com.yahoo.tensor.Tensor) DimensionSizes(com.yahoo.tensor.DimensionSizes) TensorType(com.yahoo.tensor.TensorType) IndexedTensor(com.yahoo.tensor.IndexedTensor)

Example 39 with Tensor

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

the class JsonFormatTestCase method testJsonEncodingOfSparseTensor.

@Test
public void testJsonEncodingOfSparseTensor() {
    Tensor.Builder builder = Tensor.Builder.of(TensorType.fromSpec("tensor(x{},y{})"));
    builder.cell().label("x", "a").label("y", "b").value(2.0);
    builder.cell().label("x", "c").label("y", "d").value(3.0);
    Tensor tensor = builder.build();
    byte[] json = JsonFormat.encode(tensor);
    assertEquals("{\"cells\":[" + "{\"address\":{\"x\":\"a\",\"y\":\"b\"},\"value\":2.0}," + "{\"address\":{\"x\":\"c\",\"y\":\"d\"},\"value\":3.0}" + "]}", new String(json, StandardCharsets.UTF_8));
}
Also used : Tensor(com.yahoo.tensor.Tensor) Test(org.junit.Test)

Example 40 with Tensor

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

the class SerializationTestCase method buildTensor.

private Tensor buildTensor(JsonNode tensor) {
    TensorType type = tensorType(tensor);
    Tensor.Builder builder = Tensor.Builder.of(type);
    tensorCells(tensor, builder);
    return builder.build();
}
Also used : Tensor(com.yahoo.tensor.Tensor) TensorType(com.yahoo.tensor.TensorType)

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