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);
}
}
}
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");
}
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);
}
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");
}
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");
}
Aggregations