Search in sources :

Example 41 with Tensor

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

the class SerializationTestCase method testSerialization.

@Test
public void testSerialization() throws IOException {
    for (String test : tests) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readTree(test);
        if (node.has("tensor") && node.has("binary")) {
            System.out.println("Running test: " + test);
            Tensor tensor = buildTensor(node.get("tensor"));
            String spec = getSpec(node.get("tensor"));
            byte[] encodedTensor = TypedBinaryFormat.encode(tensor);
            boolean serializedToABinaryRepresentation = false;
            JsonNode binaryNode = node.get("binary");
            for (int i = 0; i < binaryNode.size(); ++i) {
                byte[] bin = getBytes(binaryNode.get(i).asText());
                Tensor decodedTensor = TypedBinaryFormat.decode(Optional.empty(), GrowableByteBuffer.wrap(bin));
                if (spec.equalsIgnoreCase("double")) {
                    assertEquals(tensor.asDouble(), decodedTensor.asDouble(), 1e-6);
                } else {
                    assertEquals(tensor, decodedTensor);
                }
                if (Arrays.equals(encodedTensor, bin)) {
                    serializedToABinaryRepresentation = true;
                }
            }
            assertTrue("Tensor did not serialize to one of the given representations", serializedToABinaryRepresentation);
        }
    }
}
Also used : Tensor(com.yahoo.tensor.Tensor) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 42 with Tensor

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

the class ConcatTestCase method testUnequalEqualSizesDifferentDimension.

@Test
public void testUnequalEqualSizesDifferentDimension() {
    Tensor a = Tensor.from("tensor(x[2]):{ {x:0}:1, {x:1}:2 }");
    Tensor b = Tensor.from("tensor(y[3]):{ {y:0}:4, {y:1}:5, {y:2}:6 }");
    assertConcat("tensor(x[3],y[3]):{{x:0,y:0}:1.0,{x:0,y:1}:1.0,{x:0,y:2}:1.0,{x:1,y:0}:2.0,{x:1,y:1}:2.0,{x:1,y:2}:2.0,{x:2,y:0}:4.0,{x:2,y:1}:5.0,{x:2,y:2}:6.0}", a, b, "x");
    assertConcat("tensor(x[2],y[4]):{{x:0,y:0}:1.0,{x:0,y:1}:4.0,{x:0,y:2}:5.0,{x:0,y:3}:6.0,{x:1,y:0}:2.0,{x:1,y:1}:4.0,{x:1,y:2}:5.0,{x:1,y:3}:6.0}", a, b, "y");
    assertConcat("tensor(x[2],y[3],z[2]):{{x:0,y:0,z:0}:1.0,{x:0,y:0,z:1}:4.0,{x:0,y:1,z:0}:1.0,{x:0,y:1,z:1}:5.0,{x:0,y:2,z:0}:1.0,{x:0,y:2,z:1}:6.0,{x:1,y:0,z:0}:2.0,{x:1,y:0,z:1}:4.0,{x:1,y:1,z:0}:2.0,{x:1,y:1,z:1}:5.0,{x:1,y:2,z:0}:2.0,{x:1,y:2,z:1}:6.0}", a, b, "z");
}
Also used : Tensor(com.yahoo.tensor.Tensor) Test(org.junit.Test)

Example 43 with Tensor

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

the class ConcatTestCase method assertConcat.

private void assertConcat(String expectedType, String expected, Tensor a, Tensor b, String dimension) {
    Tensor expectedAsTensor = Tensor.from(expected);
    TensorType inferredType = new Concat(new ConstantTensor(a), new ConstantTensor(b), dimension).type(new MapEvaluationContext());
    Tensor result = a.concat(b, dimension);
    if (expectedType != null)
        assertEquals(TensorType.fromSpec(expectedType), inferredType);
    else
        assertEquals(expectedAsTensor.type(), inferredType);
    assertEquals(expectedAsTensor, result);
}
Also used : Tensor(com.yahoo.tensor.Tensor) TensorType(com.yahoo.tensor.TensorType) MapEvaluationContext(com.yahoo.tensor.evaluation.MapEvaluationContext)

Example 44 with Tensor

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

the class ConcatTestCase method testUnequalSizesSameDimensionUnbound.

@Test
public void testUnequalSizesSameDimensionUnbound() {
    Tensor a = Tensor.from("tensor(x[]):{ {x:0}:1, {x:1}:2 }");
    Tensor b = Tensor.from("tensor(x[]):{ {x:0}:4, {x:1}:5, {x:2}:6 }");
    assertConcat("tensor(x[])", "tensor(x[5]):{ {x:0}:1, {x:1}:2, {x:2}:4, {x:3}:5, {x:4}:6 }", a, b, "x");
    assertConcat("tensor(x[],y[2])", "tensor(x[2],y[2]):{ {x:0,y:0}:1, {x:1,y:0}:2, {x:0,y:1}:4, {x:1,y:1}:5 }", a, b, "y");
}
Also used : Tensor(com.yahoo.tensor.Tensor) Test(org.junit.Test)

Example 45 with Tensor

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

the class ConcatTestCase method testConcatNumbers.

@Test
public void testConcatNumbers() {
    Tensor a = Tensor.from("{1}");
    Tensor b = Tensor.from("{2}");
    assertConcat("tensor(x[2]):{ {x:0}:1, {x:1}:2 }", a, b, "x");
    assertConcat("tensor(x[2]):{ {x:0}:2, {x:1}:1 }", b, a, "x");
}
Also used : Tensor(com.yahoo.tensor.Tensor) 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