Package smile.sequence
Class CRFLabeler<T>
java.lang.Object
smile.sequence.CRFLabeler<T>
- Type Parameters:
T
- the data type of model input objects.
- All Implemented Interfaces:
Serializable
,SequenceLabeler<T>
First-order CRF sequence labeler.
- See Also:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> CRFLabeler
<T> Fits a CRF model.static <T> CRFLabeler
<T> fit
(T[][] sequences, int[][] labels, Function<T, Tuple> features, int ntrees, int maxDepth, int maxNodes, int nodeSize, double shrinkage) Fits a CRF.static <T> CRFLabeler
<T> fit
(T[][] sequences, int[][] labels, Function<T, Tuple> features, Properties params) Fits a CRF model.int[]
Returns the most likely label sequence given the feature sequence by the forward-backward algorithm.toString()
int[]
Labels sequence with Viterbi algorithm.
-
Field Details
-
model
The CRF model. -
features
The feature function.
-
-
Constructor Details
-
CRFLabeler
Constructor.- Parameters:
model
- the CRF model.features
- the feature function.
-
-
Method Details
-
fit
Fits a CRF model.- Type Parameters:
T
- the data type of observations.- Parameters:
sequences
- the training data.labels
- the training sequence labels.features
- the feature function.- Returns:
- the model.
-
fit
public static <T> CRFLabeler<T> fit(T[][] sequences, int[][] labels, Function<T, Tuple> features, Properties params) Fits a CRF model.- Type Parameters:
T
- the data type of observations.- Parameters:
sequences
- the training data.labels
- the training sequence labels.features
- the feature function.params
- the hyper-parameters.- Returns:
- the model.
-
fit
public static <T> CRFLabeler<T> fit(T[][] sequences, int[][] labels, Function<T, Tuple> features, int ntrees, int maxDepth, int maxNodes, int nodeSize, double shrinkage) Fits a CRF.- Type Parameters:
T
- the data type of observations.- Parameters:
sequences
- the observation sequences.labels
- the state labels of observations, of which states take values in [0, k), where k is the number of hidden states.features
- the feature function.ntrees
- the number of trees/iterations.maxDepth
- the maximum depth of the tree.maxNodes
- the maximum number of leaf nodes in the tree.nodeSize
- the number of instances in a node below which the tree will not split, setting nodeSize = 5 generally gives good results.shrinkage
- the shrinkage parameter in (0, 1] controls the learning rate of procedure.- Returns:
- the model.
-
toString
-
predict
Returns the most likely label sequence given the feature sequence by the forward-backward algorithm.- Specified by:
predict
in interfaceSequenceLabeler<T>
- Parameters:
o
- the observation sequence.- Returns:
- the most likely state sequence.
-
viterbi
Labels sequence with Viterbi algorithm. Viterbi algorithm returns the whole sequence label that has the maximum probability, which makes sense in applications (e.g.part-of-speech tagging) that require coherent sequential labeling. The forward-backward algorithm labels a sequence by individual prediction on each position. This usually produces better accuracy although the results may not be coherent.- Parameters:
o
- the observation sequence.- Returns:
- the sequence labels.
-