Tensorflow Keras模型保存和加载:ValueError:找不到要从SavedModel加载的调用的匹配函数

Tensorflow Keras模型保存和加载:ValueError:找不到要从SavedModel加载的调用的匹配函数,tensorflow,keras,save,tf.keras,Tensorflow,Keras,Save,Tf.keras,系统信息 是否编写了自定义代码:是 操作系统平台和发行版:Windows 10 移动设备:否 TensorFlow安装自:conda安装TensorFlow TensorFlow版本:2.1.0(仅限CPU) Python版本:3 描述当前的行为 我训练了一个TF Keras模型(总结如下),并将其用于插槽分类,效果良好 然后我使用:tf.saved\u model.save(joint\u model,'BERT2.tf') 然后我加载模型:bertmodel=tf.keras.models.

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

然后我使用:
tf.saved\u model.save(joint\u model,'BERT2.tf')
然后我加载模型:
bertmodel=tf.keras.models.load\u model('BERT2.tf',compile=False)
它使用与保存的模型相同的参数进行编译。 然后尝试使用与之前相同的函数进行预测,但我得到以下错误:ValueError:找不到匹配的函数以从SavedModel加载调用

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

复制问题的独立代码 提供一个可复制的测试用例,该用例是生成 这个问题。如果可能,请共享指向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}

我改变了保存和加载模型的方式,这是TF论坛上有人建议的。我没有保存model.save(),而是保存了权重,然后按照第二部分方法1中的指南加载模型。 现在好像在工作