Class SequenceExample<T extends Output<T>>
- All Implemented Interfaces:
Serializable
,Iterable<Example<T>>
,ProtoSerializable<org.tribuo.protos.core.SequenceExampleProto>
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Protobuf serialization version.static final float
The default sequence example weight.Fields inherited from interface org.tribuo.protos.ProtoSerializable
DESERIALIZATION_METHOD_NAME, PROVENANCE_SERIALIZER
-
Constructor Summary
ConstructorDescriptionCreates an empty sequence example.SequenceExample
(List<Example<T>> examples) Creates a sequence example from the list of examples.SequenceExample
(List<Example<T>> examples, float weight) Creates a sequence example from the list of examples, setting the weight.Creates a sequence example from the supplied outputs and list of list of features.SequenceExample
(List<T> outputs, List<? extends List<? extends Feature>> features, boolean attemptBinaryFeatures) Creates a sequence example from the supplied weight, outputs and list of list of features.Creates a sequence example from the supplied weight, outputs and list of list of features.SequenceExample
(List<T> outputs, List<? extends List<? extends Feature>> features, float weight, boolean attemptBinaryFeatures) Creates a sequence example from the supplied weight, outputs and list of list of features.SequenceExample
(SequenceExample<T> other) Creates a deep copy of the supplied sequence example. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addExample
(Example<T> e) Adds anExample
to this sequence.void
canonicalise
(FeatureMap featureMap) Reassigns feature name Strings in each Example inside this SequenceExample to point to those in theFeatureMap
.copy()
Returns a deep copy of this SequenceExample.static <T extends Output<T>>
SequenceExample<T>createWithEmptyOutputs
(List<? extends List<? extends Feature>> features, OutputFactory<T> outputFactory) Creates a SequenceExample usingOutputFactory.getUnknownOutput()
as the output for each sequence element.void
densify
(FeatureMap fMap) Converts all implicit zeros into explicit zeros based on the supplied feature map.static SequenceExample<?>
deserialize
(org.tribuo.protos.core.SequenceExampleProto e) Deserialization shortcut, used to firm up the types.static SequenceExample<?>
deserializeFromProto
(int version, String className, com.google.protobuf.Any message) Deserialization factory.boolean
Creates an iterator over every feature in this sequence.get
(int i) Gets the example found at the specified index.float
Gets the weight of this sequence.int
hashCode()
boolean
isDense
(FeatureMap fMap) Is this sequence example dense wrt the supplied feature map.iterator()
void
reduceByName
(Merger merger) Reduces the features in each example using the suppliedMerger
.void
removeFeatures
(List<Feature> features) Removes the features in the supplied list from each example contained in this sequence.org.tribuo.protos.core.SequenceExampleProto
Serializes this object to a protobuf.void
setWeight
(float weight) Sets the weight of this sequence.int
size()
Return how many examples are in this sequence.boolean
Checks that eachExample
in this sequence is valid.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
CURRENT_VERSION
public static final int CURRENT_VERSIONProtobuf serialization version.- See Also:
-
DEFAULT_WEIGHT
public static final float DEFAULT_WEIGHTThe default sequence example weight.- See Also:
-
-
Constructor Details
-
SequenceExample
public SequenceExample()Creates an empty sequence example. -
SequenceExample
Creates a sequence example from the list of examples.The examples are not copied by this method.
- Parameters:
examples
- The examples to incorporate.
-
SequenceExample
Creates a sequence example from the list of examples, setting the weight.The examples are encapsulated by this constructor, not copied.
- Parameters:
examples
- The examples to incorporate.weight
- The weight of this sequence.
-
SequenceExample
Creates a sequence example from the supplied outputs and list of list of features.The features are copied out by this constructor. The outputs and features lists must be of the same length. Sets the weight to
DEFAULT_WEIGHT
.- Parameters:
outputs
- The outputs for each sequence element.features
- The features for each sequence element.
-
SequenceExample
public SequenceExample(List<T> outputs, List<? extends List<? extends Feature>> features, float weight) Creates a sequence example from the supplied weight, outputs and list of list of features.The features are copied out by this constructor. The outputs and features lists must be of the same length.
- Parameters:
outputs
- The outputs for each sequence element.features
- The features for each sequence element.weight
- The weight for this sequence example.
-
SequenceExample
public SequenceExample(List<T> outputs, List<? extends List<? extends Feature>> features, boolean attemptBinaryFeatures) Creates a sequence example from the supplied weight, outputs and list of list of features.The features are copied out by this constructor. The outputs and features lists must be of the same length. Sets the weight to
DEFAULT_WEIGHT
.- Parameters:
outputs
- The outputs for each sequence element.features
- The features for each sequence element.attemptBinaryFeatures
- Attempt to useBinaryFeaturesExample
as the inner examples.
-
SequenceExample
public SequenceExample(List<T> outputs, List<? extends List<? extends Feature>> features, float weight, boolean attemptBinaryFeatures) Creates a sequence example from the supplied weight, outputs and list of list of features.The features are copied out by this constructor. The outputs and features lists must be of the same length.
- Parameters:
outputs
- The outputs for each sequence element.features
- The features for each sequence element.weight
- The weight for this sequence example.attemptBinaryFeatures
- Attempt to useBinaryFeaturesExample
as the inner examples.
-
SequenceExample
Creates a deep copy of the supplied sequence example.- Parameters:
other
- The sequence example to copy.
-
-
Method Details
-
deserializeFromProto
public static SequenceExample<?> deserializeFromProto(int version, String className, com.google.protobuf.Any message) throws com.google.protobuf.InvalidProtocolBufferException Deserialization factory.- Parameters:
version
- The serialized object version.className
- The class name.message
- The serialized data.- Returns:
- The deserialized object.
- Throws:
com.google.protobuf.InvalidProtocolBufferException
- If the protobuf could not be parsed from themessage
.
-
deserialize
Deserialization shortcut, used to firm up the types.- Parameters:
e
- The proto to deserialize.- Returns:
- The sequence example.
-
size
public int size()Return how many examples are in this sequence.- Returns:
- The number of examples.
-
removeFeatures
Removes the features in the supplied list from each example contained in this sequence.- Parameters:
features
- The features to remove.
-
get
Gets the example found at the specified index.- Parameters:
i
- The index to lookup.- Returns:
- The
Example
for index i.
-
validateExample
public boolean validateExample()Checks that eachExample
in this sequence is valid.- Returns:
- True if each
Example
is valid, false otherwise.
-
reduceByName
Reduces the features in each example using the suppliedMerger
.- Parameters:
merger
- The merger to use in the reduction.
-
setWeight
public void setWeight(float weight) Sets the weight of this sequence.- Parameters:
weight
- The new weight.
-
getWeight
public float getWeight()Gets the weight of this sequence.- Returns:
- The weight of this sequence.
-
addExample
Adds anExample
to this sequence.- Parameters:
e
- The example to add.
-
copy
Returns a deep copy of this SequenceExample.- Returns:
- A deep copy.
-
iterator
-
featureIterator
Creates an iterator over every feature in this sequence.- Returns:
- An iterator over features.
-
isDense
Is this sequence example dense wrt the supplied feature map.A sequence example is "dense" if each example inside it contains all the features in the map, and only those features.
- Parameters:
fMap
- The feature map to check against.- Returns:
- True if this sequence example contains only the features in the map, and all the features in the map.
-
densify
Converts all implicit zeros into explicit zeros based on the supplied feature map.- Parameters:
fMap
- The feature map to use for densification.
-
canonicalise
Reassigns feature name Strings in each Example inside this SequenceExample to point to those in theFeatureMap
. This significantly reduces memory allocation. It is called when a SequenceExample is added to aMutableSequenceDataset
, and should not be called outside of that context as it may interact unexpectedly withHashedFeatureMap
.- Parameters:
featureMap
- The feature map containing canonical feature names.
-
serialize
public org.tribuo.protos.core.SequenceExampleProto serialize()Description copied from interface:ProtoSerializable
Serializes this object to a protobuf.- Specified by:
serialize
in interfaceProtoSerializable<T extends Output<T>>
- Returns:
- The protobuf.
-
equals
-
hashCode
public int hashCode() -
createWithEmptyOutputs
public static <T extends Output<T>> SequenceExample<T> createWithEmptyOutputs(List<? extends List<? extends Feature>> features, OutputFactory<T> outputFactory) Creates a SequenceExample usingOutputFactory.getUnknownOutput()
as the output for each sequence element.Note: this method is used to create SequenceExamples at prediction time when there is no ground truth
Output
.- Type Parameters:
T
- The type of theOutput
.- Parameters:
features
- The features for each sequence element.outputFactory
- The output factory to use.- Returns:
- A new SequenceExample.
-