T
- the type of prediction produced by the model.public abstract class Model<T extends Output<T>> extends Object implements com.oracle.labs.mlrg.olcut.provenance.Provenancable<ModelProvenance>, Serializable
If two features map to the same id in the featureIDMap, then occurrences of those features will be combined at prediction time.
Modifier and Type | Field and Description |
---|---|
static String |
ALL_OUTPUTS
Used in getTopFeatures when the Model doesn't support per output feature lists.
|
static String |
BIAS_FEATURE
Used to denote the bias feature in a linear model.
|
protected ImmutableFeatureMap |
featureIDMap
The features this model knows about.
|
protected boolean |
generatesProbabilities
Does this model generate probability distributions in the output.
|
protected String |
name
The model's name.
|
protected ImmutableOutputInfo<T> |
outputIDInfo
The outputs this model predicts.
|
protected ModelProvenance |
provenance
The model provenance.
|
protected String |
provenanceOutput
The cached toString of the model provenance.
|
Modifier | Constructor and Description |
---|---|
protected |
Model(String name,
ModelProvenance provenance,
ImmutableFeatureMap featureIDMap,
ImmutableOutputInfo<T> outputIDInfo,
boolean generatesProbabilities)
Constructs a new model, storing the supplied fields.
|
Modifier and Type | Method and Description |
---|---|
Model<T> |
copy()
Copies a model, returning a deep copy of any mutable state, and a shallow copy otherwise.
|
protected abstract Model<T> |
copy(String newName,
ModelProvenance newProvenance)
Copies a model, replacing it's provenance and name with the supplied values.
|
boolean |
generatesProbabilities()
Does this model generate probabilistic predictions.
|
abstract Optional<Excuse<T>> |
getExcuse(Example<T> example)
Generates an excuse for an example.
|
Optional<List<Excuse<T>>> |
getExcuses(Iterable<Example<T>> examples)
Generates an excuse for each example.
|
ImmutableFeatureMap |
getFeatureIDMap()
Gets the feature domain.
|
String |
getName()
Returns the model name.
|
ImmutableOutputInfo<T> |
getOutputIDInfo()
Gets the output domain.
|
ModelProvenance |
getProvenance() |
abstract Map<String,List<com.oracle.labs.mlrg.olcut.util.Pair<String,Double>>> |
getTopFeatures(int n)
Gets the top
n features associated with this model. |
protected List<Prediction<T>> |
innerPredict(Iterable<Example<T>> examples)
Called by the base implementations of
predict(Iterable) and predict(Dataset) . |
List<Prediction<T>> |
predict(Dataset<T> examples)
Uses the model to predict the outputs for multiple examples contained in
a data set.
|
abstract Prediction<T> |
predict(Example<T> example)
Uses the model to predict the output for a single example.
|
List<Prediction<T>> |
predict(Iterable<Example<T>> examples)
Uses the model to predict the output for multiple examples.
|
void |
setName(String name)
Sets the model name.
|
String |
toString() |
boolean |
validate(Class<? extends Output<?>> clazz)
Validates that this Model does in fact support the supplied output type.
|
public static final String ALL_OUTPUTS
public static final String BIAS_FEATURE
protected String name
protected final ModelProvenance provenance
protected final String provenanceOutput
Mostly cached so it appears in the serialized output and can be read by grepping the binary.
protected final ImmutableFeatureMap featureIDMap
protected final ImmutableOutputInfo<T extends Output<T>> outputIDInfo
protected final boolean generatesProbabilities
protected Model(String name, ModelProvenance provenance, ImmutableFeatureMap featureIDMap, ImmutableOutputInfo<T> outputIDInfo, boolean generatesProbabilities)
name
- The model name.provenance
- The model provenance.featureIDMap
- The features.outputIDInfo
- The possible outputs.generatesProbabilities
- Does this model emit probabilistic outputs.public String getName()
public void setName(String name)
name
- The new model name.public ModelProvenance getProvenance()
getProvenance
in interface com.oracle.labs.mlrg.olcut.provenance.Provenancable<ModelProvenance>
public ImmutableFeatureMap getFeatureIDMap()
public ImmutableOutputInfo<T> getOutputIDInfo()
public boolean generatesProbabilities()
public boolean validate(Class<? extends Output<?>> clazz)
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 a ClassCastException
when called.
clazz
- The class object to verify the output type against.public abstract Prediction<T> predict(Example<T> example)
predict does not mutate the example.
Throws IllegalArgumentException
if the example has no features
or no feature overlap with the model.
example
- the example to predict.public List<Prediction<T>> predict(Iterable<Example<T>> examples)
Throws IllegalArgumentException
if the examples have no features
or no feature overlap with the model.
examples
- the examples to predict.public List<Prediction<T>> predict(Dataset<T> examples)
Throws IllegalArgumentException
if the examples have no features
or no feature overlap with the model.
examples
- the data set containing the examples to predict.protected List<Prediction<T>> innerPredict(Iterable<Example<T>> examples)
predict(Iterable)
and predict(Dataset)
.examples
- The examples to predict.public abstract Map<String,List<com.oracle.labs.mlrg.olcut.util.Pair<String,Double>>> getTopFeatures(int n)
n
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()
.
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.public abstract Optional<Excuse<T>> getExcuse(Example<T> 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.
example
- The input example.public Optional<List<Excuse<T>>> getExcuses(Iterable<Example<T>> examples)
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.
examples
- An iterable of examplespublic Model<T> copy()
protected abstract Model<T> copy(String newName, ModelProvenance newProvenance)
Used to provide the provenance removal functionality.
newName
- The new name.newProvenance
- The new provenance.Copyright © 2015–2021 Oracle and/or its affiliates. All rights reserved.