简体   繁体   English

如何使用 BERT model 和 TensorflowLite 预测(分类)用户句子

[英]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.

相关问题 如何使用 BERT model 预测与没有 label 的数据集的句子语义相似度? - How can I use BERT model to predict sentence semantic similarity to a dataset with no label? 如何从加载的 BERT tensorflow model 进行预测 - How to predict from loaded BERT tensorflow model 如何使用微调的 BERT model 进行句子编码? - How to use fine-tuned BERT model for sentence encoding? 如何从 Tensorflow 检查点 (ckpt) 文件预测 BERT-base 句子中的掩码词? - How to predict masked word in a sentence in BERT-base from Tensorflow checkpoint (ckpt) files? 如何使用预训练的 tensorflowlite 模型进行输入解释 - How to use a pretrained tensorflowlite model for input interpretation 如何使用张量流为 BERT SQuAD2.0 构建输入以使用已保存的模型进行预测 - How to build input to predict with saved model for BERT SQuAD2.0 with tensorflow 如何通过一次加载上下文进行预测并针对BERT神经网络模型中的问题动态预测答案? - How to make the predictions by loading the context once and predict the answer dynamically with regards to question in BERT neural network model? tensorflow tf.contrib.learn.SVM如何重新加载训练后的模型并使用预测对新数据进行分类 - How tensorflow tf.contrib.learn.SVM reload trained model and use predict to classify new data 如何使用 BERT 预测空字符串的概率 - How to predict the probability of an empty string using BERT 需要微调 BERT 模型以预测丢失的单词 - Need to Fine Tune a BERT Model to Predict Missing Words
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM