简体   繁体   中英

How to predict (classify) user sentence with BERT model and TensorflowLite

I'm trying to train a MobileBERT model with TFLite Model Maker; the training part is OK, the testing too (I can use the mb_model.evaluate(mb_test_data) ).

But I'm totally lost on how to predict a result with a string sentence, with Python...

Here is a training sample script:

import os
import tensorflow as tf
assert tf.__version__.startswith('2')
from tflite_model_maker import configs
from tflite_model_maker import ExportFormat
from tflite_model_maker import model_spec
from tflite_model_maker import text_classifier
from tflite_model_maker.text_classifier import DataLoader

mb_spec = model_spec.get('mobilebert_classifier')
mb_train_data = DataLoader.from_csv(
    filename=os.path.join(os.path.join(data_dir, 'nlu_train.tsv')),
    text_column='sentence',
    label_column='label',
    model_spec=mb_spec,
    delimiter='\t',
    is_training=True)
mb_test_data = DataLoader.from_csv(
    filename=os.path.join(os.path.join(data_dir, 'nlu_test.tsv')),
    text_column='sentence',
    label_column='label',
    model_spec=mb_spec,
    delimiter='\t',
    is_training=False)
mb_model = text_classifier.create(mb_train_data, model_spec=mb_spec, epochs=30, batch_size=8)
config = configs.QuantizationConfig.for_float16()
config._experimental_new_quantizer = True
mb_model.export(export_dir='/')

It exports /model.tflite

I can test with an existing sentence like that:

import numpy as np
import tensorflow as tf

interpreter = tf.lite.Interpreter(model_path="nlu (6).tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.int32)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)

But instead of input_data = np.array(np.random.random_sample(input_shape), dtype=np.int32) , I want to use a custom sentence, like:

input_data = "My user sentence"
output_data = interpreter.predict(input_data)

Does someone knows how to do this? I don't find any documentation, the reverse on TFLite Model Maker (and BERT on official.nlp.data repository) sources,is hard...

I didn't find the full preprocessing used on string and tokenization process, to get the int32 list that replace the original sentence:/

Thanks !

You can use BertNLClassifier to do the inference. It will handle the pre-processing and post-processing part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM