[英]How to predict (classify) user sentence with BERT model and TensorflowLite
I'm trying to train a MobileBERT model with TFLite Model Maker;我正在尝试使用 TFLite Model Maker 训练 MobileBERT model; the training part is OK, the testing too (I can use the mb_model.evaluate(mb_test_data)
).训练部分还可以,测试也可以(我可以使用mb_model.evaluate(mb_test_data)
)。
But I'm totally lost on how to predict a result with a string sentence, with Python...但是我完全不知道如何使用 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
它导出/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 = np.array(np.random.random_sample(input_shape), dtype=np.int32)
,例如:
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...我没有找到任何文档,TFLite Model Maker(和官方的 BERT。nlp.data 存储库)来源的相反,很难......
I didn't find the full preprocessing used on string and tokenization process, to get the int32 list that replace the original sentence:/我没有找到用于字符串和标记化过程的完整预处理,以获取替换原句的 int32 列表:/
Thanks !谢谢 !
You can use BertNLClassifier to do the inference.您可以使用BertNLClassifier进行推理。 It will handle the pre-processing and post-processing part.它将处理预处理和后处理部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.