为什么tflite模型输出形状与T5ForConditionalGeneration转换的原始模型不同?



T5ForConditionalGeneration模型将英语翻译成德语

from transformers import T5TokenizerFast, T5ForConditionalGeneration
tokenizer = T5TokenizerFast.from_pretrained("t5-small")
model = T5ForConditionalGeneration.from_pretrained("t5-small")
input_ids = tokenizer("translate English to German: the flowers are wonderful.", return_tensors="pt").input_ids
outputs = model.generate(input_ids)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

输出:模具Blumen sind wunderbar

输入形状

input_ids.shape

输出:火炬。大小([11])

<输出形状/strong>

outputs.shape

输出:火炬。大小([7])

Save预训练模型

!mkdir /content/test
model.save_pretrained('/content/test')

从预训练加载TFT5Model模型

from transformers import TFT5Model
t5model = TFT5Model.from_pretrained('/content/test',from_pt=True)
!mkdir /content/test/t5
t5model.save('/content/test/t5')

转换TFT5Model为TFlite

import tensorflow as tf
saved_model_dir = '/content/test/t5'
!mkdir  /content/test/tflite
tflite_model_path = '/content/test/tflite/model.tflite'
# Convert the model
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.experimental_new_converter = True
converter.experimental_new_quantizer = True
converter.experimental_new_dynamic_range_quantizer = True
converter.allow_custom_ops=True
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
]
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()
#print(tflite_model)
print(type(tflite_model))

# Save the model
with open(tflite_model_path, 'wb') as f:
f.write(tflite_model)

加载TFLite模型

import numpy as np
import tensorflow as tf
tflite_model_path = '/content/test/tflite/model.tflite'
# Load the TFLite model and allocate tensors
interpreter = tf.lite.Interpreter(model_path=tflite_model_path)
interpreter.resize_tensor_input(0, [1,5], strict=True)
interpreter.resize_tensor_input(1, [1,5], strict=True)
interpreter.resize_tensor_input(2, [1,5], strict=True)
interpreter.resize_tensor_input(3, [1,5], strict=True)
interpreter.allocate_tensors()
# Get input and output tensors
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_shape = input_details[0]['shape']
#print the output
input_data = np.array(np.random.random_sample((input_shape)), dtype=np.int64)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])

获取输出形状

print(output_data.shape)

输出:(1,8,5,64)

预期如下:(1,7)

谁能告诉我我哪里做错了?

tflite模型的输出形状与T5ForConditionalGeneration模型完全不同

输出:(1,8,5,64)

预期如下:(1,7)

问题已修复,可以使用以下代码:

输出应该是keras_output。Logits如下面的代码

model = TFGPT2LMHeadModel.from_pretrained('gpt2') # or 'distilgpt2'
input = tf.keras.Input([ 64 ], batch_size=1, dtype=tf.int32)
keras_output = model(input, training=False)
model = tf.keras.Model(input, keras_output.logits)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
# For FP16 quantization:
# converter.optimizations = [tf.lite.Optimize.DEFAULT]
# converter.target_spec.supported_types = [tf.float16]
tflite_model = converter.convert()
open("model.tflite", "wb").write(tflite_model)

相关内容

  • 没有找到相关文章

最新更新