Back to Tensorflow

BertNLClassifier

tensorflow/lite/g3doc/api_docs/java/org/tensorflow/lite/task/text/nlclassifier/BertNLClassifier.html

2.21.013.0 KB
Original Source

public class BertNLClassifier

Classifier API for NLClassification tasks with Bert models, categorizes string into different classes. The API expects a Bert based TFLite model with metadata populated.

The metadata should contain the following information:

  • 1 input_process_unit for Wordpiece/Sentencepiece Tokenizer.
  • 3 input tensors with names "ids", "mask" and "segment_ids".
  • 1 output tensor of type float32[1, 2], with a optionally attached label file. If a label file is attached, the file should be a plain text file with one label per line, the number of labels should match the number of categories the model outputs.

Nested Classes

| class | BertNLClassifier.BertNLClassifierOptions | Options to configure BertNLClassifier. |

Public Methods

| List<Category> | classify(String text) Performs classification on a string input, returns classified Categorys.

| | static BertNLClassifier | createFromBuffer(ByteBuffer modelBuffer) Creates BertNLClassifier with a model buffer and default BertNLClassifier.BertNLClassifierOptions.

| | static BertNLClassifier | createFromBufferAndOptions(ByteBuffer modelBuffer, BertNLClassifier.BertNLClassifierOptions options) Creates BertNLClassifier with a model buffer and BertNLClassifier.BertNLClassifierOptions.

| | static BertNLClassifier | createFromFile(Context context, String modelPath) Creates BertNLClassifier from a model file with metadata and default BertNLClassifier.BertNLClassifierOptions.

| | static BertNLClassifier | createFromFile(File modelFile) Creates BertNLClassifier from a File object with metadata and default BertNLClassifier.BertNLClassifierOptions.

| | static BertNLClassifier | createFromFileAndOptions(File modelFile, BertNLClassifier.BertNLClassifierOptions options) Creates BertNLClassifier from a File object with metadata and BertNLClassifier.BertNLClassifierOptions.

| | static BertNLClassifier | createFromFileAndOptions(Context context, String modelPath, BertNLClassifier.BertNLClassifierOptions options) Creates BertNLClassifier from a model file with metadata and BertNLClassifier.BertNLClassifierOptions.

|

Inherited Methods

From class org.tensorflow.lite.task.core.BaseTaskApi

| synchronized void | close() Release the memory allocated from C++ and deregister the library from the static holder.

| | long | getNativeHandle() | | boolean | isClosed() |

From class java.lang.Object

| boolean | equals(Object arg0) | | final Class<?> | getClass() | | int | hashCode() | | final void | notify() | | final void | notifyAll() | | String | toString() | | final void | wait(long arg0, int arg1) | | final void | wait(long arg0) | | final void | wait() |

From interface java.io.Closeable

| abstract void | close() |

From interface java.lang.AutoCloseable

| abstract void | close() |

Public Methods

public List<Category> classify (String text)

Performs classification on a string input, returns classified Categorys.

Parameters

| text | input text to the model. |

Returns
  • A list of Category results.

public static BertNLClassifier createFromBuffer (ByteBuffer modelBuffer)

Creates BertNLClassifier with a model buffer and default BertNLClassifier.BertNLClassifierOptions.

Parameters

| modelBuffer | a direct ByteBuffer or a MappedByteBuffer of the model |

Returns
  • a BertNLClassifier instance
Throws

| IllegalArgumentException | if the model buffer is not a direct ByteBuffer or a MappedByteBuffer | | IllegalStateException | if there is an internal error | | RuntimeException | if there is an otherwise unspecified error |

public static BertNLClassifier createFromBufferAndOptions (ByteBuffer modelBuffer, BertNLClassifier.BertNLClassifierOptions options)

Creates BertNLClassifier with a model buffer and BertNLClassifier.BertNLClassifierOptions.

Parameters

| modelBuffer | a direct ByteBuffer or a MappedByteBuffer of the model | | options | to configure the classifier |

Returns
  • a BertNLClassifier instance
Throws

| IllegalArgumentException | if the model buffer is not a direct ByteBuffer or a MappedByteBuffer | | IllegalStateException | if there is an internal error | | RuntimeException | if there is an otherwise unspecified error |

public static BertNLClassifier createFromFile (Context context, String modelPath)

Creates BertNLClassifier from a model file with metadata and default BertNLClassifier.BertNLClassifierOptions.

Parameters

| context | Android context | | modelPath | Path to the classification model |

Returns
  • a BertNLClassifier instance
Throws

| IOException | If model file fails to load | | IllegalArgumentException | if an argument is invalid | | IllegalStateException | if there is an internal error | | RuntimeException | if there is an otherwise unspecified error |

public static BertNLClassifier createFromFile (File modelFile)

Creates BertNLClassifier from a File object with metadata and default BertNLClassifier.BertNLClassifierOptions.

Parameters

| modelFile | The classification model File instance |

Returns
  • a BertNLClassifier instance
Throws

| IOException | If model file fails to load | | IllegalArgumentException | if an argument is invalid | | IllegalStateException | if there is an internal error | | RuntimeException | if there is an otherwise unspecified error |

public static BertNLClassifier createFromFileAndOptions (File modelFile, BertNLClassifier.BertNLClassifierOptions options)

Creates BertNLClassifier from a File object with metadata and BertNLClassifier.BertNLClassifierOptions.

Parameters

| modelFile | The classification model File instance | | options | to configure the classifier |

Returns
  • a BertNLClassifier instance
Throws

| IOException | If model file fails to load | | IllegalArgumentException | if an argument is invalid | | IllegalStateException | if there is an internal error | | RuntimeException | if there is an otherwise unspecified error |

public static BertNLClassifier createFromFileAndOptions (Context context, String modelPath, BertNLClassifier.BertNLClassifierOptions options)

Creates BertNLClassifier from a model file with metadata and BertNLClassifier.BertNLClassifierOptions.

Parameters

| context | Android context. | | modelPath | Path to the classification model | | options | to configure the classifier |

Returns
  • a BertNLClassifier instance
Throws

| IOException | If model file fails to load | | IllegalArgumentException | if an argument is invalid | | IllegalStateException | if there is an internal error | | RuntimeException | if there is an otherwise unspecified error |