I
- the input typeO
- the output typepublic class Predictor<I,O>
extends java.lang.Object
implements java.lang.AutoCloseable
Predictor
interface provides a session for model inference.
You can use a Predictor
, with a specified Translator
, to perform inference on
a Model
. The following is example code that uses Predictor
:
Model model = Model.load(modelDir, modelName);
// User must implement Translator interface, read Translator
for detail.
Translator<String, String> translator = new MyTranslator();
try (Predictor<String, String> predictor = model.newPredictor(translator)) {
String result = predictor.predict("What's up");
}
See the tutorials on:
Modifier and Type | Field and Description |
---|---|
protected Block |
block |
protected NDManager |
manager |
protected Metrics |
metrics |
protected ParameterStore |
parameterStore |
Constructor and Description |
---|
Predictor(Model model,
Translator<I,O> translator,
boolean copy)
|
Modifier and Type | Method and Description |
---|---|
java.util.List<O> |
batchPredict(java.util.List<I> inputs)
Predicts a batch for inference.
|
void |
close() |
protected void |
finalize() |
O |
predict(I input)
Predicts an item for inference.
|
protected NDList |
predictInternal(TranslatorContext ctx,
NDList ndList)
Predicts an item for inference.
|
void |
setMetrics(Metrics metrics)
Attaches a Metrics param to use for benchmark.
|
protected NDManager manager
protected Metrics metrics
protected Block block
protected ParameterStore parameterStore
public Predictor(Model model, Translator<I,O> translator, boolean copy)
model
- the model on which the predictions are basedtranslator
- the translator to be usedcopy
- whether to copy the parameters to the parameter storepublic O predict(I input) throws TranslateException
input
- the inputTranslateException
- if an error occurs during predictionprotected NDList predictInternal(TranslatorContext ctx, NDList ndList) throws TranslateException
ctx
- the context for the Predictor
.ndList
- the input NDList
NDList
TranslateException
- if an error occurs during predictionpublic java.util.List<O> batchPredict(java.util.List<I> inputs) throws TranslateException
inputs
- a list of inputsTranslateException
- if an error occurs during predictionpublic void setMetrics(Metrics metrics)
metrics
- the Metrics classpublic void close()
close
in interface java.lang.AutoCloseable
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable