Class Model<T extends Output<T>>
- Type Parameters:
T
- the type of prediction produced by the model.
- All Implemented Interfaces:
com.oracle.labs.mlrg.olcut.provenance.Provenancable<ModelProvenance>
,Serializable
,ProtoSerializable<org.tribuo.protos.core.ModelProto>
- Direct Known Subclasses:
AbstractSGDModel
,ClassifierChainModel
,DummyClassifierModel
,DummyRegressionModel
,EnsembleModel
,ExternalModel
,HdbscanModel
,IndependentMultiLabelModel
,KernelSVMModel
,KMeansModel
,KNNModel
,LibLinearModel
,LibSVMModel
,MultinomialNaiveBayesModel
,SkeletalIndependentRegressionModel
,SparseModel
,TensorFlowModel
,TransformedModel
,XGBoostModel
If two features map to the same id in the featureIDMap, then occurrences of those features will be combined at prediction time.
Note Model's implementation of ProtoSerializable
throws UnsupportedOperationException
for compatibility reasons. This default implementation will be removed in the next major release
of Tribuo.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Used in getTopFeatures when the Model doesn't support per output feature lists.static final String
Used to denote the bias feature in a linear model.protected final ImmutableFeatureMap
The features this model knows about.protected final boolean
Does this model generate probability distributions in the output.protected String
The model's name.protected final ImmutableOutputInfo<T>
The outputs this model predicts.protected final ModelProvenance
The model provenance.protected final String
The cached toString of the model provenance.Fields inherited from interface org.tribuo.protos.ProtoSerializable
DESERIALIZATION_METHOD_NAME, PROVENANCE_SERIALIZER
-
Constructor Summary
ModifierConstructorDescriptionprotected
Model
(String name, ModelProvenance provenance, ImmutableFeatureMap featureIDMap, ImmutableOutputInfo<T> outputIDInfo, boolean generatesProbabilities) Constructs a new model, storing the supplied fields. -
Method Summary
Modifier and TypeMethodDescriptionCasts the model to the specified output type, assuming it is valid.copy()
Copies a model, returning a deep copy of any mutable state, and a shallow copy otherwise.copy
(String newName, ModelProvenance newProvenance) Copies a model, replacing its provenance and name with the supplied values.protected ModelDataCarrier<T>
Constructs the data carrier for serialization.static Model<?>
deserialize
(org.tribuo.protos.core.ModelProto proto) Deserializes the model from the supplied protobuf.static Model<?>
deserializeFromFile
(Path path) Reads an instance ofModelProto
from the supplied path and deserializes it.static Model<?>
Reads an instance ofModelProto
from the supplied input stream and deserializes it.boolean
Does this model generate probabilistic predictions.Generates an excuse for an example.getExcuses
(Iterable<Example<T>> examples) Generates an excuse for each example.Gets the feature domain.getName()
Returns the model name.Gets the output domain.getTopFeatures
(int n) Gets the topn
features associated with this model.protected List<Prediction<T>>
innerPredict
(Iterable<Example<T>> examples) Called by the base implementations ofpredict(Iterable)
andpredict(Dataset)
.List<Prediction<T>>
Uses the model to predict the output for multiple examples.List<Prediction<T>>
Uses the model to predict the outputs for multiple examples contained in a data set.abstract Prediction<T>
Uses the model to predict the output for a single example.org.tribuo.protos.core.ModelProto
Serializes this object to a protobuf.void
serializeToFile
(Path path) Serializes this model to aModelProto
and writes it to the supplied path.void
serializeToStream
(OutputStream stream) Serializes this model to aModelProto
and writes it to the supplied output stream.void
Sets the model name.toString()
boolean
Validates that this Model does in fact support the supplied output type.
-
Field Details
-
ALL_OUTPUTS
Used in getTopFeatures when the Model doesn't support per output feature lists.- See Also:
-
BIAS_FEATURE
Used to denote the bias feature in a linear model.- See Also:
-
name
The model's name. -
provenance
The model provenance. -
provenanceOutput
The cached toString of the model provenance.Mostly cached so it appears in the serialized output and can be read by grepping the binary.
-
featureIDMap
The features this model knows about. -
outputIDInfo
The outputs this model predicts. -
generatesProbabilities
protected final boolean generatesProbabilitiesDoes this model generate probability distributions in the output.
-
-
Constructor Details
-
Model
protected Model(String name, ModelProvenance provenance, ImmutableFeatureMap featureIDMap, ImmutableOutputInfo<T> outputIDInfo, boolean generatesProbabilities) Constructs a new model, storing the supplied fields.- Parameters:
name
- The model name.provenance
- The model provenance.featureIDMap
- The features.outputIDInfo
- The possible outputs.generatesProbabilities
- Does this model emit probabilistic outputs.
-
-
Method Details
-
getName
Returns the model name.- Returns:
- The model name.
-
setName
Sets the model name.- Parameters:
name
- The new model name.
-
getProvenance
-
getFeatureIDMap
Gets the feature domain.- Returns:
- The feature domain.
-
getOutputIDInfo
Gets the output domain.- Returns:
- The output domain.
-
generatesProbabilities
public boolean generatesProbabilities()Does this model generate probabilistic predictions.- Returns:
- True if the model generates probabilistic predictions.
-
validate
Validates that this Model does in fact support the supplied output type.As the output type is erased at runtime, deserialising a Model is an unchecked operation. This method allows the user to check that the deserialised model is of the appropriate type, rather than seeing if
predict(org.tribuo.Example<T>)
throws aClassCastException
when called.- Parameters:
clazz
- The class object to verify the output type against.- Returns:
- True if the output type is assignable to the class object type, false otherwise.
-
predict
Uses the model to predict the output for a single example.predict does not mutate the example.
Throws
IllegalArgumentException
if the example has no features or no feature overlap with the model.- Parameters:
example
- the example to predict.- Returns:
- the result of the prediction.
-
predict
Uses the model to predict the output for multiple examples.Throws
IllegalArgumentException
if the examples have no features or no feature overlap with the model.- Parameters:
examples
- the examples to predict.- Returns:
- the results of the prediction, in the same order as the examples.
-
predict
Uses the model to predict the outputs for multiple examples contained in a data set.Throws
IllegalArgumentException
if the examples have no features or no feature overlap with the model.- Parameters:
examples
- the data set containing the examples to predict.- Returns:
- the results of the predictions, in the same order as the Dataset provides the examples.
-
innerPredict
Called by the base implementations ofpredict(Iterable)
andpredict(Dataset)
.- Parameters:
examples
- The examples to predict.- Returns:
- The results of the predictions, in the same order as the examples.
-
getTopFeatures
public abstract Map<String,List<com.oracle.labs.mlrg.olcut.util.Pair<String, getTopFeaturesDouble>>> (int n) Gets the topn
features 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()
.- 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
-
getExcuse
Generates an excuse for an example.This attempts to explain a classification result. Generating an excuse may be quite an expensive operation.
This excuse either contains per class information or an entry with key Model.ALL_OUTPUTS.
The optional is empty if the model does not provide excuses.
- Parameters:
example
- The input example.- Returns:
- An optional excuse object. The optional is empty if this model does not provide excuses.
-
getExcuses
Generates an excuse for each example.This may be an expensive operation, and probably should be overridden in subclasses for performance reasons.
These excuses either contain per class information or an entry with key Model.ALL_OUTPUTS.
The optional is empty if the model does not provide excuses.
- Parameters:
examples
- An iterable of examples- Returns:
- A optional list of excuses. The Optional is empty if this model does not provide excuses.
-
copy
Copies a model, returning a deep copy of any mutable state, and a shallow copy otherwise.- Returns:
- A copy of the model.
-
copy
Copies a model, replacing its provenance and name with the supplied values.Used to provide the provenance removal functionality.
- Parameters:
newName
- The new name.newProvenance
- The new provenance.- Returns:
- A copy of the model.
-
toString
-
castModel
Casts the model to the specified output type, assuming it is valid. If it's not valid, throwsClassCastException
.This method is intended for use on a deserialized model to restore its generic type in a safe way.
- Type Parameters:
U
- The output type.- Parameters:
outputType
- The output type to cast to.- Returns:
- The model cast to the correct value.
-
serialize
public org.tribuo.protos.core.ModelProto serialize()Description copied from interface:ProtoSerializable
Serializes this object to a protobuf.- Specified by:
serialize
in interfaceProtoSerializable<T extends Output<T>>
- Returns:
- The protobuf.
-
serializeToFile
Serializes this model to aModelProto
and writes it to the supplied path.- Parameters:
path
- The path to write to.- Throws:
IOException
- If the path could not be written to.
-
serializeToStream
Serializes this model to aModelProto
and writes it to the supplied output stream.Does not close the stream.
- Parameters:
stream
- The output stream to write to.- Throws:
IOException
- If the stream could not be written to.
-
deserialize
Deserializes the model from the supplied protobuf.- Parameters:
proto
- The protobuf to deserialize.- Returns:
- The model.
-
deserializeFromFile
Reads an instance ofModelProto
from the supplied path and deserializes it.- Parameters:
path
- The path to read.- Returns:
- The deserialized model.
- Throws:
IOException
- If the path could not be read from, or the parsing failed.
-
deserializeFromStream
Reads an instance ofModelProto
from the supplied input stream and deserializes it.Does not close the stream.
- Parameters:
is
- The input stream to read.- Returns:
- The deserialized model.
- Throws:
IOException
- If the stream could not be read from, or the parsing failed.
-
createDataCarrier
Constructs the data carrier for serialization.- Returns:
- The serialization data carrier.
-