Class TensorFlowSavedModelExternalModel<T extends Output<T>>
java.lang.Object
org.tribuo.Model<T>
org.tribuo.interop.ExternalModel<T, TensorMap, TensorMap>
org.tribuo.interop.tensorflow.TensorFlowSavedModelExternalModel<T>
- All Implemented Interfaces:
com.oracle.labs.mlrg.olcut.provenance.Provenancable<ModelProvenance>,Closeable,Serializable,AutoCloseable
public final class TensorFlowSavedModelExternalModel<T extends Output<T>>
extends ExternalModel<T, TensorMap, TensorMap>
implements Closeable
A Tribuo wrapper around a TensorFlow saved model bundle.
The model's serialVersionUID is set to the major TensorFlow version number times 100.
N.B. TensorFlow support is experimental and may change without a major version bump.
- See Also:
-
Field Summary
Fields inherited from class org.tribuo.interop.ExternalModel
DEFAULT_BATCH_SIZE, featureBackwardMapping, featureForwardMappingFields inherited from class org.tribuo.Model
ALL_OUTPUTS, BIAS_FEATURE, featureIDMap, generatesProbabilities, name, outputIDInfo, provenance, provenanceOutput -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()protected TensorMapconvertFeatures(SparseVector input) Converts from a SparseVector using the external model's indices into the ingestion format for the external model.protected TensorMapconvertFeaturesList(List<SparseVector> input) Converts from a list of SparseVector using the external model's indices into the ingestion format for the external model.protected List<Prediction<T>> convertOutput(TensorMap output, int[] numValidFeatures, List<Example<T>> examples) Converts a tensor into a prediction.protected Prediction<T> convertOutput(TensorMap output, int numValidFeatures, Example<T> example) Converts a tensor into a prediction.copy(String newName, ModelProvenance newProvenance) Copies a model, replacing it's provenance and name with the supplied values.static <T extends Output<T>>
TensorFlowSavedModelExternalModel<T> createTensorflowModel(OutputFactory<T> factory, Map<String, Integer> featureMapping, Map<T, Integer> outputMapping, String outputName, FeatureConverter featureConverter, OutputConverter<T> outputConverter, String bundleDirectory) Creates a TensorflowSavedModelExternalModel by loading in aSavedModelBundle.protected TensorMapexternalPrediction(TensorMap input) Runs the session to make a prediction.getTopFeatures(int n) Gets the topnfeatures associated with this model.Methods inherited from class org.tribuo.interop.ExternalModel
createFeatureMap, createOutputInfo, getBatchSize, getExcuse, innerPredict, predict, setBatchSizeMethods inherited from class org.tribuo.Model
copy, generatesProbabilities, getExcuses, getFeatureIDMap, getName, getOutputIDInfo, getProvenance, predict, predict, setName, toString, validate
-
Method Details
-
convertFeatures
Description copied from class:ExternalModelConverts from a SparseVector using the external model's indices into the ingestion format for the external model.- Specified by:
convertFeaturesin classExternalModel<T extends Output<T>, TensorMap, TensorMap>- Parameters:
input- The features using external indices.- Returns:
- The ingestion format for the external model.
-
convertFeaturesList
Description copied from class:ExternalModelConverts from a list of SparseVector using the external model's indices into the ingestion format for the external model.- Specified by:
convertFeaturesListin classExternalModel<T extends Output<T>, TensorMap, TensorMap>- Parameters:
input- The features using external indices.- Returns:
- The ingestion format for the external model.
-
externalPrediction
Runs the session to make a prediction.Closes the input tensor after the prediction has been made.
- Specified by:
externalPredictionin classExternalModel<T extends Output<T>, TensorMap, TensorMap>- Parameters:
input- The input in the external model's format.- Returns:
- A tensor representing the output.
-
convertOutput
Converts a tensor into a prediction. Closes the output tensor after it's been converted.- Specified by:
convertOutputin classExternalModel<T extends Output<T>, TensorMap, TensorMap>- Parameters:
output- The output of the external model.numValidFeatures- The number of valid features in the input.example- The input example, used to construct the Prediction.- Returns:
- A
Predictionrepresenting this tensor output.
-
convertOutput
protected List<Prediction<T>> convertOutput(TensorMap output, int[] numValidFeatures, List<Example<T>> examples) Converts a tensor into a prediction. Closes the output tensor after it's been converted.- Specified by:
convertOutputin classExternalModel<T extends Output<T>, TensorMap, TensorMap>- Parameters:
output- The output of the external model.numValidFeatures- An array with the number of valid features in each example.examples- The input examples, used to construct the Predictions.- Returns:
- A list of
Predictionrepresenting this tensor output.
-
getTopFeatures
Description copied from class:ModelGets the topnfeatures associated with this model.If the model does not produce per output feature lists, it returns a map with a single element with key Model.ALL_OUTPUTS.
If the model cannot describe it's top features then it returns
Collections.emptyMap().- Specified by:
getTopFeaturesin classModel<T extends Output<T>>- Parameters:
n- the number of features to return. If this value is less than 0, all features should be returned for each class, unless the model cannot score it's features.- Returns:
- a map from string outputs to an ordered list of pairs of feature names and weights associated with that feature in the model
-
copy
Description copied from class:ModelCopies a model, replacing it's provenance and name with the supplied values.Used to provide the provenance removal functionality.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
createTensorflowModel
public static <T extends Output<T>> TensorFlowSavedModelExternalModel<T> createTensorflowModel(OutputFactory<T> factory, Map<String, Integer> featureMapping, Map<T, Integer> outputMapping, String outputName, FeatureConverter featureConverter, OutputConverter<T> outputConverter, String bundleDirectory) Creates a TensorflowSavedModelExternalModel by loading in aSavedModelBundle.Throws
IllegalArgumentExceptionif the model bundle could not be loaded.- Type Parameters:
T- The type of the output.- Parameters:
factory- The output factory.featureMapping- The feature mapping between Tribuo's names and the TF integer ids.outputMapping- The output mapping between Tribuo's names and the TF integer ids.outputName- The name of the output tensor.featureConverter- The feature transformation function.outputConverter- The output transformation function.bundleDirectory- The path to load the saved model bundle from.- Returns:
- The TF model wrapped in a Tribuo
ExternalModel.
-