Class LabelConverter

java.lang.Object
org.tribuo.interop.tensorflow.LabelConverter
All Implemented Interfaces:
com.oracle.labs.mlrg.olcut.config.Configurable, com.oracle.labs.mlrg.olcut.provenance.Provenancable<com.oracle.labs.mlrg.olcut.provenance.ConfiguredObjectProvenance>, Serializable, OutputConverter<Label>, ProtoSerializable<org.tribuo.interop.tensorflow.protos.OutputConverterProto>

public class LabelConverter extends Object implements OutputConverter<Label>
Can convert a Label into a Tensor containing one hot encoding of the label and can convert a TFloat16 or TFloat32 into a Prediction or a Label.
See Also:
  • Field Details

    • CURRENT_VERSION

      public static final int CURRENT_VERSION
      Protobuf serialization version.
      See Also:
  • Constructor Details

    • LabelConverter

      public LabelConverter()
      Constructs a LabelConverter.
  • Method Details

    • deserializeFromProto

      public static LabelConverter deserializeFromProto(int version, String className, com.google.protobuf.Any message)
      Deserialization factory.
      Parameters:
      version - The serialized object version.
      className - The class name.
      message - The serialized data.
      Returns:
      The deserialized object.
    • serialize

      public org.tribuo.interop.tensorflow.protos.OutputConverterProto serialize()
      Description copied from interface: ProtoSerializable
      Serializes this object to a protobuf.
      Specified by:
      serialize in interface ProtoSerializable<org.tribuo.interop.tensorflow.protos.OutputConverterProto>
      Returns:
      The protobuf.
    • loss

      public BiFunction<org.tensorflow.op.Ops,com.oracle.labs.mlrg.olcut.util.Pair<org.tensorflow.op.core.Placeholder<? extends org.tensorflow.types.family.TNumber>,org.tensorflow.Operand<org.tensorflow.types.family.TNumber>>,org.tensorflow.Operand<org.tensorflow.types.family.TNumber>> loss()
      Returns a cross-entropy loss.
      Specified by:
      loss in interface OutputConverter<Label>
      Returns:
      The cross-entropy loss.
    • outputTransformFunction

      public <V extends org.tensorflow.types.family.TNumber> BiFunction<org.tensorflow.op.Ops,org.tensorflow.Operand<V>,org.tensorflow.op.Op> outputTransformFunction()
      Applies a softmax.
      Specified by:
      outputTransformFunction in interface OutputConverter<Label>
      Type Parameters:
      V - The softmax input type (should be TFloat32).
      Returns:
      A function which applies a softmax.
    • convertToPrediction

      public Prediction<Label> convertToPrediction(org.tensorflow.Tensor tensor, ImmutableOutputInfo<Label> outputIDInfo, int numValidFeatures, Example<Label> example)
      Description copied from interface: OutputConverter
      Converts a Tensor into a Prediction.
      Specified by:
      convertToPrediction in interface OutputConverter<Label>
      Parameters:
      tensor - The tensor to convert.
      outputIDInfo - The output info to use to identify the outputs.
      numValidFeatures - The number of valid features used by the prediction.
      example - The example to insert into the prediction.
      Returns:
      A prediction object.
    • convertToOutput

      public Label convertToOutput(org.tensorflow.Tensor tensor, ImmutableOutputInfo<Label> outputIDInfo)
      Description copied from interface: OutputConverter
      Converts a Tensor into the specified output type.
      Specified by:
      convertToOutput in interface OutputConverter<Label>
      Parameters:
      tensor - The tensor to convert.
      outputIDInfo - The output info to use to identify the outputs.
      Returns:
      A output.
    • convertToBatchPrediction

      public List<Prediction<Label>> convertToBatchPrediction(org.tensorflow.Tensor tensor, ImmutableOutputInfo<Label> outputIDInfo, int[] numValidFeatures, List<Example<Label>> examples)
      Description copied from interface: OutputConverter
      Converts a Tensor containing multiple outputs into a list of Predictions.
      Specified by:
      convertToBatchPrediction in interface OutputConverter<Label>
      Parameters:
      tensor - The tensor to convert.
      outputIDInfo - The output info to use to identify the outputs.
      numValidFeatures - The number of valid features used by the prediction.
      examples - The example to insert into the prediction.
      Returns:
      A list of predictions.
    • convertToBatchOutput

      public List<Label> convertToBatchOutput(org.tensorflow.Tensor tensor, ImmutableOutputInfo<Label> outputIDInfo)
      Description copied from interface: OutputConverter
      Converts a Tensor containing multiple outputs into a list of Outputs.
      Specified by:
      convertToBatchOutput in interface OutputConverter<Label>
      Parameters:
      tensor - The tensor to convert.
      outputIDInfo - The output info to use to identify the outputs.
      Returns:
      A list of outputs.
    • convertToTensor

      public org.tensorflow.Tensor convertToTensor(Label example, ImmutableOutputInfo<Label> outputIDInfo)
      Description copied from interface: OutputConverter
      Converts an Output into a Tensor representing it's output.
      Specified by:
      convertToTensor in interface OutputConverter<Label>
      Parameters:
      example - The output to convert.
      outputIDInfo - The output info to use to identify the outputs.
      Returns:
      A Tensor representing this output.
    • convertToTensor

      public org.tensorflow.Tensor convertToTensor(List<Example<Label>> examples, ImmutableOutputInfo<Label> outputIDInfo)
      Description copied from interface: OutputConverter
      Converts a list of Example into a Tensor representing all the outputs in the list. It accepts a list of Example rather than a list of Output for efficiency reasons.
      Specified by:
      convertToTensor in interface OutputConverter<Label>
      Parameters:
      examples - The examples to convert.
      outputIDInfo - The output info to use to identify the outputs.
      Returns:
      A Tensor representing all the supplied Outputs.
    • generatesProbabilities

      public boolean generatesProbabilities()
      Description copied from interface: OutputConverter
      Does this OutputConverter generate probabilities.
      Specified by:
      generatesProbabilities in interface OutputConverter<Label>
      Returns:
      True if it produces a probability distribution in the Prediction.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getProvenance

      public com.oracle.labs.mlrg.olcut.provenance.ConfiguredObjectProvenance getProvenance()
      Specified by:
      getProvenance in interface com.oracle.labs.mlrg.olcut.provenance.Provenancable<com.oracle.labs.mlrg.olcut.provenance.ConfiguredObjectProvenance>
    • getTypeWitness

      public Class<Label> getTypeWitness()
      Description copied from interface: OutputConverter
      The type witness used when deserializing the TensorFlow model from a protobuf.

      The default implementation throws UnsupportedOperationException for compatibility with implementations which don't use protobuf serialization. This implementation will be removed in the next major version of Tribuo.

      Specified by:
      getTypeWitness in interface OutputConverter<Label>
      Returns:
      The output class this object produces.