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