整合自然語言分類器

Task Library 的 NLClassifier API 會將輸入文字分類至不同類別,是用途廣泛且可設定的 API,可處理大多數的文字分類模型。

NLClassifier API 的主要功能

  • 以單一字串做為輸入內容,並使用該字串執行分類,然後輸出 <label, score=""> 配對做為分類結果。</label,>

  • 可對輸入文字進行選填的 Regex 權杖化。

  • 可設定以適應不同的分類模型。

支援的 NLClassifier 模型

下列機型保證與 NLClassifier API 相容。

在 Java 中執行推論作業

如需在 Android 應用程式中使用 NLClassifier 的範例,請參閱文字分類參考應用程式

步驟 1:匯入 Gradle 依附元件和其他設定

.tflite 模型檔案複製到要執行模型的 Android 模組資產目錄。指定檔案不應壓縮,並將 TensorFlow Lite 程式庫新增至模組的 build.gradle 檔案:

android {
    // Other settings

    // Specify tflite file should not be compressed for the app apk
    aaptOptions {
        noCompress "tflite"
    }

}

dependencies {
    // Other dependencies

    // Import the Task Vision Library dependency
    implementation 'org.tensorflow:tensorflow-lite-task-text:0.4.4'
    // Import the GPU delegate plugin Library for GPU inference
    implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin:0.4.4'
}

步驟 2:使用 API 執行推論

// Initialization, use NLClassifierOptions to configure input and output tensors
NLClassifierOptions options =
    NLClassifierOptions.builder()
        .setBaseOptions(BaseOptions.builder().useGpu().build())
        .setInputTensorName(INPUT_TENSOR_NAME)
        .setOutputScoreTensorName(OUTPUT_SCORE_TENSOR_NAME)
        .build();
NLClassifier classifier =
    NLClassifier.createFromFileAndOptions(context, modelFile, options);

// Run inference
List<Category> results = classifier.classify(input);

如要進一步瞭解如何設定 NLClassifier,請參閱原始碼

在 Swift 中執行推論

步驟 1:匯入 CocoaPods

在 Podfile 中新增 TensorFlowLiteTaskText Pod

target 'MySwiftAppWithTaskAPI' do
  use_frameworks!
  pod 'TensorFlowLiteTaskText', '~> 0.4.4'
end

步驟 2:使用 API 執行推論

// Initialization
var modelOptions:TFLNLClassifierOptions = TFLNLClassifierOptions()
modelOptions.inputTensorName = inputTensorName
modelOptions.outputScoreTensorName = outputScoreTensorName
let nlClassifier = TFLNLClassifier.nlClassifier(
      modelPath: modelPath,
      options: modelOptions)

// Run inference
let categories = nlClassifier.classify(text: input)

詳情請參閱原始碼

以 C++ 執行推論

// Initialization
NLClassifierOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
std::unique_ptr<NLClassifier> classifier = NLClassifier::CreateFromOptions(options).value();

// Run inference with your input, `input_text`.
std::vector<core::Category> categories = classifier->Classify(input_text);

詳情請參閱原始碼

在 Python 中執行推論

步驟 1:安裝 pip 套件

pip install tflite-support

步驟 2:使用模型

# Imports
from tflite_support.task import text

# Initialization
classifier = text.NLClassifier.create_from_file(model_path)

# Run inference
text_classification_result = classifier.classify(text)

如要進一步瞭解如何設定 NLClassifier,請參閱原始碼

搜尋結果範例

以下是電影評論模型的分類結果範例。

輸入:「真是浪費我的時間。」

輸出:

category[0]: 'Negative' : '0.81313'
category[1]: 'Positive' : '0.18687'

使用自己的模型和測試資料,試用簡單的 NLClassifier CLI 示範工具

模型相容性規定

視用途而定,NLClassifier API 可以載入含有或不含 TFLite 模型中繼資料的 TFLite 模型。如要瞭解如何使用 TensorFlow Lite Metadata Writer API 為自然語言分類器建立中繼資料,請參閱相關範例。

相容機型應符合下列規定:

  • 輸入張量:(kTfLiteString/kTfLiteInt32)

    • 模型的輸入內容應為 kTfLiteString 張量原始輸入字串,或是原始輸入字串的 kTfLiteInt32 張量 (適用於正規運算式權杖化索引)。
    • 如果輸入類型為 kTfLiteString,模型就不需要中繼資料
    • 如果輸入類型為 kTfLiteInt32,則必須在輸入張量的中繼資料中設定 RegexTokenizer
  • 輸出分數張量:(kTfLiteUInt8/kTfLiteInt8/kTfLiteInt16/kTfLiteFloat32/kTfLiteFloat64)

    • 每個分類類別的分數都必須輸出張量。

    • 如果類型是其中一種 Int 類型,請將其反量化為 double/float,以對應平台

    • 輸出張量的對應 Metadata 中可包含選用的相關聯檔案,以提供類別標籤。該檔案應為純文字檔案,每行一個標籤,且標籤數量應與模型輸出的類別數量相符。請參閱標籤檔案範例

  • 輸出標籤張量:(kTfLiteString/kTfLiteInt32)

    • 每個類別的標籤選填輸出張量,長度應與輸出分數張量相同。如果沒有這個張量,API 會使用分數索引做為類別名稱。

    • 如果相關聯的標籤檔案位於輸出分數張量的中繼資料中,系統會忽略這個值。