BERT नैचुरल लैंग्वेज क्लासिफ़ायर को इंटिग्रेट करना

टास्क लाइब्रेरी का BertNLClassifier एपीआई, NLClassifier के काफ़ी मिलता-जुलता है जो इनपुट टेक्स्ट को अलग-अलग कैटगरी में रखता है. हालांकि, यह एपीआई विशेष रूप से बर्ट से जुड़े ऐसे मॉडल के लिए तैयार किया गया है जिनके लिए वर्डपीस और TFLite मॉडल के बाहर सेंटेंसपीस टोकनाइज़ेशन.

BertNLClassifier एपीआई की मुख्य सुविधाएं

  • इनपुट के रूप में एक स्ट्रिंग लेता है, स्ट्रिंग की मदद से क्लासिफ़िकेशन करता है और आउटपुट <label, Score=""> जोड़े हैं.</label,>

  • ग्राफ़ से बाहर के वर्डपीस का काम करता है या सेंटेंसपीस इनपुट टेक्स्ट पर टोकनाइज़ेशन.

साथ काम करने वाले BertNLClassifier मॉडल

ये मॉडल, BertNLClassifier API के साथ काम करते हैं.

Java में अनुमान चलाएं

पहला चरण: Gradle डिपेंडेंसी और अन्य सेटिंग इंपोर्ट करना

.tflite मॉडल फ़ाइल को, Android मॉड्यूल की ऐसेट डायरेक्ट्री में कॉपी करें जहां मॉडल को चलाया जाएगा. तय करें कि फ़ाइल कंप्रेस नहीं की जानी चाहिए, और मॉड्यूल की build.gradle फ़ाइल में TensorFlow Lite लाइब्रेरी जोड़ें:

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'
}

दूसरा चरण: एपीआई का इस्तेमाल करके अनुमान लगाना

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

सोर्स देखें कोड देखें.

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

पहला चरण: CocoaPods को इंपोर्ट करना

Podfile में TensorFlowLiteTaskText पॉड जोड़ें

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

दूसरा चरण: एपीआई का इस्तेमाल करके अनुमान लगाना

// 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 में इन्फ़रेंस चलाना

पहला चरण: पीआईपी पैकेज इंस्टॉल करना

pip install tflite-support

दूसरा चरण: मॉडल का इस्तेमाल करना

# 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'

सीएलआई डेमो टूल को आज़माएं. ये टूल, BertNLClassifier की मदद से कैसे डिज़ाइन किया गया है.

मॉडल के साथ काम करने से जुड़ी ज़रूरी शर्तें

BetNLClassifier API के लिए, ज़रूरी TFLite मॉडल वाला TFLite मॉडल होना चाहिए मेटाडेटा.

मेटाडेटा इन शर्तों के मुताबिक होना चाहिए:

  • वर्डपीस/सेंटेंसपीस टोकनाइज़र के लिएinput_process_units

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

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