![](/img/trans.png)
[英]How can I use BERT model to predict sentence semantic similarity to a dataset with no label?
[英]How to predict (classify) user sentence with BERT model and TensorflowLite
我正在尝试使用 TFLite Model Maker 训练 MobileBERT model; 训练部分还可以,测试也可以(我可以使用mb_model.evaluate(mb_test_data)
)。
但是我完全不知道如何使用 Python 来预测字符串句子的结果......
这是一个训练示例脚本:
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='/')
它导出/model.tflite
我可以用这样的现有句子进行测试:
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)
但我想使用自定义句子而不是input_data = np.array(np.random.random_sample(input_shape), dtype=np.int32)
,例如:
input_data = "My user sentence"
output_data = interpreter.predict(input_data)
有人知道该怎么做吗? 我没有找到任何文档,TFLite Model Maker(和官方的 BERT。nlp.data 存储库)来源的相反,很难......
我没有找到用于字符串和标记化过程的完整预处理,以获取替换原句的 int32 列表:/
谢谢 !
您可以使用BertNLClassifier进行推理。 它将处理预处理和后处理部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.