user6027414
user6027414

Reputation: 41

Keras model save and load: ValueError: Could not find matching function to call loaded from the SavedModel

System information Have written custom code: Yes OS Platform and Distribution: Windows 10 Mobile device: No TensorFlow installed from: conda install tensorflow TensorFlow version: 2.1.0 (CPU only) Python version: 3 Describe the current behavior I train a TF Keras model (summary below) and use it for slot classification and it works fine.

I then save it using: tf.saved_model.save(joint_model, 'BERT2.tf') I then load the model: bertmodel = tf.keras.models.load_model('BERT2.tf', compile=False) It is compiled using the same arguments as saved model. And then try predictions using the same function as before but I getting the following error: ValueError: Could not find matching function to call loaded from the SavedModel

Describe the expected behavior I run prediction using the same function but the loaded model gives the error. I tried installing the nightly tensorflow build but was not successful.

Standalone code to reproduce the issue Provide a reproducible test case that is the bare minimum necessary to generate the problem. If possible, please share a link to Colab/Jupyter/any notebook. Model:

Training:

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):

Other info / logs Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.

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}

Upvotes: 0

Views: 6035

Answers (2)

kiriloff
kiriloff

Reputation: 26333

Use weight saving and loading. Before loading weight, need to construct the model, if this is a subclassed model, needs to be built first, so that it knows the shape of input data

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

Upvotes: 0

user6027414
user6027414

Reputation: 41

I changed the way I was saving and loading the model as suggested by someone in the TF forums. Instead of model.save(), I saved the weights and then loaded the model as per guidelines in Part II-Approach 1 here: https://www.tensorflow.org/guide/keras/save_and_serialize. Seems to be working now.

Upvotes: 1

Related Questions