【问题标题】:Keras model save and load: ValueError: Could not find matching function to call loaded from the SavedModelKeras 模型保存和加载:ValueError:找不到匹配的函数来调用从 SavedModel 加载
【发布时间】:2020-07-08 07:22:28
【问题描述】:

系统信息 是否编写了自定义代码:是 操作系统平台和发行版:Windows 10 移动设备:没有 TensorFlow 安装自:conda install tensorflow TensorFlow 版本:2.1.0(仅限 CPU) Python版本:3 描述当前行为 我训练了一个 TF Keras 模型(总结如下)并将其用于插槽分类,效果很好。

然后我使用:tf.saved_model.save(joint_model, 'BERT2.tf') 保存它 然后我加载模型:bertmodel = tf.keras.models.load_model('BERT2.tf', compile=False) 它使用与保存模型相同的参数进行编译。 然后尝试使用与以前相同的函数进行预测,但出现以下错误:ValueError: Could not find matching function to call loaded from the SavedModel

描述预期的行为 我使用相同的函数运行预测,但加载的模型给出了错误。 我尝试安装 nightly tensorflow build 但没有成功。

重现问题的独立代码 提供一个可重现的测试用例,这是生成所需的最低限度 问题。如果可能,请分享指向 Colab/Jupyter/任何笔记本的链接。 型号:

培训:

joint_model.compile(optimizer=opt, loss=losses, metrics=metrics)
history = joint_model.fit(encoded_train, slot_train, epochs=15, batch_size=32)

Function for predictions:

def show_predictions(text, tokenizer, model, slot_names):

其他信息/日志包括任何有助于 诊断问题。如果包括回溯,请包括完整的 追溯。应附上大型日志和文件。

ValueError: Could not find matching function to call loaded from the SavedModel. Got:
  Positional arguments (1 total):
    * Tensor("inputs:0", shape=(1, 8), dtype=int32)
  Keyword arguments: {'training': False}

Expected these arguments to match one of the following 4 option(s):

Option 1:
  Positional arguments (1 total):
    * {'attention_masks': TensorSpec(shape=(None, 25), dtype=tf.int32, name='inputs/attention_masks'), 'input_ids': TensorSpec(shape=(None, 25), dtype=tf.int32, name='inputs/input_ids')}
  Keyword arguments: {'training': False}

Option 2:
  Positional arguments (1 total):
    * {'attention_masks': TensorSpec(shape=(None, 25), dtype=tf.int32, name='attention_masks'), 'input_ids': TensorSpec(shape=(None, 25), dtype=tf.int32, name='input_ids')}
  Keyword arguments: {'training': False}

Option 3:
  Positional arguments (1 total):
    * {'attention_masks': TensorSpec(shape=(None, 25), dtype=tf.int32, name='inputs/attention_masks'), 'input_ids': TensorSpec(shape=(None, 25), dtype=tf.int32, name='inputs/input_ids')}
  Keyword arguments: {'training': True}

Option 4:
  Positional arguments (1 total):
    * {'attention_masks': TensorSpec(shape=(None, 25), dtype=tf.int32, name='attention_masks'), 'input_ids': TensorSpec(shape=(None, 25), dtype=tf.int32, name='input_ids')}
  Keyword arguments: {'training': True}

【问题讨论】:

    标签: tensorflow keras save tf.keras


    【解决方案1】:

    我按照 TF 论坛中某人的建议更改了保存和加载模型的方式。我没有使用 model.save(),而是保存了权重,然后按照此处第 II 部分方法 1 中的指南加载模型:https://www.tensorflow.org/guide/keras/save_and_serialize。 似乎现在可以工作了。

    【讨论】:

      【解决方案2】:

      使用减重和加载。在加载权重之前,需要先构建模型,如果是子类模型,需要先构建模型,这样才能知道输入数据的形状

      https://www.tensorflow.org/guide/keras/custom_layers_and_models#best_practice_deferring_weight_creation_until_the_shape_of_the_inputs_is_known

      【讨论】:

        猜你喜欢
        • 2019-12-25
        • 2020-08-28
        • 1970-01-01
        • 2020-02-22
        • 1970-01-01
        • 2020-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多