Failed to Import transformers.models

Hello!
I need some help to fix my “RunTimeError” message.
The code used:
from transformers import TFAutoModel
checkpoint=“distilbert-base-uncased-finetuned-sst-2-english”
model=TFAutoModel.from_pretrained(checkpoint)

And the long error message
RuntimeError: Failed to import transformers.models.distilbert.modeling_tf_distilbert because of the following error (look up to see its traceback):
No module named ‘keras.saving.hdf5_format’

I cant see where I have done something wrong, it seems more there is something wrong with the model?

1 Like

@Henrkkn Have you tried updating TensorFlow?

pip install -U tensorflow==2.10 

After installed the Tensorflow as mentioned, I now receive a new RunTime Error:

"RuntimeError: Failed to import transformers.models.distilbert.modeling_tf_distilbert because of the following error (look up to see its traceback):
Duplicate registrations for type ‘experimentalOptimizer’ "

Rolling back to 2.10 results out with google protobuf disconnect error.

@chem1 I tried this:
from transformers import TFAutoModel
checkpoint = ‘distilbert-base-uncased-finetuned-sst-2-english’
model = TFAutoModel.from_pretrained(checkpoint)

I am getting this:
RuntimeError: Failed to import transformers.models.distilbert.modeling_tf_distilbert because of the following error (look up to see its traceback):
module ‘tensorflow._api.v2.compat.v2.internal’ has no attribute ‘register_load_context_function’

I am using latest version of everything. More specifically, transformers = 4.38.2, tensorflow = 2.16, keras = 3.1.1, protobuf = 5.26.0

Maybe the issue is because of keras 3 which is the default since tf 2.16. suggest trying out with lower tf version.