use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class SparseBinaryFormat method decode.
@Override
public Tensor decode(Optional<TensorType> optionalType, GrowableByteBuffer buffer) {
TensorType type;
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);
} else {
type = decodeType(buffer);
}
Tensor.Builder builder = Tensor.Builder.of(type);
decodeCells(buffer, builder, type);
return builder.build();
}
use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class Reshape method lazyGetType.
@Override
protected OrderedTensorType lazyGetType() {
if (!allInputTypesPresent(2)) {
return null;
}
TensorFlowOperation newShape = inputs.get(1);
if (!newShape.getConstantValue().isPresent()) {
throw new IllegalArgumentException("Reshape in " + node.getName() + ": " + "shape input must be a constant.");
}
Tensor shape = newShape.getConstantValue().get().asTensor();
OrderedTensorType inputType = inputs.get(0).type().get();
OrderedTensorType.Builder outputTypeBuilder = new OrderedTensorType.Builder(node);
int dimensionIndex = 0;
for (Iterator<Tensor.Cell> cellIterator = shape.cellIterator(); cellIterator.hasNext(); ) {
Tensor.Cell cell = cellIterator.next();
int size = cell.getValue().intValue();
if (size < 0) {
size = -1 * (int) shape.reduce(Reduce.Aggregator.prod).asDouble() / tensorSize(inputType.type()).intValue();
}
outputTypeBuilder.add(TensorType.Dimension.indexed(String.format("%s_%d", vespaName(), dimensionIndex), size));
dimensionIndex++;
}
return outputTypeBuilder.build();
}
use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class EvaluationTester method assertEvaluates.
// TODO: Test both bound and unbound indexed
public RankingExpression assertEvaluates(String expectedTensor, String expressionString, boolean mappedTensors, String... tensorArgumentStrings) {
MapContext context = defaultContext.thawedCopy();
int argumentIndex = 0;
for (String argumentString : tensorArgumentStrings) {
Tensor argument;
if (// explicitly decided type
argumentString.startsWith("tensor("))
argument = Tensor.from(argumentString);
else
// use mappedTensors+dimensions in tensor to decide type
argument = Tensor.from(typeFrom(argumentString, mappedTensors), argumentString);
context.put("tensor" + (argumentIndex++), new TensorValue(argument));
}
return assertEvaluates(new TensorValue(Tensor.from(expectedTensor)), expressionString, context, mappedTensors ? "Mapped tensors" : "Indexed tensors");
}
use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class JsonSerializationHelper method serializeTensorField.
public static void serializeTensorField(JsonGenerator generator, FieldBase field, TensorFieldValue value) {
wrapIOException(() -> {
fieldNameIfNotNull(generator, field);
generator.writeStartObject();
if (value.getTensor().isPresent()) {
Tensor tensor = value.getTensor().get();
serializeTensorCells(generator, tensor);
}
generator.writeEndObject();
});
}
use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class JsonReaderTestCase method testParsingOfMappedTensorWithCells.
@Test
public void testParsingOfMappedTensorWithCells() {
Tensor tensor = assertMappedTensorField("{{x:a,y:b}:2.0,{x:c,y:b}:3.0}}", createPutWithMappedTensor("{ " + " \"cells\": [ " + " { \"address\": { \"x\": \"a\", \"y\": \"b\" }, " + " \"value\": 2.0 }, " + " { \"address\": { \"x\": \"c\", \"y\": \"b\" }, " + " \"value\": 3.0 } " + " ]" + "}"));
// any functional instance is fine
assertTrue(tensor instanceof MappedTensor);
}
Aggregations