use of com.spotify.zoltar.FeatureExtractFns.ExtractFn in project zoltar by spotify.
the class TensorFlowGraphModelTest method testModelInference.
@Test
public void testModelInference() throws Exception {
final Path graphFile = createADummyTFGraph();
final JFeatureSpec<Double> featureSpec = JFeatureSpec.<Double>create().required(d -> d, Identity.apply("feature"));
final URI settingsUri = getClass().getResource("/settings_dummy.json").toURI();
final String settings = new String(Files.readAllBytes(Paths.get(settingsUri)), StandardCharsets.UTF_8);
final ModelLoader<TensorFlowGraphModel> tfModel = TensorFlowGraphLoader.create(graphFile.toString(), null, null);
final PredictFn<TensorFlowGraphModel, Double, double[], Double> predictFn = (model, vectors) -> vectors.stream().map(vector -> {
try (Tensor<Double> input = Tensors.create(vector.value()[0])) {
List<Tensor<?>> results = null;
try {
results = model.instance().runner().fetch(mulResult).feed(inputOpName, input).run();
return Prediction.create(vector.input(), results.get(0).doubleValue());
} finally {
if (results != null) {
results.forEach(Tensor::close);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toList());
final ExtractFn<Double, double[]> extractFn = FeatranExtractFns.doubles(featureSpec, settings);
final Double[] input = new Double[] { 0.0D, 1.0D, 7.0D };
final double[] expected = Arrays.stream(input).mapToDouble(d -> d * 2.0D).toArray();
final CompletableFuture<double[]> result = PredictorsTest.newBuilder(tfModel, extractFn, predictFn).predictor().predict(input).thenApply(predictions -> {
return predictions.stream().mapToDouble(Prediction::value).toArray();
}).toCompletableFuture();
assertArrayEquals(result.get(), expected, Double.MIN_VALUE);
}
use of com.spotify.zoltar.FeatureExtractFns.ExtractFn 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();
}
use of com.spotify.zoltar.FeatureExtractFns.ExtractFn in project zoltar by spotify.
the class PredictorTest method timeout.
@Test
public void timeout() {
final Duration wait = Duration.ofSeconds(1);
final Duration predictionTimeout = Duration.ZERO;
final ExtractFn<Object, Object> extractFn = inputs -> Collections.emptyList();
final PredictFn<DummyModel, Object, Object, Object> predictFn = (model, vectors) -> {
Thread.sleep(wait.toMillis());
return Collections.emptyList();
};
try {
final ModelLoader<DummyModel> loader = ModelLoader.lift(DummyModel::new);
DefaultPredictorBuilder.create(loader, extractFn, predictFn).predictor().predict(predictionTimeout, new Object()).toCompletableFuture().get(wait.toMillis(), TimeUnit.MILLISECONDS);
fail("should throw TimeoutException");
} catch (final Exception e) {
assertTrue(e.getCause() instanceof TimeoutException);
}
}
use of com.spotify.zoltar.FeatureExtractFns.ExtractFn in project zoltar by spotify.
the class PredictorTest method nonEmpty.
@Test
public void nonEmpty() throws InterruptedException, ExecutionException, TimeoutException {
final Duration wait = Duration.ofSeconds(1);
final SingleExtractFn<Integer, Float> extractFn = input -> (float) input / 10;
final PredictFn<DummyModel, Integer, Float, Float> predictFn = (model, vectors) -> {
return vectors.stream().map(vector -> Prediction.create(vector.input(), vector.value() * 2)).collect(Collectors.toList());
};
final ModelLoader<DummyModel> loader = ModelLoader.lift(DummyModel::new);
final List<Prediction<Integer, Float>> predictions = DefaultPredictorBuilder.create(loader, extractFn, predictFn).predictor().predict(1).toCompletableFuture().get(wait.toMillis(), TimeUnit.MILLISECONDS);
assertThat(predictions.size(), is(1));
assertThat(predictions.get(0), is(Prediction.create(1, 0.2f)));
}
use of com.spotify.zoltar.FeatureExtractFns.ExtractFn in project zoltar by spotify.
the class XGBoostModelTest method getXGBoostIrisPredictor.
public static Predictor<Iris, Long> getXGBoostIrisPredictor() throws Exception {
final URI trainedModelUri = XGBoostModelTest.class.getResource("/iris.model").toURI();
final URI settingsUri = XGBoostModelTest.class.getResource("/settings.json").toURI();
final XGBoostPredictFn<Iris, Long> predictFn = (model, vectors) -> {
final List<CompletableFuture<Prediction<Iris, Long>>> predictions = vectors.stream().map(vector -> {
return CompletableFuture.supplyAsync(() -> {
try {
final Iterator<LabeledPoint> iterator = Collections.singletonList(vector.value()).iterator();
final DMatrix dMatrix = new DMatrix(iterator, null);
final float[] score = model.instance().predict(dMatrix)[0];
int idx = IntStream.range(0, score.length).reduce((i, j) -> score[i] >= score[j] ? i : j).getAsInt();
return Prediction.create(vector.input(), (long) idx);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}).collect(Collectors.toList());
return CompletableFutures.allAsList(predictions);
};
final String settings = new String(Files.readAllBytes(Paths.get(settingsUri)), StandardCharsets.UTF_8);
final XGBoostLoader model = XGBoostLoader.create(trainedModelUri.toString());
final ExtractFn<Iris, LabeledPoint> extractFn = FeatranExtractFns.labeledPoints(IrisFeaturesSpec.irisFeaturesSpec(), settings);
return PredictorsTest.newBuilder(model, extractFn, predictFn).predictor();
}
Aggregations