बीईआरटी प्राकृतिक भाषा वर्गीकरण को एकीकृत करें

टास्क लाइब्रेरी BertNLClassifier एपीआई NLClassifier के समान है जो इनपुट टेक्स्ट को विभिन्न श्रेणियों में वर्गीकृत करता है, सिवाय इसके कि यह एपीआई विशेष रूप से बर्ट से संबंधित मॉडल के लिए तैयार किया गया है जिसके लिए टीएफलाइट मॉडल के बाहर वर्डपीस और सेंटेंसपीस टोकननाइजेशन की आवश्यकता होती है।

BertNLClassifier API की मुख्य विशेषताएं

  • इनपुट के रूप में एक स्ट्रिंग लेता है, स्ट्रिंग और आउटपुट के साथ वर्गीकरण करता है वर्गीकरण परिणाम के रूप में जोड़े।

  • इनपुट टेक्स्ट पर आउट-ऑफ़-ग्राफ़ वर्डपीस या सेंटेंसपीस टोकननाइज़ेशन करता है।

समर्थित BertNLClassifier मॉडल

निम्नलिखित मॉडल BertNLClassifier API के साथ संगत हैं।

जावा में अनुमान चलाएँ

चरण 1: ग्रैडल निर्भरता और अन्य सेटिंग्स आयात करें

एंड्रॉइड मॉड्यूल की संपत्ति निर्देशिका में .tflite मॉडल फ़ाइल की प्रतिलिपि बनाएँ जहां मॉडल चलाया जाएगा। निर्दिष्ट करें कि फ़ाइल को संपीड़ित नहीं किया जाना चाहिए, और 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 (NNAPI is included)
    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);

अधिक जानकारी के लिए स्रोत कोड देखें।

स्विफ्ट में अनुमान चलाएँ

चरण 1: कोकोपोड्स आयात करें

पॉडफाइल में TensorFlowLiteTaskText पॉड जोड़ें

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)

अधिक जानकारी के लिए स्रोत कोड देखें।

सी ++ में अनुमान चलाएं

// 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);

अधिक जानकारी के लिए स्रोत कोड देखें।

पायथन में अनुमान चलाएँ

चरण 1: पाइप पैकेज स्थापित करें

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 को कॉन्फ़िगर करने के अधिक विकल्पों के लिए स्रोत कोड देखें।

उदाहरण परिणाम

यहां मॉडल मेकर से मोबाइलबर्ट मॉडल का उपयोग करके मूवी समीक्षाओं के वर्गीकरण परिणामों का एक उदाहरण दिया गया है।

इनपुट: "यह एक आकर्षक और अक्सर प्रभावित करने वाली यात्रा है"

आउटपुट:

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

अपने स्वयं के मॉडल और परीक्षण डेटा के साथ BertNLClassifier के लिए सरल CLI डेमो टूल आज़माएं

मॉडल संगतता आवश्यकताएँ

BetNLClassifier API अनिवार्य TFLite मॉडल मेटाडेटा के साथ TFLite मॉडल की अपेक्षा करता है।

मेटाडेटा को निम्नलिखित आवश्यकताओं को पूरा करना चाहिए:

  • Wordpiece/Sentencepiece Tokenizer के लिए input_process_units

  • टोकननाइज़र के आउटपुट के लिए "आईडी", "मास्क" और "सेगमेंट_आईड्स" नामों के साथ 3 इनपुट टेंसर

  • वैकल्पिक रूप से संलग्न लेबल फ़ाइल के साथ, फ्लोट32 प्रकार का 1 आउटपुट टेंसर। यदि एक लेबल फ़ाइल संलग्न है, तो फ़ाइल एक सादा पाठ फ़ाइल होनी चाहिए जिसमें प्रति पंक्ति एक लेबल हो और लेबल की संख्या मॉडल आउटपुट के रूप में श्रेणियों की संख्या से मेल खाना चाहिए।