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