TensorFlow Lite 转换器:'Convert concrete functions' 在官方教程中抛出错误

TensorFlow Lite converter: 'Convert concrete functions' throws error in official tutorial

我想使用 from_concrete_functions() 将自定义函数转换为 TF Lite 模型。为了熟悉这一点,我通读了文档,但无法从 TF 网站 (https://www.tensorflow.org/lite/convert#convert_concrete_functions_) 执行示例代码。

示例代码:

import tensorflow as tf
# Create a model using low-level tf.* APIs
class Squared(tf.Module):
  @tf.function
  def __call__(self, x):
    return tf.square(x)
model = Squared()
# (ro run your model) result = Squared(5.0) # This prints "25.0"
# (to generate a SavedModel) tf.saved_model.save(model, "saved_model_tf_dir")
concrete_func = model.__call__.get_concrete_function()

# Convert the model
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func],
                                                            model)
tflite_model = converter.convert()

执行行“concrete_func = model.__call__.get_concrete_function()”时抛出错误:TypeError: tf____call__() missing 1 required positional argument: 'x'

我尝试了 TF 2.3、2.4 和 Google Colab notebook,它们都给出了相同的错误。我缺少什么?我怎样才能完成这项工作?

自 TensorFlow 2.7 版本以来,具体函数 API 接受两个参数。在早期版本中,您应该只传递第一个参数。抱歉,官方指南应该相应更新。

检查this colab notebook to see the usage. Also the documentation has been updated. Check here