You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
load_model - the two structures don't have the same nested structure #14345
I trained a model and saved it using: model.save("model_name")
I then call model = tf.keras.models.load_model("model_name")
and get a "The two structures don't have the same nested structure." error. The structure that the model is expecting upon loading is not what I defined in the model.
More specifically: The two namedtuples don't have the same sequence type. First structure type=dict str={'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='inputs/input_ids')} has type dict, while second structure type=list str=[TensorSpec(shape=(None, 64), dtype=tf.int32, name='inputs/0'), TensorSpec(shape=(None, 64), dtype=tf.int32, name='inputs/1')] has type list
During handling of the above exception, another exception occurred:
Reacted by Gabriel Nogueira, Manav Rathod, Nash2325138, xiaonan, Antonio, DongGeon Lee, Ivan Zilotti and zhy-there
Same issue with TF 2.5.0-rc3 and Transformer 4.5.1.
Using out of the box Bert classifier: model = TFBertForSequenceClassification.from_pretrained(bert-base-multilingual-uncased',num_labels=2)
then traning the model and then trying to save it give me the same error
TypeError: The two structures don't have the same nested structure.
First structure: type=dict str={'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='input_ids/input_ids')}
Second structure: type=list str=[TensorSpec(shape=(None, 128), dtype=tf.int32, name='input_ids/0')]
saving the mode .hd5 results in the same error message we reloading the model: model.save('bert_input.hd5')
It seems some class/function definition are not saved with the model so reloading the model will not work. Maybe there are some paramters to set to have this working ? (Stackoverflow)
Then, it's trained and save as SavedModel. But when load it back, it's an error.
The two structures don't have the same nested structure.
First structure: type=dict str={'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='input_ids/input_ids')}
Second structure: type=dict str={'input_ids': TensorSpec(shape=(None, 512), dtype=tf.int32, name='input_ids/input_ids'), 'attention_mask': TensorSpec(shape=(None, 512), dtype=tf.int32, name='input_ids/attention_mask')}
More specifically: The two dictionaries don't have the same set of keys. First structure has keys type=list str=['input_ids'], while second structure has keys type=list str=['input_ids', 'attention_mask']
Entire first structure:
{'input_ids': .}
Entire second structure:
{'input_ids': ., 'attention_mask': .}
I finally solved this issue by using tf.saved_model.save(model, path_to_dir) instead of model.save(path_to_dir) or tf.keras.models.save_model(model, path_to_dir).
Reacted by Vicky Vishal Sahu, GiovanniGedosal, Johannes Janousek, William Ong, Satwik Ram, Jonathan Ross Claytor, Buddhikailabs, Christoph Meyer and Steve Mitchell
@eyalshafran I tried to replicate this issue with transformers 4.11.3 and tensorflow 2.8.2 and running into a different error. Here's the gist. Also as mentioned above can you please use tf.saved_model.load() instead of tf.keras.models.load_model() .
If you think this is still a bug please create a different issue with working example and will look into it.
Thanks!
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-10-3a1d333d606b> in <module>()
----> 1 ner_model_2 = pickle.load(open('ner_model_2.pkl', 'rb'))
2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/nest.py in assert_same_structure(nest1, nest2, check_types, expand_composites)
533 "Entire first structure:\n%s\n"
534 "Entire second structure:\n%s"
--> 535 % (str(e), str1, str2))
536
537
ValueError: The two structures don't have the same nested structure.
First structure: type=tuple str=(({'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='input_ids/input_ids')}, None, None, None, None, None, None, None, None, None, None, None, None, False), {})
Second structure: type=tuple str=((TensorSpec(shape=(None, 384), dtype=tf.int32, name='input_ids'), TensorSpec(shape=(None, 384), dtype=tf.int32, name='attention_mask'), TensorSpec(shape=(None, 384), dtype=tf.int32, name='token_type_ids'), None, None, None, None, None, None, None, None, None, None, False), {})
More specifically: Substructure "type=dict str={'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='input_ids/input_ids')}" is a sequence, while substructure "type=TensorSpec str=TensorSpec(shape=(None, 384), dtype=tf.int32, name='input_ids')" is not
Entire first structure:
(({'input_ids': .}, ., ., ., ., ., ., ., ., ., ., ., ., .), {})
Entire second structure:
((., ., ., ., ., ., ., ., ., ., ., ., ., .), {})
I have figured out a way to overcome this problem. Instead of saving the model using model.save or tf.keras.models.save_model(), try saving the model weights using model.save_weights('model_weight.h5') during the training process.
At the prediction or testing phase, you need to create the model (architecture) manually, the same way you did in the training phase, and then load the weights to it.
I try your mention, save_weights, create the model, and load_weights, but get this error:
ValueError: Unable to load weights saved in HDF5 format into a subclassed Model which has not created its variables yet. Call the Model first, then load the weights.
Similar, I build my model using transformers. In init function, I build a albert and load weight by from_pretrained()
Activity
wildangunawan commentedon Jan 28, 2021
I'm having the same issue with TF 2.4.1 and Transformers 4.2.2.
First structure also have shape=(None, 5) while second structure is shape=(None, maximum length of a sequence).
Edit:
Workaround for BERT: Save your model as .h5 instead of .tf. I didn't test this for other but it might worth a try.
tarrade commentedon May 7, 2021
Same issue with TF 2.5.0-rc3 and Transformer 4.5.1.
Using out of the box Bert classifier:
model = TFBertForSequenceClassification.from_pretrained(bert-base-multilingual-uncased',num_labels=2)then traning the model and then trying to save it give me the same error
saving the mode .hd5 results in the same error message we reloading the model:
model.save('bert_input.hd5')tarrade commentedon May 7, 2021
Same issue with
tensorflow 2.5.0-rc3Using Huggingface save/load is working fine
But this is not we need for tf.serving.
It seems some class/function definition are not saved with the model so reloading the model will not work. Maybe there are some paramters to set to have this working ? (Stackoverflow)
chenlongzhen commentedon May 8, 2021
Same issue
samsatp commentedon Aug 12, 2021
Same issue with transformers 4.6.1 and tensorflow 2.4.1
I built the model as the following.
Then, it's trained and save as SavedModel. But when load it back, it's an error.
samsatp commentedon Aug 12, 2021
I finally solved this issue by using
tf.saved_model.save(model, path_to_dir)instead ofmodel.save(path_to_dir)ortf.keras.models.save_model(model, path_to_dir).followed by this guide: https://www.tensorflow.org/guide/saved_model
jvishnuvardhan commentedon Sep 21, 2021
@eyalshafran Can you share a simple standalone code to reproduce the issue? Did you try recent TF/Keras versions? Thanks!
google-ml-butler commentedon Sep 28, 2021
This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.
GiovanniGedosal commentedon Oct 3, 2021
Same issue
20 remaining items
YanlongLai commentedon May 17, 2022
It is not working for me. The first structure is still fixed on 5
google-ml-butler commentedon May 24, 2022
This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.
gowthamkpr commentedon Jun 4, 2022
@eyalshafran I tried to replicate this issue with transformers 4.11.3 and tensorflow 2.8.2 and running into a different error. Here's the gist. Also as mentioned above can you please use
tf.saved_model.load()instead oftf.keras.models.load_model().If you think this is still a bug please create a different issue with working example and will look into it.
Thanks!
google-ml-butler commentedon Jun 11, 2022
This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.
google-ml-butler commentedon Jun 19, 2022
Closing as stale. Please reopen if you'd like to work on this further.
google-ml-butler commentedon Jun 19, 2022
Are you satisfied with the resolution of your issue?
Yes
No
MrKZZ commentedon Aug 3, 2022
I try your mention, save_weights, create the model, and load_weights, but get this error:
ValueError: Unable to load weights saved in HDF5 format into a subclassed Model which has not created its variables yet. Call the Model first, then load the weights.
Similar, I build my model using transformers. In init function, I build a albert and load weight by from_pretrained()