Shape Error Issues

Could not find matching concrete function to call loaded from the SavedModel (Shape Mismatch Issue)

Could not find matching concrete function to call loaded from the SavedModel.

You can access my code on Kaggle: https://www.kaggle.com/code/dyimahansah/nlp-competition-dansah

Once I attempt to train the model using the following line of code:

history = classifier_model.fit(x=Train_ds,
                                validation_data=Val_ds,
                                epochs=EPOCHS)

I receive the following error:

Traceback (most recent call last)
Cell In[38], line 1
----> 1 history = classifier_model.fit(x=Train_ds,
      2                                 validation_data=Val_ds,
      3                                 epochs=EPOCHS)

File /opt/conda/lib/python3.10/site-packages/keras/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File /tmp/__autograph_generated_filev3ngj1gm.py:15, in outer_factory.<locals>.inner_factory.<locals>.tf__train_function(iterator)
     13 try:
     14     do_return = True
---> 15     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16 except:
     17     do_return = False

File /tmp/__autograph_generated_file__2um3fl.py:74, in outer_factory.<locals>.inner_factory.<locals>.tf__call(self, inputs, training)
     72     result = ag__.converted_call(ag__.ld(smart_cond).smart_cond, (ag__.ld(training), ag__.autograph_artifact(lambda : ag__.converted_call(ag__.ld(f), (), dict(training=True), fscope)), ag__.autograph_artifact(lambda : ag__.converted_call(ag__.ld(f), (), dict(training=False), fscope))), None, fscope)
     73 result = ag__.Undefined('result')
---> 74 ag__.if_stmt(ag__.not_(ag__.ld(self)._has_training_argument), if_body_3, else_body_3, get_state_3, set_state_3, ('result', 'training'), 1)
     76 def get_state_6():
     77     return (result,)

File /tmp/__autograph_generated_file__2um3fl.py:72, in outer_factory.<locals>.inner_factory.<locals>.tf__call.<locals>.else_body_3()
     70     training = False
     71 ag__.if_stmt(ag__.ld(self).trainable, if_body_2, else_body_2, get_state_2, set_state_2, ('training',), 1)
---> 72 result = ag__.converted_call(ag__.ld(smart_cond).smart_cond, (ag__.ld(training), ag__.autograph_artifact(lambda : ag__.converted_call(ag__.ld(f), (), dict(training=True), fscope)), ag__.autograph_artifact(lambda : ag__.converted_call(ag__.ld(f), (), dict(training=False), fscope))), None, fscope)

File /tmp/__autograph_generated_file__2um3fl.py:72, in outer_factory.<locals>.inner_factory.<locals>.tf__call.<locals>.else_body_3.<locals>.<lambda>()
     70     training = False
     71 ag__.if_stmt(ag__.ld(self).trainable, if_body_2, else_body_2, get_state_2, set_state_2, ('training',), 1)
---> 72 result = ag__.converted_call(ag__.ld(smart_cond).smart_cond, (ag__.ld(training), ag__.autograph_artifact(lambda : ag__.converted_call(ag__.ld(f), (), dict(training=True), fscope)), ag__.autograph_artifact(lambda : ag__.converted_call(ag__.ld(f), (), dict(training=False), fscope))), None, fscope)

ValueError: in user code:

    File "/opt/conda/lib/python3.10/site-packages/keras/engine/training.py", line 1249, in train_function  *
        return step_function(self, iterator)
    File "/opt/conda/lib/python3.10/site-packages/keras/engine/training.py", line 1233, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/opt/conda/lib/python3.10/site-packages/keras/engine/training.py", line 1222, in run_step  **
        outputs = model.train_step(data)
    File "/opt/conda/lib/python3.10/site-packages/keras/engine/training.py", line 1023, in train_step
        y_pred = self(x, training=True)
    File "/opt/conda/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/tmp/__autograph_generated_file__2um3fl.py", line 74, in tf__call
        ag__.if_stmt(ag__.not_(ag__.ld(self)._has_training_argument), if_body_3, else_body_3, get_state_3, set_state_3, ('result', 'training'), 1)
    File "/tmp/__autograph_generated_file__2um3fl.py", line 72, in else_body_3
        result = ag__.converted_call(ag__.ld(smart_cond).smart_cond, (ag__.ld(training), ag__.autograph_artifact(lambda : ag__.converted_call(ag__.ld(f), (), dict(training=True), fscope)), ag__.autograph_artifact(lambda : ag__.converted_call(ag__.ld(f), (), dict(training=False), fscope))), None, fscope)
    File "/tmp/__autograph_generated_file__2um3fl.py", line 72, in <lambda>
        result = ag__.converted_call(ag__.ld(smart_cond).smart_cond, (ag__.ld(training), ag__.autograph_artifact(lambda : ag__.converted_call(ag__.ld(f), (), dict(training=True), fscope)), ag__.autograph_artifact(lambda : ag__.converted_call(ag__.ld(f), (), dict(training=False), fscope))), None, fscope)

    ValueError: Exception encountered when calling layer 'preprocessing' (type KerasLayer).
    
    in user code:
    
        File "/opt/conda/lib/python3.10/site-packages/tensorflow_hub/keras_layer.py", line 237, in call  *
            result = smart_cond.smart_cond(training,
    
        ValueError: Could not find matching concrete function to call loaded from the SavedModel. Got:
          Positional arguments (3 total):
            * <tf.Tensor 'inputs:0' shape=(None, 2) dtype=string>
            * False
            * None
          Keyword arguments: {}
        
         Expected these arguments to match one of the following 4 option(s):
        
        Option 1:
          Positional arguments (3 total):
            * TensorSpec(shape=(None,), dtype=tf.string, name='sentences')
            * False
            * None
          Keyword arguments: {}
        
        Option 2:
          Positional arguments (3 total):
            * TensorSpec(shape=(None,), dtype=tf.string, name='sentences')
            * True
            * None
          Keyword arguments: {}
        
        Option 3:
          Positional arguments (3 total):
            * TensorSpec(shape=(None,), dtype=tf.string, name='inputs')
            * False
            * None
          Keyword arguments: {}
        
        Option 4:
          Positional arguments (3 total):
            * TensorSpec(shape=(None,), dtype=tf.string, name='inputs')
            * True
            * None
          Keyword arguments: {}
    
    
    Call arguments received by layer 'preprocessing' (type KerasLayer):
      • inputs=tf.Tensor(shape=(None, 2), dtype=string)
      • training=True

I know that there is a shape mismatch but I am not clear on how to solve it. I believe the error is in reference to the following lines of code:

type def build_classifier_model():
  text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='sentences')
  preprocessing_layer = hub.KerasLayer(tfhub_handle_preprocess, input_shape=[2], name='preprocessing')
  encoder_inputs = preprocessing_layer(text_input)
  encoder = hub.KerasLayer(tfhub_handle_encoder, trainable=True, name='BERT_encoder')
  outputs = encoder(encoder_inputs)
  net = outputs['pooled_output']
  net = tf.keras.layers.Dropout(0.1)(net)
  net = tf.keras.layers.Dense(1, activation=None, name='classifier')(net)
  return tf.keras.Model(text_input, net)here

Hi @Dansah, I am unable to download the kaggle competition dataset for reproducing the issue. Could you please share the dataset by placing the dataset in the shared drive. Thank You.

Hi @Kiran_Sai_Ramineni,

 Thank you for helping me. Is the shared drive you are referring to on Kaggle or on Tensorflow Forum?

Hi @Dansah, The share drive is for the kaggle dataset. Because i am not able to download the dataset from kaggle. Thank You.

Please see this link to the data in my google drive: https://drive.google.com/drive/folders/15vbqZkn3Q-lVh6jESjb-zytn1VlyrmPx?usp=sharing

Thanks again for your help!

1 Like

I re-wrote it, in what seems to me a simpler way, and seems to run (no idea if this is the correct way):

https://drive.google.com/file/d/1jh3VDEsbHBviopBHzm-yjPWmeRJC540x/view?usp=sharing

Maybe @Kiran_Sai_Ramineni can better guide you here @Dansah

Last cell is actually:

test = train.take(int(train.cardinality().numpy()*0.2))
train = train.skip(int(train.cardinality().numpy()*0.2))
classifier_model.fit(train, epochs=EPOCHS, verbose=2, validation_split=0.2, validation_data=test)

I am too lazy to update the drive version, this was a simple enough way to split the dataset.

Hi @Mah_Neh! Thank you for your help. I will study your code.