use of org.tensorflow.example.Example in project zoltar by spotify.
the class TensorFlowModelTest method getTFIrisPredictor.
public static Predictor<Iris, Long> getTFIrisPredictor() throws Exception {
final TensorFlowPredictFn<Iris, Long> predictFn = (model, vectors) -> {
final List<CompletableFuture<Prediction<Iris, Long>>> predictions = vectors.stream().map(vector -> {
return CompletableFuture.supplyAsync(() -> predict(model, vector.value())).thenApply(value -> Prediction.create(vector.input(), value));
}).collect(Collectors.toList());
return CompletableFutures.allAsList(predictions);
};
final URI trainedModelUri = TensorFlowModelTest.class.getResource("/trained_model").toURI();
final URI settingsUri = TensorFlowModelTest.class.getResource("/settings.json").toURI();
final String settings = new String(Files.readAllBytes(Paths.get(settingsUri)), StandardCharsets.UTF_8);
final ModelLoader<TensorFlowModel> model = TensorFlowLoader.create(trainedModelUri.toString());
final ExtractFn<Iris, Example> extractFn = FeatranExtractFns.example(IrisFeaturesSpec.irisFeaturesSpec(), settings);
return PredictorsTest.newBuilder(model, extractFn, predictFn).predictor();
}
Aggregations