use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class TensorFieldValueTestCase method requireThatSameTensorValueIsEqual.
@Test
public void requireThatSameTensorValueIsEqual() {
Tensor tensor = Tensor.from("{{x:0}:2.0}");
TensorFieldValue field1 = new TensorFieldValue(tensor);
TensorFieldValue field2 = new TensorFieldValue(tensor);
assertTrue(field1.equals(field1));
assertTrue(field1.equals(field2));
assertTrue(field1.equals(createFieldValue("{{x:0}:2.0}")));
}
use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class TensorFlowImporter method importConstant.
private static Optional<TensorFunction> importConstant(TensorFlowModel model, TensorFlowOperation operation, SavedModelBundle bundle) {
String name = operation.vespaName();
if (model.largeConstants().containsKey(name) || model.smallConstants().containsKey(name)) {
return operation.function();
}
Tensor tensor;
if (operation.getConstantValue().isPresent()) {
Value value = operation.getConstantValue().get();
if (!(value instanceof TensorValue)) {
// scalar values are inserted directly into the expression
return operation.function();
}
tensor = value.asTensor();
} else {
// Here we use the type from the operation, which will have correct dimension names after name resolving
tensor = TensorConverter.toVespaTensor(readVariable(operation.node().getName(), bundle), operation.type().get());
operation.setConstantValue(new TensorValue(tensor));
}
if (tensor.type().rank() == 0 || tensor.size() <= 1) {
model.smallConstant(name, tensor);
} else {
model.largeConstant(name, tensor);
}
return operation.function();
}
use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class Mean method lazyGetType.
@Override
protected OrderedTensorType lazyGetType() {
if (!allInputTypesPresent(2)) {
return null;
}
TensorFlowOperation reductionIndices = inputs.get(1);
if (!reductionIndices.getConstantValue().isPresent()) {
throw new IllegalArgumentException("Mean in " + node.getName() + ": " + "reduction indices must be a constant.");
}
Tensor indices = reductionIndices.getConstantValue().get().asTensor();
reduceDimensions = new ArrayList<>();
OrderedTensorType inputType = inputs.get(0).type().get();
for (Iterator<Tensor.Cell> cellIterator = indices.cellIterator(); cellIterator.hasNext(); ) {
Tensor.Cell cell = cellIterator.next();
int dimensionIndex = cell.getValue().intValue();
if (dimensionIndex < 0) {
dimensionIndex = inputType.dimensions().size() - dimensionIndex;
}
reduceDimensions.add(inputType.dimensions().get(dimensionIndex).name());
}
return reducedType(inputType, shouldKeepDimensions());
}
use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class Join method mappedGeneralJoin.
private Tensor mappedGeneralJoin(Tensor a, Tensor b, TensorType joinedType) {
int[] aToIndexes = mapIndexes(a.type(), joinedType);
int[] bToIndexes = mapIndexes(b.type(), joinedType);
Tensor.Builder builder = Tensor.Builder.of(joinedType);
for (Iterator<Tensor.Cell> aIterator = a.cellIterator(); aIterator.hasNext(); ) {
Map.Entry<TensorAddress, Double> aCell = aIterator.next();
for (Iterator<Tensor.Cell> bIterator = b.cellIterator(); bIterator.hasNext(); ) {
Map.Entry<TensorAddress, Double> bCell = bIterator.next();
TensorAddress combinedAddress = joinAddresses(aCell.getKey(), aToIndexes, bCell.getKey(), bToIndexes, joinedType);
// not combinable
if (combinedAddress == null)
continue;
builder.cell(combinedAddress, combinator.applyAsDouble(aCell.getValue(), bCell.getValue()));
}
}
return builder.build();
}
use of com.yahoo.tensor.Tensor in project vespa by vespa-engine.
the class Join method evaluate.
@Override
public <NAMETYPE extends TypeContext.Name> Tensor evaluate(EvaluationContext<NAMETYPE> context) {
Tensor a = argumentA.evaluate(context);
Tensor b = argumentB.evaluate(context);
TensorType joinedType = new TensorType.Builder(a.type(), b.type()).build();
// Choose join algorithm
if (hasSingleIndexedDimension(a) && hasSingleIndexedDimension(b) && a.type().dimensions().get(0).name().equals(b.type().dimensions().get(0).name()))
return indexedVectorJoin((IndexedTensor) a, (IndexedTensor) b, joinedType);
else if (joinedType.dimensions().size() == a.type().dimensions().size() && joinedType.dimensions().size() == b.type().dimensions().size())
return singleSpaceJoin(a, b, joinedType);
else if (a.type().dimensions().containsAll(b.type().dimensions()))
return subspaceJoin(b, a, joinedType, true);
else if (b.type().dimensions().containsAll(a.type().dimensions()))
return subspaceJoin(a, b, joinedType, false);
else
return generalJoin(a, b, joinedType);
}
Aggregations