Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can Not Load a Saved Model Within the Same Environment #19039

Closed
shahiryar opened this issue Jan 8, 2024 · 5 comments
Closed

Can Not Load a Saved Model Within the Same Environment #19039

shahiryar opened this issue Jan 8, 2024 · 5 comments

Comments

@shahiryar
Copy link

shahiryar commented Jan 8, 2024

Hi,
I fine-tuned the following hugging face distilbert model for text classification.

from transformers import TFAutoModelForSequenceClassification

model = TFAutoModelForSequenceClassification.from_pretrained(
    "distilbert-base-uncased", num_labels=2, id2label=id2label, label2id=label2id
)

After fine-tuning the model, it was saved successfully, however, when I tried loading it, it gave me this error as shown in the image below: ValueError: Layer 'pre_classifier' expected 0 variables, but received 2 variables during loading. Expected: []

Screenshot 2024-01-08 at 2 43 33 PM

The Summary of the model looks like this:

Model: "tf_distil_bert_for_sequence_classification"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 distilbert (TFDistilBertMa  multiple                  66362880  
 inLayer)                                                        
                                                                 
 pre_classifier (Dense)      multiple                  590592    
                                                                 
 classifier (Dense)          multiple                  1538      
                                                                 
 dropout_19 (Dropout)        multiple                  0         
                                                                 
=================================================================
Total params: 66955010 (255.41 MB)
Trainable params: 66955010 (255.41 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

What could have done wrong and how can I fix this issue so can load my model again?

@dugujiujian1999
Copy link
Contributor

dugujiujian1999 commented Jan 8, 2024

Try: #18830
What versions of Keras and TensorFlow were used for saving?

@sachinprasadhs
Copy link
Collaborator

@shahiryar , Could you please use Keras 3 and import Keras directly and try it.
Refer this Migration guide for any reference https://keras.io/guides/migrating_to_keras_3/

Copy link

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions bot added the stale label Jan 23, 2024
@shahiryar
Copy link
Author

I upgraded my environment to use Keras 3, but that didn't work. It is probably because I am using pretrained model from hugging face which uses keras 2 on the backend.
However, I found a way around it by saving the model in a .h5 file and then using the .load_weights function to load the model.
That works for me.

Copy link

Are you satisfied with the resolution of your issue?
Yes
No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants