工作資料庫的 NLClassifier
API 會將輸入文字分類為不同的類別,而且是功能多元且可設定的 API,可處理大多數的文字分類模型。
NLClassifier API 的主要功能
接受單一字串做為輸入,使用該字串執行分類並輸出
輸入文字可使用選用的 Regex 權杖化。
可設定以調整不同的分類模型。
支援的 NLClassifier 模型
下列模型保證與 NLClassifier
API 相容。
電影評論情感分類模型。
具有
average_word_vec
規格的模型,由 TensorFlow Lite Model Maker for text Classification 建立。符合模型相容性需求的自訂模型。
在 Java 中執行推論
請參閱 Text Classification 參考應用程式,瞭解如何在 Android 應用程式中使用 NLClassifier
的範例。
步驟 1:匯入 Gradle 依附元件和其他設定
將 .tflite
模型檔案複製到將執行模型的 Android 模組的 assets 目錄。指定不應壓縮檔案,並將 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 (NNAPI is included)
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 中繼資料寫入器 API 為自然語言分類器建立中繼資料的範例,請參閱相關說明。
相容模型應符合下列需求
輸入張量:(kTfLiteString/kTfLiteInt32)
輸出分數張量:(kTfLiteUInt8/kTfLiteInt8/kTfLiteInt16/kTfLiteFloat32/kTfLiteFloat64)
輸出標籤張量:(kTfLiteString/kTfLiteInt32)
每個類別的標籤的選用輸出張量,長度應與輸出分數張量相同。如果此張量不存在,API 會使用分數索引做為類別名稱。
如果輸出分數張量的中繼資料中存在關聯的標籤檔案,則會忽略此張量。