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