繁体   English   中英

Python 中来自 TensorFlow Lite 的 MobileBert

[英]MobileBert from TensorFlow Lite in Python

我从 TensorFlow 下载了 MobileBert 模型——基于移动设备的 TensorFlow Lite 的问答模型从这里:

https://www.tensorflow.org/lite/models/bert_qa/overview

仅针对 Android 提供了如何使用它的示例。 任何人都可以建议如何在 Python 中使用这个模型(用于测试目的)。 我遵循了有关如何使用 TensorFlow Lite API 的建议,但我需要弄清楚如何修改它以用于 MobileBert:

import numpy as np
import tensorflow as tf

# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="mobilebert_float_20191023.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test model on random input data.
input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)

input_data = np.array(np.random.random_sample(input_shape), dtype=np.int32)

interpreter.set_tensor(input_details[0]['index'], input_data)

interpreter.invoke()

# The function `get_tensor()` returns a copy of the tensor data.
# Use `tensor()` in order to get a pointer to the tensor.
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)

请看一下这个项目: https : //github.com/samsgates/MobileBERT它基本上是为了运行你提到的谷歌模型而设计的。 我设法使用 TF 1.14 以及安装的其他依赖项运行它(句子bert-for-tf2 ,两者都可以从 PiP 获得)。 我唯一需要调整的是删除输入/输出张量上的tf.dtypes.cast()包装器。 例如对于我改变的输入

input_ids = tf.dtypes.cast(self.get_ids(stokens),tf.int32)

input_ids = self.get_ids(stokens).astype('int32')

对于我改变的输出

end = tf.argmax(end_logits,output_type=tf.dtypes.int32).numpy()[0]

简单

end = np.argmax(end_logits)

这对我有用。 希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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