BERT 自然言語分類器を統合する

タスク ライブラリの BertNLClassifier API は NLClassifier と非常によく似ている 複数のカテゴリに分類できます。ただし、この API は ワードピースとメモリを必要とする BERT 関連モデル向けに特別に TFLite モデル外の文のトークン化。

BertNLClassifier API の主な機能

  • 単一の文字列を入力として受け取り、その文字列を使用して分類を行い、 <label, score=""> を出力する分類結果として返されます。</label,>

  • グラフ外のワードピースを実行 または 入力テキストのトークン化も学習します。

サポートされている BertNLClassifier モデル

次のモデルは BertNLClassifier API と互換性があります。

Java で推論を実行する

ステップ 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 Text Library dependency
    implementation 'org.tensorflow:tensorflow-lite-task-text:0.4.4'
}

ステップ 2: API を使用して推論を実行する

// Initialization
BertNLClassifierOptions options =
    BertNLClassifierOptions.builder()
        .setBaseOptions(BaseOptions.builder().setNumThreads(4).build())
        .build();
BertNLClassifier classifier =
    BertNLClassifier.createFromFileAndOptions(context, modelFile, options);

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

出典を参照 コード をご覧ください。

Swift で推論を実行する

ステップ 1: CocoaPods をインポートする

Podfile に TensorFlowLiteTaskText Pod を追加する

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

ステップ 2: API を使用して推論を実行する

// Initialization
let bertNLClassifier = TFLBertNLClassifier.bertNLClassifier(
      modelPath: bertModelPath)

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

出典を参照 コード をご覧ください。

C++ で推論を実行する

// Initialization
BertNLClassifierOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
std::unique_ptr<BertNLClassifier> classifier = BertNLClassifier::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.BertNLClassifier.create_from_file(model_path)

# Run inference
text_classification_result = classifier.classify(text)

出典を参照 コード BertNLClassifier を構成するその他のオプションをご覧ください。

検索結果の例

このスライドは、 Model Maker の MobileBert モデル。

入力: 「魅力的で、しばしば影響を及ぼす道のり」

出力:

category[0]: 'negative' : '0.00006'
category[1]: 'positive' : '0.99994'

シンプルなCLI デモツール: BertNLClassifier 独自のモデルとテストデータで トレーニングできます

モデルの互換性要件

BetNLClassifier API は、必須の TFLite モデルを含む TFLite モデルを想定しています。 メタデータ

メタデータは次の要件を満たす必要があります。

  • Wordpiece/Sentencepiece トークナイザ用の input_process_units

  • 名前が「ids」、「mask」の 3 つの入力テンソル「segment_ids」出力を トークナイザ

  • 必要に応じてラベルファイルが付加された、float32 型の 1 つの出力テンソル。もし ラベルファイルが添付されている場合は、1 つのラベルを含む書式なしテキスト ファイルである必要があります。 ラベルの数はカテゴリ数と一致する必要があります モデルの出力です。