use of com.yahoo.tensor.TensorType in project vespa by vespa-engine.
the class Rename method evaluate.
@Override
public <NAMETYPE extends TypeContext.Name> Tensor evaluate(EvaluationContext<NAMETYPE> context) {
Tensor tensor = argument.evaluate(context);
TensorType renamedType = type(tensor.type());
// an array which lists the index of each label in the renamed type
int[] toIndexes = new int[tensor.type().dimensions().size()];
for (int i = 0; i < tensor.type().dimensions().size(); i++) {
String dimensionName = tensor.type().dimensions().get(i).name();
String newDimensionName = fromToMap.getOrDefault(dimensionName, dimensionName);
toIndexes[i] = renamedType.indexOfDimension(newDimensionName).get();
}
Tensor.Builder builder = Tensor.Builder.of(renamedType);
for (Iterator<Tensor.Cell> i = tensor.cellIterator(); i.hasNext(); ) {
Map.Entry<TensorAddress, Double> cell = i.next();
TensorAddress renamedAddress = rename(cell.getKey(), toIndexes);
builder.cell(renamedAddress, cell.getValue());
}
return builder.build();
}
use of com.yahoo.tensor.TensorType in project vespa by vespa-engine.
the class TensorConverter method toVespaTensor.
public static Tensor toVespaTensor(org.tensorflow.Tensor<?> tfTensor, String dimensionPrefix) {
TensorType type = toVespaTensorType(tfTensor.shape(), dimensionPrefix);
Values values = readValuesOf(tfTensor);
IndexedTensor.BoundBuilder builder = (IndexedTensor.BoundBuilder) Tensor.Builder.of(type);
for (int i = 0; i < values.size(); i++) builder.cellByDirectIndex(i, values.get(i));
return builder.build();
}
use of com.yahoo.tensor.TensorType in project vespa by vespa-engine.
the class ExpandDims method lazyGetFunction.
@Override
protected TensorFunction lazyGetFunction() {
if (!allInputFunctionsPresent(2)) {
return null;
}
// multiply with a generated tensor created from the reduced dimensions
TensorType.Builder typeBuilder = new TensorType.Builder();
for (String name : expandDimensions) {
typeBuilder.indexed(name, 1);
}
TensorType generatedType = typeBuilder.build();
ExpressionNode generatedExpression = new ConstantNode(new DoubleValue(1));
Generate generatedFunction = new Generate(generatedType, new GeneratorLambdaFunctionNode(generatedType, generatedExpression).asLongListToDoubleOperator());
return new com.yahoo.tensor.functions.Join(inputs().get(0).function().get(), generatedFunction, ScalarFunctions.multiply());
}
Aggregations