Class CRFModel

java.lang.Object
org.tribuo.sequence.SequenceModel<Label>
All Implemented Interfaces:
com.oracle.labs.mlrg.olcut.provenance.Provenancable<org.tribuo.provenance.ModelProvenance>, Serializable, org.tribuo.protos.ProtoSerializable<org.tribuo.protos.core.SequenceModelProto>

An inference time model for a linear chain CRF trained using SGD.

Can be switched to use Viterbi, belief propagation, or constrained BP at test time. By default it uses Viterbi.

See:

 Lafferty J, McCallum A, Pereira FC.
 "Conditional Random Fields: Probabilistic Models for Segmenting and Labeling Sequence Data"
 Proceedings of the 18th International Conference on Machine Learning 2001 (ICML 2001).
 
See Also:
  • Field Details

  • Method Details

    • deserializeFromProto

      public static CRFModel 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 the message.
    • setConfidenceType

      Sets the inference method used for confidence prediction. If CONSTRAINED_BP uses the constrained belief propagation algorithm from Culotta and McCallum 2004, if MULTIPLY multiplies the maximum marginal for each token, if NONE uses Viterbi.
      Parameters:
      type - Enum specifying the confidence type.
    • getFeatureWeights

      public DenseVector getFeatureWeights(int featureID)
      Get a copy of the weights for feature featureID.
      Parameters:
      featureID - The feature ID.
      Returns:
      The per class weights.
    • getFeatureWeights

      public DenseVector getFeatureWeights(String featureName)
      Get a copy of the weights for feature named featureName.
      Parameters:
      featureName - The feature name.
      Returns:
      The per class weights.
    • predict

      public List<org.tribuo.Prediction<Label>> predict(org.tribuo.sequence.SequenceExample<Label> example)
      Specified by:
      predict in class org.tribuo.sequence.SequenceModel<Label>
    • getTopFeatures

      public Map<String, List<com.oracle.labs.mlrg.olcut.util.Pair<String,Double>>> getTopFeatures(int n)
      Specified by:
      getTopFeatures in class org.tribuo.sequence.SequenceModel<Label>
    • scoreSubsequences

      public <SUB extends ConfidencePredictingSequenceModel.Subsequence> List<Double> scoreSubsequences(org.tribuo.sequence.SequenceExample<Label> example, List<org.tribuo.Prediction<Label>> predictions, List<SUB> subsequences)
      Specified by:
      scoreSubsequences in class ConfidencePredictingSequenceModel
    • scoreChunks

      public List<Double> scoreChunks(org.tribuo.sequence.SequenceExample<Label> example, List<Chunk> chunks)
      Scores the chunks using constrained belief propagation.
      Parameters:
      example - The example to score.
      chunks - The predicted chunks.
      Returns:
      The scores.
    • generateWeightsString

      Generates a human readable string containing all the weights in this model.
      Returns:
      A string containing all the weight values.
    • serialize

      public org.tribuo.protos.core.SequenceModelProto serialize()
      Specified by:
      serialize in interface org.tribuo.protos.ProtoSerializable<org.tribuo.protos.core.SequenceModelProto>
      Overrides:
      serialize in class org.tribuo.sequence.SequenceModel<Label>
    • convert

      @Deprecated public static <T extends org.tribuo.Output<T>> SparseVector[] convert(org.tribuo.sequence.SequenceExample<T> example, org.tribuo.ImmutableFeatureMap featureIDMap)
      Deprecated.
      Converts a SequenceExample into an array of SparseVectors suitable for CRF prediction.
      Type Parameters:
      T - The type parameter of the sequence example.
      Parameters:
      example - The sequence example to convert
      featureIDMap - The feature id map, used to discover the number of features.
      Returns:
      An array of SparseVector.
    • convert

      @Deprecated public static com.oracle.labs.mlrg.olcut.util.Pair<int[], SparseVector[]> convert(org.tribuo.sequence.SequenceExample<Label> example, org.tribuo.ImmutableFeatureMap featureIDMap, org.tribuo.ImmutableOutputInfo<Label> labelIDMap)
      Deprecated.
      Converts a SequenceExample into an array of SparseVectors and labels suitable for CRF prediction.
      Parameters:
      example - The sequence example to convert
      featureIDMap - The feature id map, used to discover the number of features.
      labelIDMap - The label id map, used to get the index of the labels.
      Returns:
      A Pair of an int array of labels and an array of SparseVector.
    • convertToVector

      public static <T extends org.tribuo.Output<T>> SGDVector[] convertToVector(org.tribuo.sequence.SequenceExample<T> example, org.tribuo.ImmutableFeatureMap featureIDMap)
      Converts a SequenceExample into an array of SGDVectors suitable for CRF prediction.
      Type Parameters:
      T - The type parameter of the sequence example.
      Parameters:
      example - The sequence example to convert
      featureIDMap - The feature id map, used to discover the number of features.
      Returns:
      An array of SGDVector.
    • convertToVector

      public static com.oracle.labs.mlrg.olcut.util.Pair<int[], SGDVector[]> convertToVector(org.tribuo.sequence.SequenceExample<Label> example, org.tribuo.ImmutableFeatureMap featureIDMap, org.tribuo.ImmutableOutputInfo<Label> labelIDMap)
      Converts a SequenceExample into an array of SGDVectors and labels suitable for CRF prediction.
      Parameters:
      example - The sequence example to convert
      featureIDMap - The feature id map, used to discover the number of features.
      labelIDMap - The label id map, used to get the index of the labels.
      Returns:
      A Pair of an int array of labels and an array of SparseVector.