Search in sources :

Example 6 with Tensor

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

the class DenseBinaryFormatTestCase method assertSerialization.

private void assertSerialization(Tensor tensor, TensorType expectedType) {
    byte[] encodedTensor = TypedBinaryFormat.encode(tensor);
    Tensor decodedTensor = TypedBinaryFormat.decode(Optional.of(expectedType), GrowableByteBuffer.wrap(encodedTensor));
    assertEquals(tensor, decodedTensor);
}
Also used : Tensor(com.yahoo.tensor.Tensor)

Example 7 with Tensor

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

the class MixedBinaryFormatTestCase method testTwoMappedSerialization.

@Test
public void testTwoMappedSerialization() {
    TensorType type = new TensorType.Builder().mapped("x").mapped("y").build();
    Tensor tensor = MixedTensor.Builder.of(type).cell().label("x", "0").label("y", "0").value(1).cell().label("x", "0").label("y", "1").value(2).cell().label("x", "1").label("y", "0").value(4).cell().label("x", "1").label("y", "1").value(5).cell().label("x", "1").label("y", "2").value(6).build();
    assertSerialization(tensor);
}
Also used : MixedTensor(com.yahoo.tensor.MixedTensor) Tensor(com.yahoo.tensor.Tensor) TensorType(com.yahoo.tensor.TensorType) Test(org.junit.Test)

Example 8 with Tensor

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

the class SparseBinaryFormatTestCase method assertSerialization.

private void assertSerialization(Tensor tensor, TensorType expectedType) {
    byte[] encodedTensor = TypedBinaryFormat.encode(tensor);
    Tensor decodedTensor = TypedBinaryFormat.decode(Optional.of(expectedType), GrowableByteBuffer.wrap(encodedTensor));
    assertEquals(tensor, decodedTensor);
}
Also used : Tensor(com.yahoo.tensor.Tensor)

Example 9 with Tensor

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

the class Map method evaluate.

@Override
public <NAMETYPE extends TypeContext.Name> Tensor evaluate(EvaluationContext<NAMETYPE> context) {
    Tensor argument = argument().evaluate(context);
    Tensor.Builder builder = Tensor.Builder.of(argument.type());
    for (Iterator<Tensor.Cell> i = argument.cellIterator(); i.hasNext(); ) {
        java.util.Map.Entry<TensorAddress, Double> cell = i.next();
        builder.cell(cell.getKey(), mapper.applyAsDouble(cell.getValue()));
    }
    return builder.build();
}
Also used : TensorAddress(com.yahoo.tensor.TensorAddress) Tensor(com.yahoo.tensor.Tensor)

Example 10 with Tensor

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

the class JsonFormat method encode.

/**
 * Serialize the given tensor into JSON format
 */
public static byte[] encode(Tensor tensor) {
    Slime slime = new Slime();
    Cursor root = slime.setObject();
    Cursor cellsArray = root.setArray("cells");
    for (Iterator<Tensor.Cell> i = tensor.cellIterator(); i.hasNext(); ) {
        Tensor.Cell cell = i.next();
        Cursor cellObject = cellsArray.addObject();
        encodeAddress(tensor.type(), cell.getKey(), cellObject.setObject("address"));
        cellObject.setDouble("value", cell.getValue());
    }
    return com.yahoo.slime.JsonFormat.toJsonBytes(slime);
}
Also used : Tensor(com.yahoo.tensor.Tensor) Slime(com.yahoo.slime.Slime) Cursor(com.yahoo.slime.Cursor)

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